I have implemented a service with the tag config.factory.override
. In my class I check if the current user is logged in, and if so I change the system.site.page.front
variable to a different page. This kind-of works, but I have a problem with cache. If the first user to visit my site is not logged in that frontpage will be used for everyone, and if a logged in user is first, that version is used for everyone.
This is my current attempt at the getCacheableMetadata
:
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name) {
$metadata = new CacheableMetadata();
$metadata->setCacheContexts([$this->isLoggedIn ? 'user.roles:authenticated' : 'user.roles:anonymous']);
return $metadata;
}
user.roles:anonymous
in both cases. But I don't think this works, in a quick check of the route matching code I couldn't find any evidence that caching depends on the cache metadata you provide here. There are plenty of other options. You can decide later when building and rendering what content you want to display, for rendered output a cache context for user permissions is already default. Or you can redirect, for example anonymous users to a login site. – 4k4 Jun 12 '18 at 7:46