How to use a Worker to detect origin is offline and show a "maintenance" message?

Hi,

I would like to setup an automation so that when my origin server is offline (restarting, upgrading, etc), the user’s browser will automatically show a maintenance page, like:

“Sorry about this, we are just doing some quick maintenance and we’ll be right back.
If you need assistance urgently, please email us at support@…”

Is this possible to do with a Worker?

If yes, could you point me to an example?

Thank you!

1 Like

Not quite, you’d need to know and filter all requests and pipe all traffic via the worker.

It’s better to just add a virtual host with the maintanance message and activate it when needed via script on the serverside.

1 Like

Hi Thomas,

hmm, what you wrote prompted me to look elsewhere. I found that it is possible to do this using the Edgy app.

Here’s how it works: Edgy | Network Chimp

I have done some testing, and it seems like that’s the answer.

It’s fairly easy, you just check the statuscode if it’s 200 or response.ok?

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const response = await fetch('https://example.com')

  if (!response.ok)
    return new Response('Sorry about this, we are just doing some quick maintenance and we’ll be right back.')

  return response
}
1 Like

If you don’t need all traffic to go through the worker, you can just disable/enable the route to enable/disable the maintenance page.

See this guide:
https://www.resdevops.com/2018/03/20/Cloudflare-workers-maintenance-mode-static-page/

This way it would be 5$/mo max (to enable workers), I don’t know how much edgy cost but probably more.

1 Like

Thank you for the example, I will try it out.

Thank you for the code example, exactly what I was looking for. I will try it out.

Edgy has a free option with I did test, and it worked. The paid options give you more customisation with the content of the maintenance page.

You can basically copy and paste any HTML you want if you follow the sample code, I’d prefer full control any day of the week rather than be locked to a subscription.