URL-dependent templates only evaluated once, incorrectly cached
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.