Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shortbrownman's avatar

Inertia.js + SSR

I'm trying out inertia with SSR. I have my project setup pretty much the same as the "Build Modern Laravel Apps Using Inertia.js" series. However, when I try to serve my ssr.js file and access it via browser, the server errors out and falls back to js

ReferenceError: window is not defined
    at normalizeContainer (/home/shortbrownman/sites/reach/node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:1611:5)
    at Object.app.mount (/home/shortbrownman/sites/reach/node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:1556:27)
    at setup (/home/shortbrownman/sites/reach/public/js/ssr.js:2129:176)
    at /home/shortbrownman/sites/reach/node_modules/@inertiajs/inertia-vue3/dist/index.js:1:9464
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Server.<anonymous> (/home/shortbrownman/sites/reach/node_modules/@inertiajs/server/lib/index.js:46:37)

I think it might have something to do with vendor extraction or setting the Layout file asynchronously in my app.js file. I'm not really sure but those are two points that deviate from the Inertia SSR documentation that I can see.

0 likes
3 replies
Sinnbeck's avatar

Can you track down where you use window? As far as I know ssr does not have access to window as it isn't compiling to a browser

JPrudence's avatar

You can put a condition every time you use "window". Or create a computed variable to return ( boolean : window === undefined ) to detect if you are on a browser or in server render mode. SSR can't use "window" because it's only accessible on browser

shortbrownman's avatar

Thanks for all the suggestions, guys. I figured it out. I misconfigured my ssr.js file. I copied my settings from my app.js and left .mount(el) chained to createSSRApp in the setup function. I removed the line and it works, compiles, and delivers the SSR.

3 likes

Please or to participate in this conversation.