Postponed on #2453059: Set default render cache contexts: 'theme' + 'languages:' . LanguageInterface::TYPE_INTERFACE and #2888838: Optimize render caching.
Problem/Motivation
The language switcher block is currently not cacheable at all.
Proposed resolution
If we have a "language redirect" route, i.e. /language-redirect/<language code>
, then e.g. "français" would link to /language-redirect/fr?destination=node/42
. Then everything except the destination
parameter could be cached globally. The destination
parameter could be set in JS, just like contextual.js
already does today:
// Set the destination parameter on each of the contextual links.
var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath);
$contextual.find('.contextual-links a').each(function () {
var url = this.getAttribute('href');
var glue = (url.indexOf('?') === -1) ? '?' : '&';
this.setAttribute('href', url + glue + destination);
});
To remain JS-less for anonymous users, we could still generate the entire block dynamically for anonymous users, because Drupal assumes that anonymous users get served cached pages anyway. This would then be in line with what we do for active link handling: we set the active
class in PHP (on the server side) for anonymous users, but in JS (on the client side) for authenticated users.
That's the best of both worlds.
Remaining tasks
Blocked on #2107427: Regression: Language names should display in their native names in the language switcher block and #2335661: Outbound path & route processors must specify cacheability metadata and #2888838: Optimize render caching.
User interface changes
None.
API changes
None.