SolidStart SSR: window API errors after production build
0 reputation · 29 Nov 2022, 14:15 UTC
Goal
Ensure that a SolidStart application renders correctly in production when server‑side rendering (SSR) is enabled, while still functioning during local development.
Context
During local development, Vite’s dev server polyfills the window global, so code that references window outside of a useClient guard runs without error. In a production build, the same code is executed on the Node server during the initial render, resulting in a ReferenceError: window is not defined. The difference is not apparent until the production bundle is served, and hydration mismatches can surface only after the page loads.
Unresolved Decision
There is no definitive guidance on whether to guard every browser‑only API with useClient, adjust the Vite SSR configuration to provide a polyfill, or rely on tree‑shaking to remove unused effects that depend on window. Each approach has trade‑offs for build size, runtime performance, and developer ergonomics.
Specific Questions
- Should all
windowreferences be wrapped withuseClient(or an equivalentisClientcheck) to guarantee SSR safety? - Does Vite’s
ssr.noExternalor a customdefineconfiguration provide a reliable way to polyfillwindowfor production SSR? - How does tree‑shaking in the production build affect reactive effects that depend on
window, and can those effects be preserved without manual guarding?