How to successfully deploy a worker so a custom maintenance page appears if the website goes down?

I've created a Worker to employ on a site if it goes down during a server maintenance. What domains & routes and/or trigger events would I have to set up at this point to ensure this is what would appear if the site goes down?
23 Replies
Walshy
Walshy4d ago
HTTP trigger, workers route over <domain>/* worker code would fetch site, if bad error code serve a HTML page But I'd recommend Snippets so it's free - they're ideal for this kinda use case. Same code for that and trigger is the same just as a rule
Mech
Mech4d ago
If you: - have pro - have a maintenance page which is under 32KB (with the rest of the snippet logic) - redirect
Beeps85
Beeps85OP4d ago
@Walshy | Workers/Pages Would this be correct? Is there any additional step I have to do to ensure it's enabled?
No description
Beeps85
Beeps85OP4d ago
My failure mode is Fail closed (block)
Walshy
Walshy4d ago
Nope that's fine but you probably don't want to restrict to HTTP I'd hope your traffic isn't HTTP haha You can just remove the http:// to make it handle both HTTP/HTTPS
Beeps85
Beeps85OP4d ago
I did this, and now the site goes the custom page, even though the site isn't down
Walshy
Walshy4d ago
what's your code?
Beeps85
Beeps85OP4d ago
If I use the www protocol it doesn't, but http://dkcustomproducts.com yes I'm going to remove it for now const AUTH_KEY = "4dfgdgdgfdfgdn" const AUTH_VALUE = "234242DFSAASD5432234423t5th" const VALID_AUTH = AUTH_KEY + "=" + AUTH_VALUE; addEventListener("fetch", event => { event.respondWith(handle(event.request)) }) async function handle(request) { const cookies = request.headers.get("Cookie") ""; // Check for cookie or token in url if (cookies.includes(VALID_AUTH) request.url.includes(VALID_AUTH)) { // User has a valid token, so show the original page const originalResponse = await fetch(request); const response = new Response(originalResponse.body, originalResponse); // Store token in cookie if not included already if (!cookies.includes(VALID_AUTH)) { const tokenCookie = ${VALID_AUTH}; Path=/;; response.headers.set('Set-Cookie', tokenCookie); } return response; } else { // User should see the maintenance site const modifiedHeaders = new Headers(); modifiedHeaders.set('Content-Type', 'text/html'); modifiedHeaders.append('Pragma', 'no-cache');
return new Response(maintenancePageHtml, { headers: modifiedHeaders, status: 503, statusText: "Service Unavailable" }); } } const maintenancePageHtml = <!DOCTYPE html> <html lang="en" style=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Under Maintenance</title> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="false"> <meta name="robots" content="nocache"> <meta name="robots" content="noindex,nofollow"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center"> <h1>Under Maintenance! <p>Check back in an hour</h1></p> </body> </html> ;
Walshy
Walshy4d ago
might want to remove that auth stuff but either way, this will serve the page if there's no cookie. I assume you just don't have a cookie. If you want to serve the page when the site is down you wouldn't want to look for a cookie, you'd want to check the response status
Beeps85
Beeps85OP4d ago
It was a maintenance code page I found doing a search for this; I'm not exactly the world's greatest developer so this is a bit of an unknown for me. Is there a generic template to use in this instance?
Walshy
Walshy4d ago
something simple like this would do it
export default {
fetch(req) {
const res = await fetch(req);

if (res.status >= 500) {
return new Response(maintenancePageHtml);
}

return res;
}
}
export default {
fetch(req) {
const res = await fetch(req);

if (res.status >= 500) {
return new Response(maintenancePageHtml);
}

return res;
}
}
Beeps85
Beeps85OP4d ago
What would this replace in the code I provided?
Walshy
Walshy4d ago
everything except the variable at the end
Beeps85
Beeps85OP4d ago
I'm using this export default { fetch(req) { const res = await fetch(req); if (res.status >= 500) { return new Response(maintenancePageHtml); } return res; } } const maintenancePageHtml = <!DOCTYPE html> <html lang="en" style=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Under Maintenance</title> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="false"> <meta name="robots" content="nocache"> <meta name="robots" content="noindex,nofollow"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center"> <h1>Under Maintenance! <p>Check back in an hour</h1></p> </body> </html> ;''' and it's giving an error
Beeps85
Beeps85OP4d ago
No description
Beeps85
Beeps85OP4d ago
No description
Walshy
Walshy4d ago
async fetch(req)
Beeps85
Beeps85OP4d ago
No description
Beeps85
Beeps85OP4d ago
No description
Beeps85
Beeps85OP4d ago
No description
Walshy
Walshy4d ago
no change the func to async leave that as await fetch
Beeps85
Beeps85OP4d ago
No description
Beeps85
Beeps85OP3d ago
This doesn't return any errors now. However, if I preview the worker, the bare bones page doesn't appear(it says nothing is here yet) Is it possible there was something in the original code I had that would display this page correctly? Or even just a generic page ... what I have now is pretty basic, so it doesn't really matter

Did you find this page helpful?