The session token bug that silently dropped browser logs after 6 hours

Some bugs only show up when someone leaves a tab open for hours, which means they’re almost guaranteed to not show up in your own testing, your team’s testing, or your CI — and guaranteed to show up in production, on exactly the tab your most engaged user forgot to close. This is the story of one of those, in the relay authentication for nextjs-logger.
How the browser side authenticates without holding a secret
This package lets you call log.info(...) from anywhere in a Next.js app, including the browser, and the logs end up written server-side — to your terminal, a file, or wherever you’ve configured a transport. That means there’s a network hop from browser to server on every log call, and that hop needs to be authenticated, or anyone who opens your site’s dev tools could POST arbitrary junk to your logging endpoint forever.
The design here is a bearer session-token scheme, not a per-payload signature. That distinction matters more than it sounds like it should. A per-payload HMAC would mean the browser computes a signature over each batch of log entries using a shared secret — except the browser can never safely hold that secret, so this doesn’t actually work; it was, in fact, a real bug in an earlier version of this package, where the scheme required a client-computed HMAC the client could never produce, and every relay request silently failed verification as a result.
The scheme that replaced it: <LoggerProvider>, an async Server Component mounted once near your app’s root, mints a signed session token server-side — sign(secret, "session." + issuedAt), computed with the Web Crypto API so the exact same code path works on both Node.js and Edge Runtime — and hands that token to the client as a prop. The browser holds onto that token and resends it, unchanged, with every relay call for the rest of the page’s lifetime: the primary fetch path, retries, and even the sendBeacon call fired on tab close, which reuses the identical token rather than needing its own scheme.
This authenticates “a session the server minted made this request,” not “these exact bytes are untampered.” And critically, the token itself is not a secret — it’s minted server-side and embedded straight into the page HTML, so anyone who can load the page can read it and technically reuse it. That’s inherent to the design, not an oversight: the browser has to authenticate somehow without ever holding the signing key, and a readable-but-verifiable bearer token is how you get that. It’s why the relay endpoint also has a strict rate limit sitting in front of it — the token bounds who can write, not how much, and the rate limit is what bounds the second part.
The bug: a token that’s correct once and never rechecked
Here’s what I initially shipped, described in plain terms rather than code: the token is minted exactly once, when <LoggerProvider> renders on the server for that page load. The client holds that one token for as long as the tab stays open. There’s a SESSION_MAX_AGE_MS window — six hours — past which the server considers a token expired and rejects it.
For the overwhelming majority of page loads, this is completely fine, because most tabs don’t stay open for six hours. But some do — a dashboard someone leaves open all day, an admin tool pinned in a tab that never gets closed, a long-running internal app. And for those tabs, here’s exactly what happened: at the six-hour mark, every single relay request started getting rejected. Not with a visible error the user would notice — this is a logging pipeline, so its own failure mode is silence. The client-side transport got a 401, had nowhere useful to surface that to a real user, and from that point forward, every browser log from that tab just… stopped arriving server-side. No crash, no console error visible to anyone but a developer who happened to be watching, just a growing gap in the log stream with no signal that the gap existed.
That’s about as bad as a logging library’s own reliability story can get: the very telemetry you’d use to notice something’s wrong is the thing that silently went dark.
The fix: sessions roll over instead of expiring out from under you
The fix isn’t “make the window longer” — that just moves the same cliff further out and makes it rarer, which if anything makes it harder to catch, since it becomes an even longer-lived tab that’s needed to reproduce it. The actual fix is to stop treating six hours as a hard wall and instead treat it as a point where the server proactively hands the client a fresh token, before the old one becomes a problem.
The mechanism: shouldRenewSession() checks the token’s age against the halfway point of the window — three hours in, not six. Past that point, the relay route handler’s response includes a freshly minted session alongside the normal response body, and the client-side queue swaps it in transparently. The tab never has to do anything special to trigger this — it’s riding along on requests the tab was already making anyway, since a browser logging enough to matter is, definitionally, making relay requests regularly.
Token minted at page load (t=0)
│
├─ t < 3h ───────────── every relay call: verify only
│
├─ t ≥ 3h (halfway) ─── every relay call: verify, AND
│ response includes a fresh session
│ client swaps it in silently
│
└─ t ≥ 6h, no renewal reached the tab somehow?
└─ verification fails → 401
└─ transport treats 401 as "try the Server Action fallback"
which needs no token of ours, and itself
returns a freshly minted session on successThis grants the client nothing it didn’t already have a right to. A renewed token is only ever handed to a tab that already presented a currently-valid token — so there’s no new public mint endpoint to worry about, and no way to bootstrap a valid session from nothing. It’s strictly “extend a session that’s already proven itself,” which is a much smaller trust boundary to reason about than “let anyone request a token.”
What happens to a tab that missed the window entirely
There’s still a path where a tab could end up past the hard six-hour expiry with no renewal ever having reached it — say the tab went into the background for the entire window and browser throttling meant it made zero relay requests during the renewal period, so it never got the chance to pick up a fresh token. For that case, the fallback isn’t “log an error and give up.” A 401 on the primary route-handler path is specifically interpreted by the transport as “try the Server Action fallback instead,” and the Server Action path needs no token of its own to succeed — it mints a fresh session on success and hands it back the same way. So a tab that fell all the way off the renewal cliff heals itself back onto the normal path on its very next log call, rather than staying broken until the user refreshes the page.
Why I think this class of bug deserves more attention than it usually gets
The uncomfortable thing about this bug is that it would have passed essentially every form of testing I’d normally run. Unit tests around token minting and verification: pass, because they test a single mint-then-verify cycle, which is exactly the case that was never broken. Integration tests: pass, for the same reason — nothing in a normal CI run keeps a browser tab open for six hours. Manual QA: pass, because nobody manually tests a six-hour window by literally waiting six hours.
The only way I actually caught it was reasoning backward from the design: “this token has an expiry, and nothing currently refreshes it — what happens to a session that outlives that window?” That’s a question worth asking explicitly for any time-bounded credential you ship, independent of whether you have a failing test pointing at it, because the failure mode for a credential that expires with no renewal path is never a loud one. It’s a for loop that keeps running successfully for the first N hours and then goes quiet, and “goes quiet” is exactly the kind of bug that doesn’t page anyone.
If you’re issuing any kind of time-bounded token to a long-lived client — a browser tab, a mobile app session, a long-running worker — it’s worth writing down explicitly what happens on the other side of the expiry window, before someone’s six-hour-old tab finds out for you.
Why I didn’t just make renewal happen on a timer instead
One alternative design I considered and rejected: have the client run its own timer — setTimeout(renewSession, THREE_HOURS) — and proactively request a fresh token when it fires, independent of whatever else the tab happens to be doing. I didn’t go this way for a fairly simple reason: it adds a code path whose only job is to exist for hours before firing once, in a browser tab that could be backgrounded, throttled, or have its timers deprioritized by the browser the entire time it’s supposed to be counting down — background tab timer throttling is a real, well-documented browser behavior, not an edge case. A renewal path that depends on a long-lived timer surviving exactly the conditions (a backgrounded, inactive tab) where you most need it to work reliably is a fragile design for the one thing it exists to do.
Piggybacking the renewal on requests the tab is already making sidesteps that problem entirely: it doesn’t need its own timer, doesn’t need to survive being backgrounded, and doesn’t introduce a new thing that can silently fail to fire. It works precisely because a tab logging enough to care about token freshness is, by definition, a tab still making relay requests — and if a tab genuinely goes quiet for the entire six-hour window with zero log activity, there’s no renewal to miss in the first place, because there was nothing worth authenticating either. The self-healing fallback through the Server Action path exists specifically to cover the remaining edge: a tab that goes quiet for most of the window and then resumes logging right as the hard expiry hits, with no renewal request having landed in between.