Last active
October 9, 2018 15:21
-
-
Save vdbelt/083207890e9ce6ed543a29545fea98ff to your computer and use it in GitHub Desktop.
Send info about device back to origin
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
addEventListener('fetch', event => { | |
event.respondWith(fetchAndApply(event.request)) | |
}) | |
function isMobile(userAgent) { | |
return userAgent.match(/iPhone|Android|webOS/i) ? 'Yes' : 'No'; | |
} | |
async function fetchAndApply(request) { | |
let isMobileResult = isMobile(request.headers.get('user-agent')) | |
request = new Request(request) | |
request.headers.set("Is-Mobile", isMobileResult) | |
return fetch(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment