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.

[Internationalization] Redirect with `{ locale: false }` and dynamic isn't working properly.

See original GitHub issue

Bug report

Describe the bug

Redirect with { locale: false } isn’t working properly.

Context

Before describe the bug, I need to give you some context about what problem I’m trying to solve.

I recently switch my website default locale from pt to en. It means that some old URLs I had localized, now would be inverted, for example:

URL example:

Before: /blog/e-o-coronavirus-hein (default lang PT)
After: /pt/blog/e-o-coronavirus-hein (Default lang EN)

The problem

What’s is happening now is I can’t just setup this redirect via next.config.js or via vercel.json (but I’ll focus on next config).

The way I would try to solve that is setting up something like this:

module.exports = {
  async redirects() {
    return [
      {
        source: "/blog/e-o-coronavirus-hein",
        destination: "/pt/blog/e-o-coronavirus-hein",
        permanent: true,
      },
    ];
  },
};

This won’t work and that’s comprehensible. I assume when I try to access /blog/e-o-coronavirus-hein, since my browser is in English, it’ll try to resolve the request locale to /en. But the stranger thing is that the redirect works but it concatenates everything and I’m being redirected to: /en/pt/blog/e-o-coronavirus-hein.

That’s weird but makes sense because I’m embedding the locale in the path.

In the docs it says we can turn off the locale process, that should do the job, right?

module.exports = {
  async redirects() {
    return [
      {
        source: "/blog/e-o-coronavirus-hein",
        destination: "/pt/blog/e-o-coronavirus-hein",
        locale: false,
        permanent: true,
      },
    ];
  },
};

But now if I try to access /blog/e-o-coronavirus-hein, instead being redirected to /pt/blog/e-o-coronavirus-hein, I just reach 404.

I assume that somehow it’s thinking that’s is a valid route (because I use pages/blog/[slug].js) and it does not consider the redirect setup.

Another problem (which I can open another issue if you want to) is when I try to redirect from a url which even does not exist from an existing one:

module.exports = {
  async redirects() {
    return [
      {
        source: "/2018/08/09/coronavirus",
        destination: "/pt/blog/e-o-coronavirus-hein",
        locale: false,
        permanent: true,
      },
    ];
  },
};

In this case I also get 404.

To Reproduce

I prepare a demo site with some deeper explanation how to test this problem but:

  1. Access: https://next-redirect-bug.vercel.app/
  2. Check the Defective redirects section
  3. Click in both links which should redirect

Expected behavior

As a dev user, I want to be able to setup a redirect from a /blog/some-post to /<locale>/blog/some-post .

Screenshots

Not applicable

System information

  • OS: Pop!_OS
  • Browser (if applies) [e.g. chrome, safari]: any
  • Version of Next.js: [e.g. 10.0.1]: 10.0.3-canary.3
  • Version of Node.js: [e.g. 12.0.0]: 12.19.1
  • Deployment: [e.g. next start, next export, Vercel, other platform]: Vercel

Additional context

Update

  • This also does not work with rewrites

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
robertcoopercodecommented, Jan 14, 2021

I think this might also affect rewrites in addition to redirects since I’m having a similar issue: https://github.com/vercel/next.js/discussions/21096

2reactions
c7nj7ncommented, Apr 11, 2022

Despite the traction it does not seem that the issue will be addressed any time soon. Is there a recommended workaround to be able to “cleanly” deal with this? Thank you in advance for your help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django internationalization isn't redirecting correctly when ...
So I found the problem. Apparently, you MUST include a next parameter/input field that correspond to the current path (without the language ...
Read more >
Router - Angular
Promise<boolean> : A Promise that resolves to true when navigation succeeds, to false when navigation fails, or is rejected on error. Usage Noteslink....
Read more >
Multi-Regional and Multilingual Sites | Google Search Central
Multi-regional and multilingual sites can be optimized for Search. Learn more about managing SEO for sites with multiple languages.
Read more >
How to obtained or generated dynamic URL depending...
Solved: To expert developers, does anyone have any suggestion on how to change the script below to make the redirect URL dynamic?
Read more >
Static HTML Export with i18n compatibility in Next.js - locize
Well, this features requires a Node.js server, or dynamic logic that cannot be ... Executing next export when not using i18n seems to...
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