↼ My Two Cents

Cover image for Maintenance Page with Cloudflare Workers

Maintenance Page with Cloudflare Workers

Sometimes you need to take a service offline — upgrades, migrations, or just fixing the mess you accidentally deployed at 2 AM. Instead of letting users hit a broken site, you can drop in a lightweight maintenance page using a Cloudflare Worker. No servers, no Nginx rewrites, no duct tape. Just edge logic.


Why use a Worker?

  • Always on edge – served before any origin call, so downtime at the backend doesn’t matter.
  • Simple rollback – one click deploy/remove in the Cloudflare dashboard.
  • Cheap – Workers free tier covers most maintenance use cases.

The Worker script

Here’s a minimal example:

export default {
  async fetch(request, env, ctx) {
    // return maintenance page for all requests
    return new Response(
      `<!DOCTYPE html>
      <html>
        <head>
          <title>Maintenance</title>
          <meta charset="UTF-8" />
          <style>
            body { font-family: sans-serif; text-align: center; padding: 10%; }
            h1 { font-size: 2em; }
            p { color: #666; }
          </style>
        </head>
        <body>
          <h1>🚧 Under Maintenance</h1>
          <p>We’ll be back shortly. Thanks for your patience.</p>
        </body>
      </html>`,
      {
        headers: {
          "content-type": "text/html;charset=UTF-8",
          "cache-control": "no-store",
        },
        status: 503,
        statusText: "Service Unavailable",
      }
    );
  },
};

Deploying it

  1. Log in to Cloudflare dashboard.
  2. Go to Workers & PagesCreate ApplicationWorker.
  3. Paste the script and save.
  4. Set a route (e.g., example.com/*) so all traffic flows through the Worker.

Toggling maintenance mode

  • Enable: point your route to the Worker.
  • Disable: remove the route (traffic bypasses the Worker).

This way you don’t need to redeploy code — just flip the switch on the route binding.


Extra tweaks

  • Add IP exceptions (so your team can still access the origin).
  • Add a Retry-After header with a timestamp if you want browsers and crawlers to know when to come back.
  • Make the page fancier: company logo, estimated downtime, or a status page link.

Wrap-up

Cloudflare Workers make a quick, reliable maintenance page trivial to deploy. No load balancers, no container juggling, no downtime anxiety. Just edge logic that does exactly what you tell it to.


← How to Set Up IMAP in Outlook …
Access Synology NAS Securely … →