- π¨π¦Canada drclaw
This feels like a bit of a sledgehammer fix since many of the cached "empty" routes could be belong to actual 404s (in which case you _want_ them to be cached for performance). The problem here isn't that the empty routes are being cached, but that they aren't being cached specifically enough.
The thing is that it's usually a module that's introducing the various context that makes a route accessible in one case and not another. In that case it's really up to the module to somehow inform the routing system about those contexts. Luckily core actually does have a way to do this with
CacheableRouteProviderInterface::addExtraCacheKeyPart()
. This was added in #3007669: Add publishing status to path aliases β and allows you to add your own contexts to the route cache key.An example of this is the Domain Access + Country Path module. Country path lets you toggle the active domain by path so:
- example.com/uk -> Domain #1
- example.com/us -> Domain #2In this case when you see a link to example.com/us/whatever from example.com/uk on a cold cache, the empty route gets cached for all domains because the routing system doesn't know that there's a difference based if you're seeing the link on /us vs /uk. In this case Country path or even Domain access could inform the router about this difference. And in fact, there is actually a patch doing just that for Domain Access β¨ Use core route provider with addExtraCacheKeyPart for route caching? Needs review .
Really the hard part is usually figuring out specifically which module is introducing the contexts that cause this behaviour to happen. Ideally, the router would pull from the various render contexts that are floating around automatically, but I've only seen that eluded to in https://www.drupal.org/project/drupal/issues/2799543#comment-13344318 β