Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
Created August 23, 2019 20:22
Show Gist options
  • Save shagamemnon/88411d19732314d20fb327082ac41682 to your computer and use it in GitHub Desktop.
Save shagamemnon/88411d19732314d20fb327082ac41682 to your computer and use it in GitHub Desktop.
addEventListener('fetch', e => e.respondWith(handleRequest(e.request)))
async function handleRequest(request) {
let url = new URL(request.url)
let params = new URLSearchParams(url.search.slice(1))
let features = {
minify: ['html', 'css', 'js'],
polish: ['lossy', 'lossless', 'off']
}
let opts = {
scrapeShield: false,
minify: {
html: false,
css: false,
js: false
},
polish: 'off'
}
let hdrs = new Headers()
for (const feature in features) {
let toggle = params.get(feature)
if (toggle) {
feature === 'minify' ? opts[feature] = { [toggle]: true } : opts[feature] = toggle
console.log(opts)
}
}
hdrs.append('X-Feature-Settings', JSON.stringify(opts))
let response = await fetch(request, { cf: opts })
for (const header of response.headers) {
hdrs.set(header[0], header[1])
}
return new Response(response.body, {
headers: hdrs,
status: response.status,
statusText: response.statusText
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment