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

#1

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 [email protected]…”

Is this possible to do with a Worker?

If yes, could you point me to an example?

Thank you!

1 Like
#2

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
#3

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: https://www.networkchimp.com/docs/edgy.html

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

#4

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
#5

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
#6

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

#7

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.

#8

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.