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

Dynamic routes using the wrong locale in Next 13.4.13 #1131

Open
Curetix opened this issue Aug 12, 2023 · 2 comments
Open

Dynamic routes using the wrong locale in Next 13.4.13 #1131

Curetix opened this issue Aug 12, 2023 · 2 comments
Milestone

Comments

@Curetix
Copy link
Contributor

Curetix commented Aug 12, 2023

What version of this package are you using? 2.5.3

What operating system, Node.js, and npm version? Windows, Node 20, pnpm

What happened?

I have an app with the two locales en (base) and de. Some of my routes are dynamic, like /[channel] and /video/[id].

After updating to Next 13.4.13, I am encountering this behavior in my app: When navigating to a dynamic route like /[channel], it switches to the base locale even though the URL locale doesn't change. So it would navigate from /de to /de/test, but loads the en locale. When navigating back to a non-dynamic route it uses the correct locale again. This only happens with Next 13.4.13, dynamic routes and when navigating using a router Link or router.push.

Here's the dev server logs:

next-translate - compiled page: / - locale: de - namespaces: common, navbar, home, channel - used loader: getServerSideProps
next-translate - compiled page: /[channelName] - locale: en - namespaces: common, navbar - used loader: getServerSideProps
@shelooks16
Copy link
Contributor

shelooks16 commented Aug 14, 2023

I experience something similar while using pages directory.

When navigating through <Link> to a dynamic route or page with getServerSideProps, translations are not loaded. Instead, it shows translation keys i.e. about_us.some.key. After page refresh or when the page opened directly translations show OK. Navigation between static pages works OK.

next-translate and the plugin of v2.5.3, next.js v13.4.13

  • Downgrading next to 13.3.x did not help
  • legacyBehavior on <Link> did not help

Any idea?

========

Update

I figured this is related to middleware.ts. Middleware I use:

export function middleware(req: NextRequest) {
  if (!PUBLIC_FILE.test(req.nextUrl.pathname)) {
    const { browser } = userAgent(req);
    
    if (!isBrowserSupported(browser?.name, browser?.version)) {
      const redirectUrl = req.nextUrl.clone();
      redirectUrl.pathname = '/unsupported.html';

      return NextResponse.redirect(redirectUrl);
    }
  }
}

As you can see, the middleware has nothing to do with locale.

If I delete middleware.ts, navigation works as expected and all translations are loaded.

@aralroca I saw a workaround with [lang] prefix but in my case:

  1. there is only one locale. In i18.js I have locales: ['en']
  2. only pages directory is in use. app directory is not even created
  3. locale is not visible in the url. All urls are in format site.com/about rather than site.com/en/about or site.com/about?lang=en

Is it a known problem? I do not want to remove the middleware, and want to keep urls in current format site.com/about

@aralroca aralroca added this to the next release milestone Aug 14, 2023
@Curetix
Copy link
Contributor Author

Curetix commented Aug 15, 2023

I can confirm what @shelooks16 has found. When removing my middleware.ts, which does not handle anything locale-related, this issues does not occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants