Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A naive and incomplete, but functional approach to http2 push
<?php
/**
* Plugin name: HTTP2 Push scripts
* Author: Tom J Nowell
*/
function tomjn_get_dep_url( /*\WP_Dependency*/ $dep ) {
global $wp_version;
$relative = str_replace( site_url(), '', $dep->src );
$ver = $dep->ver;
if ( $ver === false ) {
$ver = $wp_version;
}
return $relative.'?ver='.$ver;
}
function tomjn_push_scripts() {
global $wp_scripts, $wp_styles;
foreach ( $wp_scripts->queue as $handle ) {
header("Link: <".tomjn_get_dep_url( $wp_scripts->registered[$handle] ).">; rel=preload; as=script", false);
}
}
function tomjn_push_styles() {
global $wp_styles;
foreach ( $wp_styles->queue as $handle ) {
header("Link: <".tomjn_get_dep_url( $wp_styles->registered[$handle] ).">; rel=preload; as=style", false);
}
}
add_action( 'wp_enqueue_scripts', 'tomjn_push_scripts', PHP_INT_MAX );
add_action( 'wp_enqueue_scripts', 'tomjn_push_styles', PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.