Skip to content

Instantly share code, notes, and snippets.

@vdbelt
Last active October 9, 2018 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdbelt/083207890e9ce6ed543a29545fea98ff to your computer and use it in GitHub Desktop.
Save vdbelt/083207890e9ce6ed543a29545fea98ff to your computer and use it in GitHub Desktop.
Send info about device back to origin
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