Conditional Routing

The easiest way to deliver different content based on the device being used is to rewrite the URL of the request based on the condition you care about. For example:

Device Type

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)
}

Custom Headers

addEventListener('fetch', event => {
  event.respondWith(fetchAndApply(event.request))
})

async function fetchAndApply(request) {
  const suffix = ''
  //Assuming that the client is sending a custom header
  const cryptoCurrency = request.headers.get('X-Crypto-Currency')
  if (cryptoCurrency === 'BTC') {
    suffix = '/btc'
  } else if (cryptoCurrency === 'XRP') {
    suffix = '/xrp'
  } else if (cryptoCurrency === 'ETH') {
    suffix = '/eth'
  }

  return fetch(request.url + suffix, request)
}