- main
- manifest
- deps
- main
- Install
- Run
async function handler(event){
const req = event.request;
let beresp = await fetch(req, {
backend: "origin_0",
});
const synth = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>There was a problem</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet" type="text/css">
<style>
body {
display: inline-block;
background: #7000f9 no-repeat;
height: 100vh;
margin: 0;
color: white;
}
h1 {
margin: .8em 3rem;
font: 4em Roboto;
}
p {
display: inline-block;
margin: .2em 3rem;
font: 2em Roboto;
}
</style>
</head>
<body>
<h1>451 APOLOGIES!</h1>
<p>Something went wrong</p>
</body>
</html>
`;
if (beresp.status === 451) {
beresp = new Response(synth, {
status: 451,
headers: {
"Content-Type": "text/html",
},
});
return beresp;
}
else if (beresp.status === 503 || beresp.status === 500 ){
const status = beresp.status;
const cacheOverride = new CacheOverride("pass");
const clonedBeresp = await fetch("https://httpbin.org/anything/some/testing"+Math.random(), {
cacheOverride,
backend: "origin_0"
});
const newResponse = new Response(clonedBeresp.body, {
status,
headers: clonedBeresp.headers
});
return newResponse;
}
}
addEventListener("fetch", (event) => event.respondWith(handler(event)));