From 233ce108693014fae4393632f8dcc2660db99707 Mon Sep 17 00:00:00 2001 From: Manuel Strehl Date: Fri, 1 Mar 2024 01:12:04 +0100 Subject: [PATCH] send rel=preload link headers --- codepoints.net/lib/Controller/Block.php | 8 +++++++ codepoints.net/lib/Controller/Codepoint.php | 4 ++++ codepoints.net/lib/Controller/Index.php | 8 +++++++ codepoints.net/lib/Controller/StaticPage.php | 5 ++++ .../lib/Controller/TraitPreload.php | 24 +++++++++++++++++++ codepoints.net/lib/view_functions.php | 2 +- 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 codepoints.net/lib/Controller/TraitPreload.php diff --git a/codepoints.net/lib/Controller/Block.php b/codepoints.net/lib/Controller/Block.php index b534aa26..695e4e94 100644 --- a/codepoints.net/lib/Controller/Block.php +++ b/codepoints.net/lib/Controller/Block.php @@ -3,6 +3,7 @@ namespace Codepoints\Controller; use Codepoints\Controller; +use Codepoints\Controller\TraitPreload; use Codepoints\Router\NotFoundException; use Codepoints\Router\Pagination; use Codepoints\Unicode\Block as UnicodeBlock; @@ -10,10 +11,17 @@ class Block extends Controller { + use TraitPreload; + /** * @param Array $data */ public function __invoke($data, Array $env) : string { + $this->sendPreloadHeaders([ + /* TODO preloading SVGs for is not yet supported: + * https://github.com/whatwg/html/issues/8870 */ + #sprintf('<%s>; rel=preload; as=image', static_url('images/LastResort.svg')), + ]); $block = new UnicodeBlock($data, $env['db']); $page = get_page(); diff --git a/codepoints.net/lib/Controller/Codepoint.php b/codepoints.net/lib/Controller/Codepoint.php index 29383adb..5164d85b 100644 --- a/codepoints.net/lib/Controller/Codepoint.php +++ b/codepoints.net/lib/Controller/Codepoint.php @@ -3,6 +3,7 @@ namespace Codepoints\Controller; use Codepoints\Controller; +use Codepoints\Controller\TraitPreload; use Codepoints\Router\NotFoundException; use Codepoints\Unicode\Codepoint as UnicodeCodepoint; use Codepoints\Unicode\CodepointInfo\Aliases; @@ -24,7 +25,10 @@ class Codepoint extends Controller { + use TraitPreload; + public function __invoke($match, Array $env) : string { + $this->sendPreloadHeaders(); new Aliases($env); new CLDR($env); new CSUR($env); diff --git a/codepoints.net/lib/Controller/Index.php b/codepoints.net/lib/Controller/Index.php index 92076e0c..2ecba4be 100644 --- a/codepoints.net/lib/Controller/Index.php +++ b/codepoints.net/lib/Controller/Index.php @@ -3,12 +3,20 @@ namespace Codepoints\Controller; use Codepoints\Controller; +use Codepoints\Controller\TraitPreload; use Codepoints\Unicode\Codepoint; +use Codepoints\View; class Index extends Controller { + use TraitPreload; + public function __invoke($match, Array $env) : string { + $this->sendPreloadHeaders([ + sprintf('<%s>; rel=preload; as=image', static_url('src/images/front_light.webp')), + sprintf('<%s>; rel=preload; as=image', static_url('src/images/front_dark.webp')), + ]); /** @var array */ $data = $env['db']->getOne('SELECT cp, name, gc FROM codepoints diff --git a/codepoints.net/lib/Controller/StaticPage.php b/codepoints.net/lib/Controller/StaticPage.php index 5e046441..b4619d9e 100644 --- a/codepoints.net/lib/Controller/StaticPage.php +++ b/codepoints.net/lib/Controller/StaticPage.php @@ -3,15 +3,20 @@ namespace Codepoints\Controller; use Codepoints\Controller; +use Codepoints\Controller\TraitPreload; use Codepoints\Router\NotFoundException; use Codepoints\View; class StaticPage extends Controller { + + use TraitPreload; + /** * @param string $view */ public function __invoke($view, Array $env) : string { + $this->sendPreloadHeaders(); switch ($view) { case 'about': $data = $env['db']->getOne('SELECT COUNT(*) AS c FROM codepoints USE INDEX (PRIMARY)'); diff --git a/codepoints.net/lib/Controller/TraitPreload.php b/codepoints.net/lib/Controller/TraitPreload.php new file mode 100644 index 00000000..74ec2799 --- /dev/null +++ b/codepoints.net/lib/Controller/TraitPreload.php @@ -0,0 +1,24 @@ +; rel=preload; as=style; fetchpriority=high', static_url('src/css/main.css')), + sprintf('<%s>; rel=preload; as=font; fetchpriority=high; crossorigin=anonymous', static_url('src/fonts/Literata.woff2')), + sprintf('<%s>; rel=preload; as=font; fetchpriority=high; crossorigin=anonymous', static_url('src/fonts/Literata-Italic.woff2')), + sprintf('<%s>; rel=preload; as=style; fetchpriority=low', static_url('src/css/print.css')), + /* TODO preloading SVGs for is not yet supported: + * https://github.com/whatwg/html/issues/8870 */ + # sprintf('<%s>; rel=preload; as=image', static_url('src/images/icons.svg')), + # sprintf('<%s>; rel=preload; as=image', static_url('images/unicode-logo-framed.svg')), + sprintf('<%s>; rel=modulepreload; as=script', static_url('src/js/main.js')), + ], $additional); + header('Link:'.join(',', $headers), false); + header('Link: ; rel=preconnect', false); + } + +} diff --git a/codepoints.net/lib/view_functions.php b/codepoints.net/lib/view_functions.php index 8b04607f..8bfde995 100644 --- a/codepoints.net/lib/view_functions.php +++ b/codepoints.net/lib/view_functions.php @@ -96,7 +96,7 @@ function bl(Block $block, string $rel='', string $class='') : string { */ function blimg(Block $block, int $width=16) : string { $name = str_replace([' ', '_', '-'], '', strtolower($block->name)); - $url = sprintf('/static/images/LastResort.svg#%s', $name); + $url = sprintf('%s#%s', static_url('images/LastResort.svg'), $name); return sprintf(''. '', $width, $width, $url); }