question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"ReferenceError: navigator is not defined" error when page is first time accessed in Next.js

See original GitHub issue

Issue 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

image

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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
guillaume-chervetcommented, Sep 13, 2022

Last version 6.5.7 seem to resolve the problem (I’am not sure @ryanelian ). image

0reactions
guillaume-chervetcommented, Sep 13, 2022

I can reproduce the error thank you very much 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found