"ReferenceError: navigator is not defined" error when page is first time accessed in Next.js
See original GitHub issueIssue and Steps to Reproduce
Setup _app.tsx
in Next.js like below:
function CustomApp({
Component,
pageProps
}: AppPropsWithLayout): JSX.Element {
const router = useRouter()
const withCustomHistory = () => {
return {
replaceState: (url: string) => {
router.replace({
pathname: url,
}).then(() => {
window.dispatchEvent(new Event('popstate'));
});
}
};
};
const withLayout = Component.layout ?? (page => page);
return (
<OidcProvider withCustomHistory={withCustomHistory} configuration={{
authority: '...',
client_id: '...',
redirect_uri: '...',
silent_redirect_uri: '...',
scope: 'openid profile email offline_access',
}}>
{withLayout(<Component {...pageProps} />)}
</OidcProvider>
);
}
Run the page in browser.
Versions
6.5.5
Screenshots
Expected
No errors
Actual
[dev:nextjs] error - unhandledRejection: ReferenceError: navigator is not defined
[dev:nextjs] at D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:111:17
[dev:nextjs] at step (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:33:23)
[dev:nextjs] at Object.next (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:14:53)
[dev:nextjs] at D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:8:71
[dev:nextjs] at new Promise (<anonymous>)
[dev:nextjs] at __awaiter (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:4:12)
[dev:nextjs] at initWorkerAsync (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\initWorker.js:106:87)
[dev:nextjs] at Oidc.<anonymous> (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\oidc.js:617:87)
[dev:nextjs] at step (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\oidc.js:59:23)
[dev:nextjs] at Object.next (D:\VS\accelist-nextjs-starter\node_modules\@axa-fr\react-oidc\dist\vanilla\oidc.js:40:53)
Additional Details
Suspected that this function errored when the page is first accessed on server-side https://github.com/AxaGuilDEv/react-oidc/blob/master/packages/react/src/oidc/vanilla/initWorker.ts#L69
navigator
is only available at browser and not during SSR.
- Installed packages:
Next.js 12.3
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
NextJS build error - ReferenceError: navigator is not defined
From reading around I gather that this issue occurs with NextJS when trying to build a project on the server side instead of...
Read more >Getting "ReferenceError: navigator is not defined" in Next.js app
Getting error - ReferenceError: navigator is not defined error from Plasmic in development and production for our NextJS app
Read more >ReferenceError: navigator is not defined · Issue #77 - GitHub
It seems the trick from above no longer works in NextJs. This is a terrible hack but the following works for now (if...
Read more >How to solve Next.js window is not defined
ReferenceError : window is not defined is a pretty common error you may run into when using Next.js for the first time but...
Read more >next js navigator is not defined - You.com | The AI Search ...
Reference Error :Navigator not defined with nextjs. Ask Question Asked 2 years, 3 months ago. Modified 5 months ago. Viewed 13k times ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Last version 6.5.7 seem to resolve the problem (I’am not sure @ryanelian ).
I can reproduce the error thank you very much 😃