Small things that keep the website standing
Five small pieces of the Lodge Internet website that rarely get noticed, each written because something real went wrong without it, from stale tabs after a deploy to the Android back button.
Lodge Internet · 14 November 2026 · 6 min read
You open the plans page in the evening, pick nothing, and leave the tab. Overnight we deploy. In the morning you tap a plan, and instead of a checkout you get a white page that says "Application error".
Nothing about your account broke. The tab was running yesterday's JavaScript, and yesterday's files no longer exist. This post is about that failure and four others like it: small, unglamorous, and each one worth a few dozen lines.
1. A tab left open across a deploy
The problem. Next.js splits the site into chunks and loads them as you move between pages. A deploy replaces every chunk with new files under new names. A tab opened before the deploy still asks for the old names, the request fails, and Next shows its bare error page.
What we do now. Every page crash lands in components/ErrorScreen.tsx, through app/error.tsx for pages and app/global-error.tsx for the root layout. It asks lib/staleBuild.ts whether this looks like a missing chunk:
return /ChunkLoadError|Loading chunk|Loading CSS chunk|Failed to fetch dynamically imported module|Importing a module script failed/i.test(text);Those are the messages Webpack and the browsers use for a module that failed to load. If it matches, the screen says "Updating…" and reloads, which fetches the new build.
The trade-off. A reload is the right answer to a stale tab and the wrong answer to a real crash, because a real crash comes straight back and reloads again, forever. So reloadOnce() writes a timestamp to the tab's session storage and refuses to reload a second time within a minute. A genuine bug gets one retry, then the normal error screen with "Try again" and "Go home", and a line saying any payment made is safe.
2. A fresh script policy for every request
The problem. A Content Security Policy tells the browser which scripts it may run. The easy policy allows inline scripts, and an easy policy is one an injected <script> walks straight through. We also have one inline script we need: the tiny one that applies a saved Light or Dark choice before the page paints (see Making a website feel like an app).
What we do now. middleware.ts mints a random nonce on every page request and builds the policy around it. script-src has no 'unsafe-inline'. It allows our own origin, scripts carrying this request's nonce, and the payment provider's script by host. An injected inline script or event handler has no nonce, so it does not run.
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
// ...
const response = NextResponse.next({ request: { headers: requestHeaders } });
response.headers.set("Content-Security-Policy", csp);The policy goes on the request as well as the response. That is the signal Next.js reads to stamp the same nonce onto the framework scripts it emits. Our theme script reads the nonce in the client layout and sets it itself. The middleware skips static assets and router prefetches, so we don't mint nonces for HTML that is thrown away.
The trade-off. style-src keeps 'unsafe-inline' on purpose. Next and next/font inject inline styles, and CSS injection can't run code, so locking it down would break the site for little gain. And a per-request nonce means the customer pages are rendered on each request rather than served as prebuilt HTML. Their content comes from the backend at runtime anyway, so the cost is server time, not stale pages. How sign-ins stay out of reach of scripts is its own post: Your sign-in, and why scripts on the page can't touch it.
3. A scroll lock that survives the payment popup
The problem. When a bottom sheet opens, the page behind it shouldn't scroll. The usual trick is document.body.style.overflow = "hidden". But checkout opens Paystack's payment popup on top of our checkout sheet, and Paystack writes body.style.overflow itself when its popup opens and closes. Its close would undo our lock, and the page would scroll under a sheet that was still open.
What we do now. Sheet.tsx never touches body.style. It adds a class to <html>, and globals.css turns that class into overflow: hidden on both elements. The lock is counted, because sheets stack (a confirmation over the checkout, for example):
let lockCount = 0;
function lockScroll() {
if (lockCount++ === 0) document.documentElement.classList.add("ui-scroll-locked");
}
function unlockScroll() {
lockCount = Math.max(0, lockCount - 1);
if (lockCount === 0) document.documentElement.classList.remove("ui-scroll-locked");
}The same caution shapes focus. A sheet moves focus into itself only when it opens, and traps Tab with a keydown listener on its own panel. It never sets inert or aria-hidden on the body, so focus inside Paystack's frame is never pulled back to us.
4. The Android back button closes sheets
The problem. On Android, the back button is how people dismiss things. A sheet is not a page, so pressing back while one is open leaves the whole page, and with it the plan you had picked.
What we do now. A sheet given the history prop pushes a history entry for the same URL when it opens. Back pops that entry, the sheet hears popstate, and closes. Three details make it behave:
- Only the top sheet reacts, using a small stack of open sheets.
- A sheet that can't be dismissed right now, like checkout while a payment is in progress, pushes its entry straight back and stays put.
- Closed by its own button instead, a sheet steps back through its entry, but only if that entry is still the current one. If the page has moved on, history is left alone.
That last rule shapes WelcomeSheet.tsx. Its "Set up my wallet" button closes the sheet and goes to the Account page. The close steps back through history, and a step back that lands after the new page has been pushed cancels it. So the button waits for the step back to finish before it moves.
5. Admin sessions that end out loud
The problem. Our staff sign in to an admin console. Before this, when a session expired, nothing said so. Every admin call failed quietly and each page handled that on its own, mostly by not handling it, so an expired session looked like admin pages where every table was empty. That is a worse failure than an error, because it looks like data.
What we do now. AdminSessionKeeper.tsx does three jobs, and none of them needs the person to think about sessions:
- While the tab is in use, it renews the session shortly before it would lapse, so nobody is signed out mid-task.
- If nobody has touched the tab for a while, it stops renewing and lets the session lapse on its own.
- When the backend refuses a call because the session is over, the admin API client announces it once, for the whole app. The keeper clears the session and goes to the sign-in screen with the reason and the page to come back to.
The reason is stashed in session storage as well as passed along, because the keeper and a page's own sign-in guard can both redirect at almost the same moment, and the sign-in screen should explain itself whichever one wins. A refusal that means "you may not do that" is left alone; the session is fine. More on how admin sign-ins work in No token in JavaScript.
What's still open
The stale-build guard needs session storage. Where the tab's storage is blocked, reloadOnce() can't record that it already tried, and it reloads anyway. That is the one path where a genuine crash could repeat, and we have accepted it rather than refuse the reload for everyone.
The error screen itself is also still on its old fixed colours rather than the design tokens, because it has to render for admin pages and for a crashed root layout as well as for customers. If you add a new overlay, use Sheet with history for anything a person would dismiss with back, and leave body.style alone.
Last checked against the product on 14 November 2026.