Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevin39/2f19e47a6552f2a873744bf2922d4b3c to your computer and use it in GitHub Desktop.
Save kevin39/2f19e47a6552f2a873744bf2922d4b3c to your computer and use it in GitHub Desktop.

Revisions

  1. @section-io-gists section-io-gists created this gist Mar 15, 2017.
    53 changes: 53 additions & 0 deletions pagespeed-requirement.vcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    # Note: You will want to add the snippet: `include "pagespeed-requirement.vcl";` above your `vcl_recv` in the default.vcl file.

    sub vcl_recv {
    call pagespeed_capability_detection;
    }

    # Function derived from requirements here https://modpagespeed.com/doc/downstream-caching#ps-capabilitylist
    # Additional detection logic for crawlers, tablet and mobile devices.
    sub pagespeed_capability_detection {
    if (req.http.User-Agent ~ "(?i)Chrome/[3][2-9]+\.|Chrome/[4-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
    # Cache-fragment 1: Desktop User-Agents that support lazyload_images (ll),
    # inline_images (ii), defer_javascript (dj), webp (jw) and lossless_webp
    # (ws).
    set req.http.PS-CapabilityList = "ll,ii,dj,jw,ws:";
    } else if (req.http.User-Agent ~ "(?i)Firefox/[4-9].+|Firefox/[\d][\d]+|MSIE 10\.|Safari") {
    # Cache-fragment 2: Desktop User-Agents that support lazyload_images (ll),
    # inline_images (ii) and defer_javascript (dj).
    # Note: Wget is added for testing purposes only.
    set req.http.PS-CapabilityList = "ll,ii,dj:";
    } else {
    # As a fallback, the PS-CapabilityList header that is sent to the upstream
    # PageSpeed server should be for a device with no browser specific optimizations.
    set req.http.PS-CapabilityList = "fully general optimizations only";
    }

    # Cache-fragment 3: This fragment contains (a) Desktop User-Agents that
    # match fragments 1 or 2 but should not because they represent older
    # versions of certain browsers or bots and (b) Tablet User-Agents that
    # on all browsers and use image compression qualities applicable to large
    # screens. Note that even Tablets that are capable of supporting inline or
    # webp images, e.g. Android 4.1.2, will not get these advanced
    # optimizations.
    if (req.http.User-Agent ~ "(?i)Firefox/[1-2]\.|MSIE [5-8]\.|bot|Yahoo!|Ruby|RPT-HTTPClient|(Google \(\+https\:\/\/developers\.google\.com\/\+\/web\/snippet\/\))|Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire") {
    set req.http.PS-CapabilityList = "LargeScreen.SkipUADependentOptimizations:";
    }
    # Cache-fragment 4: Mobiles and small screen Tablets will use image compression
    # qualities applicable to small screens, but all other optimizations will be
    # those that work on all browsers.
    if (req.http.User-Agent ~ "(?i)Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo|Galaxy Nexus|GT-I9300|GT-N7100|HTC One|Nexus [4|7|S]|Xoom|XT907") {
    set req.http.PS-CapabilityList = "TinyScreen.SkipUADependentOptimizations:";
    }
    }

    sub vcl_hash {
    # Split cache by PageSpeed capability
    hash_data(req.http.PS-CapabilityList);
    }

    sub vcl_deliver {
    # The following line can be uncommented for debugging purposes and will
    # add a response header to show the header parameter sent to pagespeed.
    #set resp.http.section-io-PS-capability = req.http.PS-CapabilityList;
    }