#Enabling SSR breaks my website

19 messages · Page 1 of 1 (latest)

noble chasm
#

Heyo, I found out I've been developing with ssr: false the whole time, and I wanted to deploy my website completely statically. Turning SSR on breaks my website and I don't know where to start fixing it.

at <App>
[nitro] [dev] [unhandledRejection] ReferenceError: window is not defined

Using RC-13 nuxt, pinia, all data is gathered through graphql.

Also, how will nuxt know to also statically grab things that only get called for once user clicks a button? (For example music file when user clicks play).

dull gorge
# noble chasm

you should perform any stuff related to DOM inside the onMounted hook, which is called when the page is rendered on the user's browser, so the window would actually exist

noble chasm
#

I am using on mounted

fallow vessel
#

You have some of plugins/code that uses window. which is not available on server

#

You have to wrap that code in

if(!import.meta.env.SSR){window...}
#

if it is plugin u have to name plugin as nameofplugin.client.ts if it is template then u have to wrap code in <client-only></client-only> and third thing if it is a whole component u can name ur component as componentName.client.vue
Hope it helps

noble chasm
#

uuf

#

AHA! I was getting

if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
    musicLibrary.is_darkmode = true
}

in app.vue

#

Where should I move this part of the code then?

dull gorge
#

inside onMounted 🙂

noble chasm
#

Ah perfect

#

One more thing, know what this could be?

dull gorge
#

not sure about that, can you try restarting your dev server?

#

actually, your musicLibrary composable, it is possible you are trying to access something outside the lifecycle

noble chasm
#

it still appears after reset

#

Yeah, I query graphql stuff in there, but don't know what that error exactly means

dull gorge
noble chasm
#

Mine is inside an async function already, and honestly I don't see exactly what breaks as everything works as it should ^^'