From d2a8560c0bff40bbd115ae90a34575bb883ec4cf Mon Sep 17 00:00:00 2001 From: Krystian Szymukowicz Date: Mon, 28 Oct 2024 15:19:10 +0100 Subject: [PATCH 1/2] [FEATURE] Add event to allow manage $resources. --- Classes/Event/ProcessResourcesEvent.php | 23 +++++++++++++++++++++++ Classes/Http/ResourcePusher.php | 23 +++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 Classes/Event/ProcessResourcesEvent.php diff --git a/Classes/Event/ProcessResourcesEvent.php b/Classes/Event/ProcessResourcesEvent.php new file mode 100644 index 0000000..1bad840 --- /dev/null +++ b/Classes/Event/ProcessResourcesEvent.php @@ -0,0 +1,23 @@ +resources = $resources; + } + + public function getResources(): array + { + return $this->resources; + } + + public function setResources(array $resources): void + { + $this->resources = $resources; + } +} diff --git a/Classes/Http/ResourcePusher.php b/Classes/Http/ResourcePusher.php index 3cbbe69..d6de7a7 100644 --- a/Classes/Http/ResourcePusher.php +++ b/Classes/Http/ResourcePusher.php @@ -1,5 +1,6 @@ handle($request); if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) { $resources = $GLOBALS['TSFE']->config['b13/http2'] ?? null; + /** @var NormalizedParams $normalizedParams */ $normalizedParams = $request->getAttribute('normalizedParams'); if (is_array($resources) && $normalizedParams->isHttps()) { - foreach ($resources['scripts'] ?? [] as $resource) { + $eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class); + $event = $eventDispatcher->dispatch(new ProcessResourcesEvent($resources)); + $resources = $event->getResources(); + foreach ($resources['scripts'] ?? [] as $resource) { $response = $this->addPreloadHeaderToResponse($response, $resource, 'script'); } - foreach ($resources['styles'] ?? [] as $resource) { + foreach ($resources['styles'] ?? [] as $resource) { $response = $this->addPreloadHeaderToResponse($response, $resource, 'style'); } } @@ -49,10 +58,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface protected function addPreloadHeaderToResponse(ResponseInterface $response, string $uri, string $type): ResponseInterface { - if(str_contains($uri, '.mjs')) { - return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=modulepreload; as=' . $type); + if (str_contains($uri, '.mjs')) { + return $response->withAddedHeader('Link', + '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=modulepreload; as=' . $type); } else { - return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); + return $response->withAddedHeader('Link', + '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); } } } From 02185e0402e4e86a527d80f4f6ca8561bb0d539e Mon Sep 17 00:00:00 2001 From: Krystian Szymukowicz Date: Mon, 28 Oct 2024 17:39:17 +0100 Subject: [PATCH 2/2] [FEATURE] Add event to allow manage $resources. Bring back formatting not connected with changed lines. --- Classes/Http/ResourcePusher.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Classes/Http/ResourcePusher.php b/Classes/Http/ResourcePusher.php index d6de7a7..70aaf06 100644 --- a/Classes/Http/ResourcePusher.php +++ b/Classes/Http/ResourcePusher.php @@ -1,6 +1,5 @@ handle($request); if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) { $resources = $GLOBALS['TSFE']->config['b13/http2'] ?? null; - /** @var NormalizedParams $normalizedParams */ $normalizedParams = $request->getAttribute('normalizedParams'); if (is_array($resources) && $normalizedParams->isHttps()) { @@ -58,12 +55,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface protected function addPreloadHeaderToResponse(ResponseInterface $response, string $uri, string $type): ResponseInterface { - if (str_contains($uri, '.mjs')) { - return $response->withAddedHeader('Link', - '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=modulepreload; as=' . $type); + if(str_contains($uri, '.mjs')) { + return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=modulepreload; as=' . $type); } else { - return $response->withAddedHeader('Link', - '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); + return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); } } }