Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expanded on No Router Instance #21248

Merged
merged 6 commits into from Jan 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion errors/no-router-instance.md
Expand Up @@ -2,8 +2,13 @@

#### Why This Error Occurred

Next.js is universal, which means it executes code first server-side with NodeJS, then on the client-side, `window` object is not defined in NodeJS, so some methods are not supported or availale at build time.
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
During SSR you might have tried to access a router method `push`, `replace`, `back`, which is not supported.

#### Possible Ways to Fix It

Move any calls to router methods to `componentDidMount` or add a check such as `typeof window !== 'undefined'` before calling the methods
In a class Component, move any calls to router methods to `componentDidMount` lifecycle method or add a check such as `typeof window !== 'undefined'` before calling the methods

In a functional Component you can move the code into the `useEffect` hook. checking `typeof window !== 'undefined'` also works in a functional component.

This way the calls to the router methods are only executed on the client (in the browser), thus ensuring access to the `window` object.