How can I have multiple caches of the same page with different query arguments? 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?
You have to add a cache context for the query parameter version to the page:

function THEME_preprocess_page(&amp;$variables) {

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

}


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