How to condition output based on a URL query string
There are three layers where rendered content can be cached: 


The Page Cache, which stores complete pages based on the URL for anonymous users.
The Dynamic Page Cache, which stores pages with placeholders for dynamic content that is rendered on each request.
The Render Cache, which stores individual rendered elements.


Each has a corresponding cache bin that can be disabled during development in settings.php



To set an additional cache context in hook_preprocess, so that the Dynamic Page cache knows to re-render that fragment and the render cache stores variants as needed, update the #cache variable:

function hook_preprocess_HOOK(&amp;$variables) {
  $variables['#cache']['contexts'][] = 'url.query_args:exclude_urls';
}