szepeviktor / w3-total-cache-fixed

A community driven build of W3 Total Cache. The aim is to continuously incorporate fixes, improvements, and enhancements over the official WordPress release of W3 Total Cache.
https://github.com/szepeviktor/w3-total-cache-fixed/releases
MIT License
237 stars 47 forks source link

HTTP/2 push not working when using CDN #514

Open longbigbronzeelephantfish opened 7 years ago

longbigbronzeelephantfish commented 7 years ago

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:

image

The browser ends up preloading the resources but not using them

image

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:

image

altimea commented 7 years ago

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 7 years ago

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 );
}