Make language switcher block cacheable

Created on 3 April 2014, over 10 years ago
Updated 13 February 2023, almost 2 years ago

Postponed on #2453059: Set default render cache contexts: 'theme' + 'languages:' . LanguageInterface::TYPE_INTERFACE โ†’ and ๐ŸŒฑ Optimize render caching Needs work .

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 ๐ŸŒฑ Optimize render caching Needs work .

User interface changes

None.

API changes

None.

๐Ÿ“Œ Task
Status

Postponed

Version

10.1 โœจ

Component
Language systemย  โ†’

Last updated 1 day ago

  • Maintained by
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @sun
Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024