Next.js Discord

Discord Forum

Windows is not Defined during prefetch

Unanswered
West African Lion posted this in #help-forum
Open in Discord
Avatar
West African LionOP
Hi, i am having problem with prefetch and prerendering.


Generating static pages (0/10)  [    ]ReferenceError: window is not defined
Error occurred prerendering page "/private/settings". Read more: https://nextjs.org/docs/messages/prerender-error


In my code i have a simple layouts.tsx where i use router.prefetch("/private/settings) from next/navigation.
/private
|-layouts.tsx
|-page.tsx
|-/settings
|--page.tsx

full log in file.
I rly could find an answer on this problem
Image

6 Replies

Avatar
Arinji
Send the settings file
Avatar
Spectacled bear
You want to check if windows exist first, make a check if
typeof window !== 'undefined'
and then use it
Avatar
Arinji
Avatar
Turkish Van
You are most likely trying to use window object without making sure that You are doing that on the client, and client only.

You might want to take a look here: https://www.youtube.com/watch?v=5QP0mvrJkiY&t=1673s

Also, even You are not trying to use it directly, but there might be a package that provides You with custom component that uses the window object.
Avatar
West African LionOP
Thx for all feedback , I fixed this by putting my router.prefetch inside a useEffect
Avatar
Turkish Van
Make sure to mark it as answered, so thread gets closed.