Set Link header in PHP so it can work in a subfolder

This commit is contained in:
Pierre Rudloff 2020-10-20 23:10:14 +02:00
parent bd61320cfc
commit 496ac212e2
3 changed files with 48 additions and 1 deletions

View file

@ -36,5 +36,4 @@ FileETag None
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy no-referrer
Header add Link "</css/style.css>; rel=preload" "expr=%{CONTENT_TYPE} =~ m#text/html#"
</ifmodule>

View file

@ -0,0 +1,46 @@
<?php
namespace Alltube;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Router;
/**
* Class LinkHeaderMiddleware
* @package Alltube
*/
class LinkHeaderMiddleware
{
/**
* @var Router
*/
private $router;
/**
* LinkHeaderMiddleware constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->router = $container->get('router');
}
/**
* @param Request $request
* @param Response $response
* @param callable $next
* @return mixed
*/
public function __invoke(Request $request, Response $response, callable $next)
{
$response = $response->withHeader(
'Link',
'<' . $this->router->getBasePath() . '/css/style.css>; rel=preload; as=style'
);
return $next($request, $response);
}
}

View file

@ -8,6 +8,7 @@ use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\CspMiddleware;
use Alltube\ErrorHandler;
use Alltube\LinkHeaderMiddleware;
use Alltube\LocaleManagerFactory;
use Alltube\LocaleMiddleware;
use Alltube\LoggerFactory;
@ -44,6 +45,7 @@ try {
$app->add(new LocaleMiddleware($container));
$app->add(new RouterPathMiddleware($container));
$app->add(new CspMiddleware($container));
$app->add(new LinkHeaderMiddleware($container));
// Controllers.
$frontController = new FrontController($container);