I have a twig template that controls the HTML of the page based off of some URL-specific information. However, this template seems to be evaluated once on the server and then gets cached, never to be evaluated again. The result is that the first page loaded displays the correct information inside this particular block, but every page after that displays the same information as the first page in the block, even though the URLs are obviously different.

To illustrate, page A should have data A loaded on the side in a block, page B should have data B loaded on the side, and so on. If you go to page B first, then you will see data B loaded correctly. However, after that, if you go to page A, you will still see data B loaded on the side, rather than data A. Note that this bug only pertains to the portion of the page that is controlled by the URL-specific logic; the rest of each page loads correctly.

If I go to Home >> Administration >> Configuration >> Development and clear the cache, the next page loads correctly, but then I'm just stuck with a new incorrectly cached chunk of data for all the other pages.

Why is Drupal caching this block whose display is dependent on the URL? More importantly, how can I get rid of this behavior without disabling caching completely?

Update 1: I have tried going to the view for the block (Home >> Administration >> Structure >> Views) and under Advanced Settings selecting None for Caching, but this has no effect.

Update 2: I don't have a custom block with code to modify, so not sure how to incorporate fixes from questions like this.


1 Answer 1


As stated you would need a cache context for the block, more specifically the "url.path" cache context. You could add it in a preprocess hook, but it requires some PHP knowledge.

You could try the Blocache module which lets you change the Cacheability metadata for any block. Once enabled, select "Contexts" and check "url.path". Disclaimer: I have not tried this module myself, but it looks like it would solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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