Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stackpath Edge Worker Example 1
// script entry point
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
/**
* Fetch and return the request body
* @param {Request} request
*/
async function handleRequest(request) {
try {
/* The request can be modified here before sending it with fetch */
const path = new URL(request.url).pathname; // we need get the url in order to figure out where to route them
const secDomain = "https://agility-gatsby-sync.netlify.com"
//route the /posts and ANY of the gatsby specific resources from netlify...
if (path.indexOf("/posts") != -1
|| path.indexOf("/webpack") != -1
|| path.indexOf("/common") != -1
|| path.indexOf("/component") != -1
|| path.indexOf("/page-data") != -1
|| path.indexOf("/styles") != -1
|| path.indexOf("/app-") != -1) {
request.url = secDomain + path;
}
//return new Response(request.url, { status: 200 });
const response = await fetch(request);
/* The response can be modified here before returning it */
return response;
} catch (e) {
return new Response(e.stack || e, { status: 500 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment