A marketing website has a sign-up flow. One Node process serves both the site and its small API. It renders some pages on the server and sends an empty shell for the app-like steps of the sign-up flow.
One afternoon the site answered 500 and 503 on and off, and I told two people: "errors on and off since" a given minute. Most of those errors turned out to be my own. The bug underneath was real; the outage I described was mostly my checks.
Key facts
- One step of the sign-up flow crashed the whole server on every direct load. A second step had the same fault.
- The site answered 500 / 503 on and off for about 16 minutes.
- The deployment log, 9 days deep, held 28 crashes with the same error. 23 were my own loads.
- Once I stopped loading that page, 18 of 18 requests to other paths answered 200.
What I saw
The errors came and went: a page would load, the next request would fail, and seconds later everything worked again.
I had a suspect page, one step of the sign-up flow, so I kept re-loading it to see whether it was back: from a script, from a browser, from another server. In those 16 minutes that was about 12 loads, and a helper process loaded it too. Each failure looked like more proof that the site was down.
The crash
The deployment log had the answer, one line over and over:
ReferenceError: sessionStorage is not defined
It was thrown inside the server render of that step's component. The component read browser storage while rendering. On the server there is none, so the read throws.
Nothing caught the exception. The Node process exited, and the host restarted it 5 to 30 seconds later. Meanwhile every page and every API call answered 500 or 503, not only the broken step.
Why was that page rendered on the server at all? The server decides per address. It renders a page in full when the address is on its "pages for search engines" list, or when it does not know the address. It sends the empty shell for addresses on a second list, "app steps, do not render". The neighbouring steps of the flow were on that second list. This one step was on neither, so it fell through to the full server render.
Why nobody saw it
Inside the site, a button moves you to that step in the browser: a client-side route change, which never asks the server for the page. The step had been in every published build for 25 days. The running process had 49.8 hours of live traffic without a single crash before the first one.
Only a direct load triggers it: a link from another site, a refresh, a bookmark. That day, a new landing page on a different host was published with its sign-up buttons linking straight to that step. Then came my checks.
Like a reload guard that depends on how the page was reached, it worked when you clicked to it and failed when you arrived cold.
My own checks
The deployment log reaches back 9 days. It holds 28 crashes, all with that same error and none of another kind. The first six days have none. 23 of the 28 fall inside my 16 minutes of checking, and they are my own loads.
The other 5 cannot be tied to a request at all. The server writes no request lines, so for those only the minute is known.
The proof was simple. I stopped loading the suspect page and waited. Then I ran three rounds of six other paths: 18 of 18 answered 200.
So my first message described an outage I was causing. I corrected it to both people within minutes. The bug was still real, and my checks did not cause it: any visitor who opened that address directly would take the site down for seconds.
The fix
The fix has three small parts. The step joins its neighbours on the "do not render" list. Both components touch browser storage only in the browser. And when a render throws, the server answers with the empty shell and one log line instead of exiting.
I checked it on a local build of the server. Both addresses answered 200 and the process stayed up. A render error forced on purpose gave the shell plus one log line. A sketch, with made-up names:
// Before: runs on the server too, and throws there.
function PlanStep() {
const saved = sessionStorage.getItem('plan');
return <Form plan={saved} />;
}
// After: read storage only in the browser.
function PlanStep() {
const [saved, setSaved] = useState(null);
useEffect(() => setSaved(sessionStorage.getItem('plan')), []);
return <Form plan={saved} />;
}
// Server: before, the throw ended the process.
app.get('*', (req, res) => {
let html;
try {
html = render(req.url);
} catch (e) {
log('render failed', req.url, e.message);
html = shell;
}
res.send(html);
});
The checklist
Anyone running a server-rendered site can do these tomorrow:
- Guard browser-only reads at render time. Anything that reads
window,document,localStorageorsessionStoragewhile rendering must be guarded or moved into an effect. Test each route with a direct load, with curl or a fresh tab, not only by clicking through. - Catch render errors. One uncaught render error must not end the process. Catch it in the render handler, send the shell or an error page, and log one line.
- Know your default for unknown routes. With a route allow-list and a "render everything unknown" default, every new route is server-rendered until someone remembers the list. Say which default you have, and prefer the safe one: unknown means shell.
- Log requests. At least the path and the status. Without request lines, the five earlier crashes could only be guessed at by the clock.
- Open outbound links once. Before publishing a page whose buttons link into another site, open each link once in a fresh tab.
- Count your own requests. When probing a failure, count your own requests and compare the failure times with them before you report.
The lesson in one line: when something fails while you test it, your own requests are the first suspect, so stop, wait one restart and sample without the suspect address before you tell anyone it is down. And a page that works when you click to it is not a page that works when you load it.
Common questions
Why did every page fail, not only the broken step?
One Node process serves the whole site and its API. When that process exits, nothing answers until the host restarts it, so every page and every API call gets 500 or 503 for those seconds.
Why not just keep the list of app steps up to date?
Lists get forgotten. Here an address on neither list was rendered in full, so every new step was unsafe until someone remembered the list. With unknown addresses getting the shell, a forgotten step fails safe.
Melta is free; get it on the App Store or Google Play.