Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Live status checks on a static site, with no backend

My portfolio, mohitpatel.org, is a static site on shared hosting: no server, no serverless functions. I still wanted its hero to show whether the products I work on are actually up right now, not a made-up "99.9% uptime"

My portfolio, mohitpatel.org, is a static site on shared hosting: no server, no serverless functions. I still wanted its hero to show whether the products I work on are actually up right now, not a made-up "99.9% uptime" badge.

With no backend, the only thing that can do the checking is the visitor's browser. Here's how that works, what it can and can't tell you, and the response header that made a perfectly healthy site show up as "unreachable".

The trick: a no-cors HEAD request

A normal cross-origin fetch needs the other site to send CORS headers before you can read the response. But a status check doesn't need to read anything. It only needs to know whether the server answered.

That's exactly what mode: "no-cors" gives you. The request goes out, and fetch resolves with an opaque response for any HTTP answer: 200, 404, even 500. It rejects only when the request can't complete at all: DNS failure, TLS error, connection refused, or your own timeout. Resolved means reachable.

async function once(url, timeoutMs) {
  const ctrl = new AbortController();
  const timer = setTimeout(() => ctrl.abort(), timeoutMs);
  const t0 = performance.now();
  try {
    await fetch(url, {
      method: "HEAD",          // no body to download
      mode: "no-cors",         // opaque response, no CORS needed
      cache: "no-store",       // a cached answer would hide an outage
      credentials: "omit",
      referrerPolicy: "no-referrer",
      signal: ctrl.signal,
    });
    return Math.round(performance.now() - t0);
  } finally {
    clearTimeout(timer);
  }
}

HEAD is allowed in no-cors mode, and it keeps the check light: nothing gets downloaded but headers.

Honest numbers: two requests, keep the faster one

The first request to a host pays for the DNS lookup and the TLS handshake, so on its own it overstates the latency a returning user would feel. Each check therefore sends two requests and keeps the faster time.

A single dropped packet on the visitor's network shouldn't paint a product red either, so a failed first attempt gets one retry before the site is marked down:

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

export async function probe(url, timeoutMs = 8000) {
  try {
    const cold = await once(url, timeoutMs).catch(() => sleep(800).then(() => once(url, timeoutMs)));
    const warm = await once(url, timeoutMs).catch(() => cold);
    return { up: true, ms: Math.min(cold, warm) };
  } catch {
    return { up: false, ms: null };
  }
}

The gotcha: a healthy site that looked down

On the first real run, one of the sites showed as "unreachable" while it was clearly up in another tab. Chrome's console had the answer: net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin.

That site sends this response header:

Cross-Origin-Resource-Policy: same-origin

CORP tells the browser not to let other origins load the resource, even as an opaque no-cors response. The browser blocks it, and from JavaScript's point of view the fetch rejects exactly as if the network had failed. A good security header turned into a false alarm.

The fix is to probe a file that the site does allow cross-origin. A favicon is worth trying: in my case the site's favicon was served without that header even though its pages weren't. Check first:

curl -sI https://example.com/ | grep -i cross-origin-resource-policy
curl -sI https://example.com/favicon.ico | grep -i cross-origin-resource-policy

So each site in the config can name a separate probe URL:

services: [
  { name: "My app", url: "https://app.example.com" },
  { name: "Strict site", url: "https://strict.example.com", probe: "https://strict.example.com/favicon.ico" },
],

If a site blocks every path that way, a browser-only check simply can't see it, and you're better off leaving it out than showing a red dot that isn't true.

Being a polite neighbour

Every visitor's browser now sends requests to these sites, so the checks should stay cheap:

  • They start only after the hero animation settles.
  • They repeat every 60 seconds, but only while the widget is on screen (an IntersectionObserver) and the tab is visible (document.visibilityState).
  • They use HEAD, so nothing is downloaded.

That's two tiny requests a minute to each site, per person who's actually looking, which is negligible next to normal traffic.

What it can't tell you

This is the part worth being upfront about, on the page itself too:

  • "Reachable" isn't "healthy". A server answering with a 500 still counts as up, because no-cors hides the status code. That's why the note under the checks on my site says "Probed from your browser" rather than promising anything about health.
  • It's one visitor's view. Their network, their country, their ad blocker. If their connection is bad, everything looks slow.
  • It isn't monitoring. Nobody gets alerted when something goes down. For that you need a real uptime monitor.

The component can also show real 30-day uptime: UptimeRobot offers a read-only API key that's meant to be public, and its getMonitors endpoint returns uptime ratios. I built that path but haven't switched it on yet, so on my site you'll currently see the browser checks only.

Try it

Open mohitpatel.org: the console in the hero runs these checks live from your browser, and the refresh button runs them again. The code is in the site's repo (src/lib/status.js).

If you've found other headers that trip up browser-side checks, I'd like to hear about them in the comments.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.