// Change these to suit your case!
const config = {
  dataset_url: "polymer/flixgem",
  cf: {
    cf: {
      // Always cache this fetch regardless of content type
      // for a max of 5 seconds before revalidating the resource
      cacheTtl: 1800,
      cacheEverything: true
    }
  }
}


function getDatasetUrl() {
  return `https://app.polymersearch.com/polymer/data/${config.dataset_url}`
}

// Function that processes requests to the URL the worker is at
async function handleRequest(request) {
  // Grab the request URL's pathname, we'll use it later
  const url = new URL(request.url)
  var targetPath = url.pathname

  if (
    targetPath === ('/')
  ) {
    return await fetch(getDatasetUrl(), config.cf)
  }

  targetPath = targetPath.replace(`/core`, `/${config.dataset_url}`)


  // Change request URLs to go through to the subdomain
  let response = await fetch(`https://app.polymersearch.com${targetPath}`, config.cf)

  return response
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request))
})