URL-dependent templates only evaluated once, incorrectly cached 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 &gt;&gt; Administration &gt;&gt; Configuration &gt;&gt; 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 &gt;&gt; Administration &gt;&gt; Structure &gt;&gt; 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.
As stated you would need a cache context for the block, more specifically the &quot;url.path&quot; 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 &quot;Contexts&quot; and check &quot;url.path&quot;. Disclaimer: I have not tried this module myself, but it looks like it would solve your problem.