Your website is a distributed system (and your theme switcher proves it)
Here's a scenario. You run a blog with a light/dark theme switch. A reader opens three of your articles, each in its own tab. In the first tab, they switch to dark mode. Then they click over to the second tab. What do t
Here's a scenario. You run a blog with a light/dark theme switch. A reader opens three of your articles, each in its own tab. In the first tab, they switch to dark mode. Then they click over to the second tab.
What do they expect to see? Dark, obviously. What do they actually see? White.
Nothing is broken in the usual sense. There's no exception and no failed request. And yet the experience is wrong. Let's see why.
Why it happens
Most theme switchers are built in one of two ways:
- In-memory state (component state, a store). The theme lives in that tab's JavaScript heap. Other tabs don't know it exists, and it's gone on reload.
- A cookie or localStorage. The value persists, but the page reads it once, while rendering. A tab that was already open never reads it again.
With the cookie, a reload fixes it. That's exactly why this bug survives: it's easy to shrug off. Until you start looking at your website differently.
Every tab is a replica
A desktop app usually has one process and one copy of its state. A web app has as many copies as the user has open tabs. Each tab:
- runs its own JavaScript runtime with its own in-memory state,
- was loaded at a different time, possibly with a different version of your code,
- reads shared storage only when it decides to.
That's a distributed system: many replicas of the same state, no automatic propagation, and any of them can be stale. The theme bug is just a stale replica.
It helps to separate two levels:
- Tabs in one browser share storage (cookies, localStorage, IndexedDB) but not memory. You can sync them entirely on the client.
- Different browsers and devices share nothing. Their only common ground is your backend.
The same user can easily be on both levels at once: three browsers, a dozen tabs in each.
Where it actually hurts
A wrong theme is harmless. Other stale replicas aren't:
- Logout. The user logs out in one tab. Another tab still looks logged in, and the first action there fails with a 401. If that action was submitting a long form, the user just lost their work.
- Carts and drafts. The same draft is edited in two tabs. Which save wins? That's a write conflict, classic distributed-systems territory.
- Version skew after a deploy. A tab opened yesterday runs yesterday's bundle, while your backend and lazy-loaded chunks are already on today's version. You get missing chunks, broken API contracts and feature flags that disagree with the server.
Fixing the theme switcher
The good news: for the theme, the fix is a few lines. Here are three options, from real-time to good enough.
Option 1: localStorage and the storage event. The event fires in every other tab of the same origin, not in the one that wrote the value.
function setTheme(theme) {
localStorage.setItem('theme', theme);
applyTheme(theme);
}
window.addEventListener('storage', (event) => {
if (event.key === 'theme') applyTheme(event.newValue);
});
Option 2: BroadcastChannel. An explicit message bus between tabs. It works with any storage, including a cookie the server reads during render.
const prefs = new BroadcastChannel('prefs');
function setTheme(theme) {
document.cookie = `theme=${theme}; path=/; max-age=31536000`;
applyTheme(theme);
prefs.postMessage({ theme }); // delivered to other tabs, not this one
}
prefs.onmessage = ({ data }) => applyTheme(data.theme);
Option 3: re-read the source of truth when the tab becomes visible. Not real-time, but it runs exactly when the user would notice.
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
applyTheme(readThemeCookie());
}
});
For heavier cases, look at the Cookie Store API (it emits change events), a SharedWorker as a single owner of shared state, and the Web Locks API to elect one leader tab, for example so only one tab refreshes the auth token.
Questions to ask about your app
I don't expect you to rush off and rewrite your theme switcher. But it's worth going through your app's state with these questions:
- Where does each piece of state live, and what is its source of truth: memory, browser storage or the backend?
- How does a change in one replica reach the others? Does it at all?
- How stale can a replica get before it hurts the user?
- What happens when two replicas change the same thing?
If the answer is "we read it once on load", you have a theme-switcher bug somewhere. Possibly a much more expensive one.
Further reading
Sync engines (Replicache and Zero, Powersync, Electric, the engine behind Linear) take this idea to its conclusion: local-first state, continuously synced with the backend. Many of them also handle cross-tab sync for you, usually through BroadcastChannel or a SharedWorker. Even if you never adopt one, learning how they work is the best way to start thinking about your frontend as the distributed system it already is.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.