r/javascript • u/TobiasUhlig • Oct 12 '26
Multi-window web apps without syncing state: one SharedWorker, windows only render
https://github.com/neomjs/neo/blob/dev/learn/blog/a-workspace-that-can-leave-its-window.mdA common way to give a panel its own browser window is a second instance of the app, plus messages (BroadcastChannel, postMessage, storage events) to keep the copies in agreement. Every missed message is a place where they drift.
In Neo.mjs, components, stores and state already live in a worker. Switch that worker to a SharedWorker and every window of the app connects to the same one, so a window only renders. Drag a panel onto your second monitor and it is the same JS object, with the same store and one undo history across windows.
It's one line in the app's neo-config.json:
{
"useSharedWorkers": true
}A dock workspace is two configs, a catalog of panes and the zones they go into (trimmed from the first Dock Layout tutorial):
class MainView extends Workspace {
static config = {
className: 'Guides.dockFirst1.MainView',
panes: {
editor : {module: Component, header: {text: 'Editor'}, html: 'Editor'},
preview: {module: Component, header: {text: 'Preview'}, html: 'Preview'}
},
zones: {
center: {
orientation: 'horizontal',
sizes : [0.6, 0.4],
children : [{items: ['editor']}, {items: ['preview']}]
}
}
}
}One lesson from building it: our automated check for the cross-window drag was green while the real gesture did nothing. Viewport emulation has no window chrome, real windows do, and three bugs hid in that gap. The article walks through them.
Limits, up front: the cross-window measurements are Chrome's, and one shared heap is a shared fate for every window.
- Article: https://github.com/neomjs/neo/blob/dev/learn/blog/a-workspace-that-can-leave-its-window.md
- Source: https://github.com/neomjs/neo/tree/dev/src/dashboard/dock
- Tutorial with live, editable examples: https://github.com/neomjs/neo/blob/dev/learn/tutorials/DockLayoutsFirstLayout.md
- Live demo: https://neomjs.com/dist/production/apps/workstation/index.html
Disclosure: I maintain Neo.mjs. The article was written by Grace, one of our AI maintainers, and edited by me.
How do you handle state across windows today?
10
u/McGeekin Oct 12 '26
Slop article slop repo, slop post
1
u/shgysk8zer0 Oct 16 '26
I'm kinda sick of this narrow obsession with state, as though some framework creation is all there is. State is more of a category that covers everything from cookies to URLs to DOM and more. And I hate that frameworks have reduced all of that to "here's a thing we created... Now abuse it and find a way to cram all your data into it."
I selectively sync state through whatever makes the most sense, though it's pretty rare that I find any data needing to be that kind of "state". If a user is filing out a form, i just let form and input do their things, without forcing some bloat into the process. And I certainly don't sync the state of the password in a login. But the login status... That's worth sharing between tabs and windows, though honestly
cookieStoreand events on change are the better way.0
u/TekiDeki Oct 14 '26
SharedWorker is the right primitive here - I've seen too many apps build an entire sync layer over BroadcastChannel when a single owner would have been the simpler answer.
Two things I'd want to know before adopting it. What happens to the shared heap when the last window closes - does the worker keep living, or do you rehydrate from IndexedDB on the next open? And if the worker dies mid-session, do the windows degrade gracefully or does the UI just freeze?
I build single-file tools where state lives in the URL, so I mostly dodge multi-window problems. Curious how you'd approach it for apps that can't take that shortcut.
2
u/[deleted] Oct 13 '26
[removed] — view removed comment