HTTP/2 push not working when using CDN
longbigbronzeelephantfish opened this issue · 2 comments
longbigbronzeelephantfish commented
Configuration:
Generic Mirror used for CDN
HTTP/2 push enabled for CSS and JS
The generated headers instructs the browser to preload the resources from the website:
The browser ends up preloading the resources but not using them
Editing Minify_Plugin.php from
public function http2_header_add( $url, $as ) {
if ( empty( $url ) )
return;
// Cloudflare needs URI without host
$uri = Util_Environment::url_to_uri( $url );
header( 'Link: <' . $uri . '>; rel=preload; as=' . $as, false );
}
to
public function http2_header_add( $url, $as ) {
if ( empty( $url ) )
return;
// Cloudflare needs URI without host
$uri = Util_Environment::url_to_uri( $url );
// Get the domain of the CDN
$domain = Dispatcher::component( 'Cdn_Core' )->get_cdn()->get_prepend_path($uri);
// Append the domain to the uri
header( 'Link: <' . $domain . $uri . '>; rel=preload; as=' . $as, false );
}
fixes the problem, but will probably break CloudFlare functionality.
The headers are correct now:
altimea commented
Same problem here. That line is causing my style to be downloaded twice, once with my website url, and the second with my CDN url
nigrosimone commented
this works for both CDN and CloudFlare
/**
* Sends HTTP/2 push header
*/
public function http2_header_add( $url, $as ) {
if ( empty( $url ) )
return;
$domain = '';
// Cloudflare needs URI without host
$uri = Util_Environment::url_to_uri( $url );
// CDN always require domain
if( $this->config->get_boolean( 'cdn.enabled' ) ){
$domain = Dispatcher::component( 'Cdn_Core' )->get_cdn()->get_prepend_path($uri);
}
header( 'Link: <' . $domain . $uri . '>; rel=preload; as=' . $as, false );
}