Sovereign-Computing.com

Reclaim your digital life. Own your sovereignty.

A dusty box pulled from a shelf while a clean identical box sits behind it, a handprint wiped in the dust

Cached JavaScript Looks Exactly Like a Broken Backend

The page had been working for an hour, and then it stopped. A browser can show you a fault that lives inside the browser, and the message it shows you will point at your server instead. That is what happened here, and it cost me most of an afternoon.

The backend was answering correctly the whole time. I know that because I spent that hour reading its logs, which is a fine way to spend an afternoon if your goal is to learn nothing at all.

What went wrong is ordinary. The browser kept an old copy of the page in its cache, so it ran last week’s code instead of the code I had just put there. That old copy spoke the previous version of the protocol. The server had moved on to the new one, so the handshake failed. The failure reached me as one blunt sentence in the interface. It could not reach the translator.

Why the message points the wrong way

A WebSocket that fails during setup cannot tell you why. The browser only knows that a connection failed. The specific reason, such as a rejected subprotocol or a path that no longer exists, never reaches your code. So the only explanation left is the generic one, and the generic one always blames the network.

Meanwhile every layer behind the page is healthy, and each one says so in its own quiet way. The reverse proxy is up, and the service is listening. A synthetic client on the same machine finishes a round trip in about 3 seconds.

Here is what each client reported:

  • the phone’s browser: “cannot reach the translator”
  • the same URL in a private tab: works
  • a scripted client on loopback: works, 3.28 s round trip

2 of those clients worked, and that ratio is the shape of a problem on the client side. I read it as a problem on the server side anyway, because the client that failed was the one with a person attached to it. A person makes a convincing witness and a poor one at the same time.

The fix, and the habit behind it

While a page is under development, serve the HTML with a directive that forbids reuse. In nginx, it looks like this.

location ~* \.html?$ {
    add_header Cache-Control "no-store, must-revalidate" always;
    proxy_pass http://<backend>;
}

That is the whole fix, and it is 4 lines of config.

A version string in the URL helps while you are testing by hand. It does not survive someone typing the bare address into a phone, and it does nothing for a tab that was restored from 3 days ago. The header is the part that holds, because the server is telling the browser not to keep a copy in the first place. The browser will ignore a surprising amount of what you ask it to do. This is one of the few requests it tends to honour.

The habit matters more than the header. Prove what the client is actually running before you read a server log. One line in the browser console does it, and that line separates “the software is broken” from “you are looking at software from last Tuesday”.

A second way the client lies

The same project has a test harness that pushes a recorded clip down the socket. It ran at roughly 2 per cent of real time, which made everything downstream look stalled, so it read as a fault in the pipeline.

The cause was not the pipeline. A background tab slows its own timers down, so a loop paced with setInterval fires far less often than it asks to. Nothing was broken, because the clock the program was using had been quietly slowed and the program had no way to notice.

The browser had not broken my code. It had decided that my code did not need full speed while nobody was looking at it, which is a fair call and a miserable thing to debug.

So the fix was to send the whole buffer in one pass instead of pacing it out. That is the right shape for a test anyway, because a test paced by a timer is really measuring the timer.

The lesson is the same one in a different costume. When a browser is in the loop, its own state is a suspect before the server is. Cached files, slowed timers and restored tabs all produce the same confident, wrong message.

How I test a browser app now

Three checks, in this order, and I do all of them before I open the server’s logs.

  1. Hard reload, or a private window, first.
  2. Confirm the version string the client reports matches the build I just deployed.
  3. Only then read the proxy log and the service log.

The private window matters because it rules out more than a cached file. It also rules out a restored tab that is still running an old service worker. A progressive web app makes this worse rather than better, because it can keep serving a cached shell from disk long after the network is fine. The only cheap honesty is to show the build identifier somewhere visible on the screen.

If the build number is not on the screen, you are guessing about the one thing you could have checked in 5 seconds.

The cost of getting the order wrong

An afternoon, twice, on the same project. The first time I suspected the proxy, and the second time I suspected the service. Both were answering correctly throughout, and each search ended the same way, with a private window that worked on the first try.

That is the part worth writing down, because it is not a knowledge gap. I know what a cache is. What I lacked was a fixed order of operations, so under pressure I reached for the most complicated explanation first. A complicated theory feels like real engineering, which is why it is the first one I reach for and the last one I should.

Why this matters for self-hosting

When you run the whole stack, there is no vendor to blame and no status page to check. That is the value and the trap at once. You own every layer, so the fault can be in any of them. The layer with the user attached is the one whose error message you will believe first.

It is the same shape as a gate that protects the address but not the program. The control is real, the assumption behind it is wrong, and the symptom turns up somewhere else entirely. Both times, the thing I trusted did exactly what I told it to do, which is the least useful kind of correct.

WordPress Appliance - Powered by TurnKey Linux