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.

useSession() return null after Internationalized Routing, unless I refresh the page.

See original GitHub issue

Description 🐜

session is null after Internationalized Routing. Then if I refresh the current page, the session will return without problem;

Is this a bug in your own project?

Yes

How to reproduce ☕️


import Link from 'next/link'

export default function I18nLink(props) {
  return (
    <Link href="/another" locale="fr">
      <a>To /fr/another</a>
    </Link>
  )
}

In one page or component I have the following code:


const ExampleComponent = () => {
   const [session, loading] = useSession();
   return (
       {  session ?  <div> session exist </div> :   <div> session does not exist </div> }
   )

In a page

 const index =  () => {
   return (
      <div>
        <I18nLink />
        <ExampleComponent />
      </div>
    )
}
 export default index.

When I was already logined. And then click the I18nLink, the page will transit to the new locate without problem, but the session will be missing. Hence <div> session does not exist </div> displayed.

My current workaround:

instead of using next router, just simply use window.location = \newLocateUrl to hard refresh the page.

Note:

I was using useSession() to conditionally render certain components. If this wasn’t a right way to do so, just let me know.

Screenshots / Logs 📽

No response

Environment 🖥

Ubuntu Windows 10

Contributing 🙌🏽

Yes, I am willing to help solve this bug in a PR

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

10reactions
aboveyunhaicommented, Aug 8, 2021

This may be an issue with the next-i18next package: isaachinman/next-i18next#1075

Can you confirm if this tracks with what you’re both seeing?

Yes, can confirm it’s a next-18next or probably i18next issue. And the solution is what @thomvand26 said.

In terms of code for next-18next, the fix would be something like the following:


const MyApp = ({ Component, pageProps }: AppProps) => {
  return (
   //...Component
  );
};

const AppWithI18n = appWithTranslation(MyApp);

const AppWithAuth = (props: AppProps) => (
  <Provider session={props.pageProps.session} >
    <AppWithI18n {...props} />
  </Provider>
)

export default AppWithAuth;


0reactions
nguyentiendancommented, Jan 19, 2022

It work for me. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next Auth useSession returning undefined on initial page load
I have an issue where my Next Auth useSession function is returning undefined on intial page load but works when hitting refresh on...
Read more >
Advanced Features: Internationalized Routing - Next.js
js has built-in support for internationalized (i18n) routing since v10.0.0 . You can provide a list of locales, the default locale, and domain-specific...
Read more >
CodeIgniter - Session Management - Tutorialspoint
This function will return NULL if the data you are trying to access is not available. $name = $this->session->userdata('name');. Example. Create a controller ......
Read more >
Developing Python Web Applications with Flask
It routes the URL '/' request to main() which returns the hello-world message. ... I will show you how to run a Flask...
Read more >
admin-bro-expressjs/plugin.js
Router() ; router.use(formidableMiddleware(formidableOptions)); routes. ... it causes; * logging out after every page refresh (if you use nodemon).
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