src/Controller/EntryPoint.php | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Controller/EntryPoint.php b/src/Controller/EntryPoint.php index efe2d64..7e4d20e 100644 --- a/src/Controller/EntryPoint.php +++ b/src/Controller/EntryPoint.php @@ -102,23 +102,22 @@ class EntryPoint extends ControllerBase { }; $urls = $this->renderer->executeInRenderContext($context, $do_build_urls); - $me_url = Url::fromRoute('jsonapi.user--user.individual', ['user' => User::load($this->user->id())->uuid()]) - ->setAbsolute() - ->toString(TRUE); - - $json_response = new CacheableJsonResponse([ + $json_response = new CacheableJsonResponse(); + $doc = [ 'data' => [], 'links' => $urls, - 'meta' => [ - 'links' => [ - 'me' => $me_url->getGeneratedUrl(), - ], - ], - ]); - - // The cacheability of the "me" URL is the cacheability of that URL itself - // plus the fact that its route parameter depends varies by user. - $json_response->addCacheableDependency(CacheableMetadata::createFromObject($me_url)->addCacheContexts(['user'])); + ]; + $json_response->addCacheableDependency((new CacheableMetadata())->addCacheContexts(['user.roles:authenticated'])); + if ($this->user->isAuthenticated()) { + $me_url = Url::fromRoute('jsonapi.user--user.individual', ['user' => User::load($this->user->id())->uuid()]) + ->setAbsolute() + ->toString(TRUE); + $doc['meta']['links']['me'] = $me_url->getGeneratedUrl(); + // The cacheability of the "me" URL is the cacheability of that URL itself + // plus the fact that its route parameter depends varies by user. + $json_response->addCacheableDependency(CacheableMetadata::createFromObject($me_url)->addCacheContexts(['user'])); + } + $json_response->setData($doc); if (!$context->isEmpty()) { $json_response->addCacheableDependency($context->pop());