Skip to content

Instantly share code, notes, and snippets.

@Nooshu
Created February 20, 2021 23:35
Show Gist options
  • Save Nooshu/58718d04e4d1026187cba3dc3ced5f6d to your computer and use it in GitHub Desktop.
Save Nooshu/58718d04e4d1026187cba3dc3ced5f6d to your computer and use it in GitHub Desktop.
Cloudflare Worker code used when modifying the priority hints for the Nike UK homepage.
// set the site we are modifying
const site = 'www.nike.com';
// do this on a fetch
addEventListener('fetch', event => {
const request = event.request
const url = new URL(request.url)
event.respondWith(handleRequest(request))
});
async function handleRequest(request) {
// store the URL
const url = new URL(request.url);
// disallow crawlers (write a robots.txt file)
if(url.pathname === "/robots.txt") {
return new Response('User-agent: *\nDisallow: /', {status: 200});
}
// when overrideHost is used in a WPT script, WPT sets x-host to original host i.e. site we want to proxy
// store the value of x-host
const xhost = request.headers.get('x-host');
// If this header is missing, abort
if(!xhost) {
return new Response('x-host header missing', {status: 403});
}
// set our hostname to that listed in the xhost header
url.hostname = xhost;
// look for header that allows us to bypass the transform entirely
const bypassTransform = request.headers.get('x-bypass-transform');
// get the accept header to allow us to examine the type of request it is
const acceptHeader = request.headers.get('accept');
// check our x-host header matches the site we are testing and we aren't trying to bypass the transformation
if(xhost === site && (!bypassTransform || (bypassTransform && bypassTransform.indexOf('true') === -1))){
// check to see the request is for the HTML page
if(acceptHeader && acceptHeader.indexOf('text/html') >= 0){
// store this particular request for modification
let oldResponse = await fetch(url.toString(), request)
// create a new response
let newResponse = new HTMLRewriter()
// drop the preload importance
.on('link[href="https://www.nike.com/assets/experience/ciclp/landing-pages/static/v2/common/vendor.6a1e0df25e0509359e59.js"]', new SetPriorityLow())
.on('link[href="https://www.nike.com/assets/experience/ciclp/landing-pages/static/v2/1494-4685d103b4e/client.7b2ff9cd8475a0f2cf37.js"]', new SetPriorityLow())
// modify the HTML
.transform(oldResponse)
// return the modified page
return newResponse
}
}
// otherwise just proxy the request unmodified
return fetch(url.toString(), request)
}
class addPreloads {
element(element) {
// prepend it to the specified element
element.prepend(preloads, {html: true});
}
}
class removePreloads {
element(element) {
element.remove();
}
}
class SetPriorityLow {
element(e) {
e.setAttribute("importance", "low");
}
}
class SetPriorityHigh {
element(e) {
e.setAttribute("importance", "high");
}
}
class SetPriorityAuto {
element(e) {
e.setAttribute("importance", "auto");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment