Answer
As of the latest WebContainers implementation, there is no way to keep a Node.js process running after the browser tab that hosts the StackBlitz editor is backgrounded or closed. All processes are tied to the tab’s lifecycle, and no daemon mode, experimental flag, or environment variable exists to change this behavior.
Confirmed facts
- WebContainers run the Node.js runtime inside the main thread of the tab; when the tab is suspended or closed, the runtime is terminated.
- There is no documented API for headless or persistent execution, and the StackBlitz team has only acknowledged the limitation without publishing a roadmap.
- Browser throttling of background tabs further reduces any chance of keeping a WebSocket or server alive.
- State can be persisted across sessions via the filesystem abstraction (files written to /tmp or the project directory) or via client‑side storage such as IndexedDB, but this only preserves data, not active processes or open sockets.
- The only reliable method to maintain a network presence independent of the tab is to run an external service (e.g., on a remote VPS, Glitch, Railway, etc.) and connect to it from the WebContainer.
Likely explanation / uncertainty
The WebContainers architecture is deliberately tightly coupled to the tab to guarantee a deterministic, self‑contained sandbox. Introducing a true background daemon would require a separate worker thread or a service‑worker‑based shim, which the current design does not support. No experimental flags have been announced, and any future feature would likely be opt‑in and documented in the WebContainer release notes.
Practical steps for persistence today
- Identify the long‑running component you need (e.g., a local SQLite database, a WebSocket server).
- Deploy that component to an external host that you control (a cheap VPS, a PaaS, or another StackBlitz project configured as a proxy).
- From the WebContainer, connect to the external service using standard protocols (TCP, HTTP, WebSocket). Store any necessary connection details in
process.env or a small JSON file.
- If you need to retain client‑side UI state across reloads, write that data to IndexedDB via the
idb library or the localForage wrapper; on re‑open, read it back and re‑establish the connection to the external service.
- When you close the tab, the external service continues running; reopening the StackBlitz project lets you resume work without restarting the daemon.
If you have a specific external endpoint you are considering, please share its address or protocol so I can verify whether any known firewall or CORS restrictions might affect the connection.