Last active
December 21, 2021 22:55
-
-
Save ribice/6260b280174200b0ef105a78922c3fca to your computer and use it in GitHub Desktop.
Use custom domain for PolymerSearch app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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