Skip to content

Commit

Permalink
send rel=preload link headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldewyn committed Mar 1, 2024
1 parent 56325ca commit 233ce10
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions codepoints.net/lib/Controller/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
namespace Codepoints\Controller;

use Codepoints\Controller;
use Codepoints\Controller\TraitPreload;
use Codepoints\Router\NotFoundException;
use Codepoints\Router\Pagination;
use Codepoints\Unicode\Block as UnicodeBlock;


class Block extends Controller {

use TraitPreload;

/**
* @param Array $data
*/
public function __invoke($data, Array $env) : string {
$this->sendPreloadHeaders([
/* TODO preloading SVGs for <use> 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();

Expand Down
4 changes: 4 additions & 0 deletions codepoints.net/lib/Controller/Codepoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions codepoints.net/lib/Controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions codepoints.net/lib/Controller/StaticPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand Down
24 changes: 24 additions & 0 deletions codepoints.net/lib/Controller/TraitPreload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Codepoints\Controller;


trait TraitPreload {

private function sendPreloadHeaders(Array $additional=[]) : void {
$headers = array_merge([
sprintf('<%s>; 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 <use> 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: <https://stats.codepoints.net>; rel=preconnect', false);
}

}
2 changes: 1 addition & 1 deletion codepoints.net/lib/view_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<svg width="%s" height="%s"><svg viewBox="194 97 1960 1960" width="100%%" height="100%%">'.
'<use xlink:href="%s"/></svg></svg>', $width, $width, $url);
}
Expand Down

0 comments on commit 233ce10

Please sign in to comment.