tolgee-js icon indicating copy to clipboard operation
tolgee-js copied to clipboard

How to Prevent Page Refresh on Language Switch in Next.js App Router

Open tuzkiiiii233 opened this issue 11 months ago • 3 comments

When switching languages, the entire page refreshes. This happens in Next.js with the app router, whether using next-intl or directly storing the language in a cookie. This issue didn’t occur when using the page router before. How can I prevent the global refresh when switching languages in the app router?

tuzkiiiii233 avatar Jan 17 '25 08:01 tuzkiiiii233

Hey, could you go into more detail with this issue? Our testapp (https://github.com/tolgee/next-app-example) is using a server action, which invalidates cookie and that forces all server components to re-rendered. However there is no hard refresh (in traditional sense). I believe this is correct behavior.

stepan662 avatar Jan 17 '25 09:01 stepan662

I just ran next-app-example and next-app-intl-example locally, and added a client component Test in src/app/[locale]/page.tsx.

"use client"

import { memo } from "react";

const Test = () => {
    console.log("test");

    return (<div>test</div>)
}

export default memo(Test)

When switching languages, I noticed that Test keeps refreshing as well. Is this the correct behavior? Is there any way to avoid the refresh of the Test component? In my actual project, this refresh causes all requests on the page to be re-sent when switching languages, essentially re-rendering the page, even though the data is the same. This results in a poor user experience. If there’s any solution, please let me know. Thank you! 🙏

tuzkiiiii233 avatar Jan 17 '25 09:01 tuzkiiiii233

I'm not sure, honestly. We are basically keeping this behavior up to next.js. Basically we just use the next.js API to change the cookie and I guess that causes next.js to re-render everything.

I found this issue, seems like that could be it: https://github.com/vercel/next.js/issues/50163

stepan662 avatar Jan 20 '25 08:01 stepan662