I'm using the Groups module (http://dgo.to/group) to set up groups on a SaaS project I'm working on. I've set up a domain field on the groups, allowing groups to have vanity domain. As such, groups can have custom front pages, which are shown at site route. The front page is set as a field on the Group entity. For example, group 1 may set their front page path as /page/1
and group 2 may set it as /page/2
.
To show this front page, I'm using inbound URL processing, showing the different paths on the different domains. To get the different paths to cache independently, I've overridden Drupal\Core\Routing\RouteProviderRouteProvider::getRouteCollectionCacheId()
:
/**
* {@inheritdoc}
*/
protected function getRouteCollectionCacheId(Request $request) {
$cache_id = parent::getRouteCollectionCacheId($request);
// Get the current domain.
$domain = $this->currentPath->getRequestStack()->getCurrentRequest()->getHost();
$cache_id .= ':' . $domain;
return $cache_id;
}
I've got this all working, however I ran into an issue in that when the path to the front page of a group is updated, the page content doesn't update until the router is rebuild.
I did find a solution, rebuilding the router in hook_entity_update()
using \Drupal::service('router.builder')->rebuild()
. This works, but it seems very heavy handed.
I tried finding a way to invalidate the cache tags for the route using the cache tags invalidator, but I could not figure out which cache tags to pass.
So my questions are:
Is there a way to partially rebuild the router, rather than rebuilding the whole thing? Or...
Is there a way to invalidate the cache for a route?