Skip to content

Instantly share code, notes, and snippets.

@luandevpro
Created August 18, 2019 03:47
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 luandevpro/13a00022c812fd8500f30151005b8fd5 to your computer and use it in GitHub Desktop.
Save luandevpro/13a00022c812fd8500f30151005b8fd5 to your computer and use it in GitHub Desktop.
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
let uaSuffix = ''
const ua = request.headers.get('user-agent')
if (ua.match(/iphone/i) || ua.match(/ipod/i)) {
uaSuffix = '/mobile'
} else if (ua.match(/ipad/i)) {
uaSuffix = '/tablet'
}
return fetch(request.url + uaSuffix, request)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment