Skip to content

Instantly share code, notes, and snippets.

@joelvarty
Last active March 10, 2020 17:42
Show Gist options
  • Save joelvarty/21d6c5344f7b3765eeb04b450242a929 to your computer and use it in GitHub Desktop.
Save joelvarty/21d6c5344f7b3765eeb04b450242a929 to your computer and use it in GitHub Desktop.
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