From 9bbe7c6aaf34b7700ab7ee1c2f6cd01b600c5839 Mon Sep 17 00:00:00 2001 From: ste101 Date: Mon, 17 May 2021 14:44:46 +0200 Subject: [PATCH] Option for adding additional files --- Classes/Http/ResourcePusher.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Classes/Http/ResourcePusher.php b/Classes/Http/ResourcePusher.php index f72b010..2d2fd3d 100644 --- a/Classes/Http/ResourcePusher.php +++ b/Classes/Http/ResourcePusher.php @@ -17,6 +17,7 @@ use TYPO3\CMS\Core\Http\NormalizedParams; use TYPO3\CMS\Core\Utility\PathUtility; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; +use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Takes existing accumulated resources and pushes them as HTTP2 headers as middleware. @@ -36,19 +37,42 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface /** @var NormalizedParams $normalizedParams */ $normalizedParams = $request->getAttribute('normalizedParams'); if (is_array($resources) && $normalizedParams->isHttps()) { + // before + foreach ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_http2.']['settings.']['before.'] as $resource) { + $path = GeneralUtility::getFileAbsFileName($resource['file']); + $response = $this->addPreloadHeaderToResponse($response, $path, $resource['type'], $resource['rel'], $resource['mime'], $resource['crossorigin']); + } + foreach ($resources['scripts'] ?? [] as $resource) { $response = $this->addPreloadHeaderToResponse($response, $resource, 'script'); } foreach ($resources['styles'] ?? [] as $resource) { $response = $this->addPreloadHeaderToResponse($response, $resource, 'style'); } + + // after + foreach ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_http2.']['settings.']['after.'] as $resource) { + $path = GeneralUtility::getFileAbsFileName($resource['file']); + $response = $this->addPreloadHeaderToResponse($response, $path, $resource['type'], $resource['rel'], $resource['mime'], $resource['crossorigin']); + } } } return $response; } - protected function addPreloadHeaderToResponse(ResponseInterface $response, string $uri, string $type): ResponseInterface + protected function addPreloadHeaderToResponse(ResponseInterface $response, string $uri, string $type, string $rel = null, string $mimeType = null, string $crossorigin = null): ResponseInterface { - return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); + if($rel === null) { + $rel = 'preload'; + } + $value = '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=' . $rel . '; as=' . $type; + if($mimeType !== null) { + $value .= '; type=' . $mimeType; + } + if($crossorigin !== null) { + $value .= '; crossorigin=' . $crossorigin; + } + return $response->withAddedHeader('Link', $value); } + }