5

Using Drupal 8, I have a page (a node) that has two different renderings depending on a passed in query parameter. This is to support a regular page view in a browser and also an embedded version of the page without the header, footer, etc. Example:

https://www.domain.com/my-page
https://www.domain.com/my-page?version=embedded_view

I've implemented this by using THEME_theme_suggestions_page_alter() in my .theme file to suggest a different page template to use depending on the query parameter. (Perhaps there's a better way.)

When viewed on a site with caching enabled, both of the URLs return the same rendering of the page. How do I get different cached versions for the two URLs?

7

You have to add a cache context for the query parameter version to the page:

function THEME_preprocess_page(&$variables) {

  $variables['#cache']['contexts'][] = 'url.query_args:version';

}

The context will then be used to vary the cached pages by this query argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.