Skip to content

Instantly share code, notes, and snippets.

@ribice
Last active December 21, 2021 22:55
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 ribice/6260b280174200b0ef105a78922c3fca to your computer and use it in GitHub Desktop.
Save ribice/6260b280174200b0ef105a78922c3fca to your computer and use it in GitHub Desktop.
Use custom domain for PolymerSearch app
// 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))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment