Skip to content

Instantly share code, notes, and snippets.

@exodus4d
Last active Sep 9, 2018
Embed
What would you like to do?
Pathfinder HTTP2 ServerPush
<?php
abstract class Controller {
function beforeroute(\Base $f3, $params): bool {
if( !$f3->get('AJAX') ){
// singleton Resource() handels serverPush for static resources
$resource = Resource::instance();
$resource->setOption('filePath', [
'style' => $f3->get('BASE') . '/public/css/' . Config::getPathfinderData('version'),
'script' => $f3->get('BASE') . '/public/js/' . Config::getPathfinderData('version'),
'font' => $f3->get('BASE') . '/public/fonts',
'image' => $f3->get('BASE') . '/public/img'
]);
$resource->register('style', 'pathfinder'); // pathfinder.css
$resource->register('script', 'lib/require'); // lib/require.js
$resource->register('script', 'app'); // app.js
$resource->register('font', 'oxygen-regular-webfont');// oxygen-regular-webfont.woff2
$resource->register('font', 'oxygen-bold-webfont'); // oxygen-bold-webfont.woff2
$resource->register('font', 'fa-regular-400');
$resource->register('font', 'fa-solid-900');
$resource->register('font', 'fa-brands-400');
// First option -> inline preload tags for registered resources and let the browser handle the preloading for resources...
// Pro: Browser decides wether to load the resource or not (browser cache is checked)
$f3->set('tplResource', $resource); // add Resouce() instance for Template render (for <link rel="preload"> tags)
}
}
public function afterroute(\Base $f3){
// Second option -> send a single preload header to client that contains all registered resources...
// => Pro: resources get pushed to the client on initial index.html request
$resource = Resource::instance();
if($resource->getOption('output') === 'header'){
header($resource->buildHeader(), false);
}
}
public function init(\Base $f3) {
// main route -> register some more resources to preload for this route
$resource = Resource::instance();
// ... set output type to "HTTP Header" | default: 'inline' -> renders inline <link rel="preload" ..> Tags in <head>
$resource->setOption('output', 'header');
$resource->register('script', 'app/login');
$resource->register('script', 'app/mappage', 'prefetch'); // 'prefetch' is also supported | default: 'preload'
}
}
// index.html
<html>
<head>
{* inline tags (First [default] option) *}
<check if="{{ @tplResource->getOption('output') === 'inline' }}">
{{ @tplResource->buildLinks() }}
</check>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment