Created
April 22, 2019 15:36
-
-
Save rohenaz/1c04d2d50054a1dd1d9c74c93aa35a43 to your computer and use it in GitHub Desktop.
cloudflare worker - strip referral query parameters such as fbclid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Define regular expressions at top to have them precompiled. | |
*/ | |
const urlRegex = new RegExp('^(refreshce|gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|fbid|fbclid|mr:[A-z]+|ref(id|src))$'); | |
addEventListener('fetch', event => { | |
event.passThroughOnException() | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function handleRequest(request) { | |
let url = new URL(request.url) | |
url = await normalizeUrl(url) | |
let modifiedRequest = new Request(url, request) | |
return fetch(modifiedRequest) | |
} | |
async function normalizeUrl(url) { | |
let deleteKeys = [] | |
for(var key of url.searchParams.keys()) { | |
if(key.match(urlRegex)){ | |
deleteKeys.push(key) | |
} | |
} | |
deleteKeys.map(k => url.searchParams.delete(k)) | |
return url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment