Jack
Jack

Reputation: 13

'ReferenceError: window is not defined' still preventing NextJS build with SSR disabled?

Recently I began transitioning my personal website from CRA to NextJS and have largely had no issues, but am a bit stuck on this error.

My site has a dynamic background that relies heavily on client-side window properties; given NextJS' SSR defaults I've isolated all references to window size in a single component, and dynamically import it with SSR disabled. I only reference the component in a single place, imported using:

const DynamicBG = dynamic(() => import('./landingSections/FramerBG'), {
ssr: false,
})

The application behaves as expected when ran through npm run dev but refuses to compile through npx next build due to the references to client window size in my code, despite those files being imported with SSR disabled. Am I supposed to differentiate client-rendered components in some way outside dynamic importing?

Upvotes: 1

Views: 228

Answers (1)

R1's
R1's

Reputation: 41

I had the same problem. Resolved by moving out my component from the pages/ folder. All the components of this folder are being built my next with ssr on even if you try to disable it.

Upvotes: 0

Related Questions