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.

redirects don't work properly with i18n and basepath

See original GitHub issue

What version of Next.js are you using?

10.0.5

What version of Node.js are you using?

12.16.1

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

docker / k8s

Describe the Bug

I’m developing a nextjs app that should be deployed at /my-base-path. I wanted to redirect / to /my-base-path, and I setup next.config.js accordingly:

const basePath = '/my-base-path';

const nextConfig = {
    basePath,
    env: {
        BASE_PATH: basePath,
    },
    redirects: async () => ([
        {
            source: '/',
            destination: basePath,
            permanent: false,
            basePath: false,
        },
    ])
};
module.exports = nextConfig;

Everything works as expected: / redirects to /my-base-path.

Once I add an i18n config to nextConfig, redirects don’t function as I expect:

const basePath = '/my-base-path';

const nextConfig = {
    basePath,
    env: {
        BASE_PATH: basePath,
    },
    i18n: {
        defaultLocale: 'en',
        locales: ['en', 'es'],
    },
    redirects: async () => ([
        {
            source: '/',
            destination: basePath,
            permanent: false,
            basePath: false,
        },
    ])
};
module.exports = nextConfig;

Expected Behavior

  • / should redirect to /my-base-path, but / just returns a 404.
  • /en redirects to /en/my-base-path, and /en/my-base-path returns a 404.
  • /my-base-path/en renders the welcome page, but I would expect it to redirect to /my-base-path.
  • /my-base-path renders the welcome page as expected. 👍

To Reproduce

A repo demonstrating this behavior exists at https://github.com/ckeeney/nextjs-redirect-i18n-basepath

It’s unclear to me whether the correct path structure is /{locale}/{basePath}/{page} or /{basePath}/{locale}/{page}.

I think the former would make a more consistent URL pattern across multiple zones, but I’m a bit nervous about how complicated that would make the routing rules for the reverse proxy. For the latter option we can simply route all traffic for each base path to it’s designated nextjs app, matching on basePath as you would expect. The former requires that the load balancer have some way of handling the locales, and I’m not sure the more predictable locale URLs are worth that trouble.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:24
  • Comments:8

github_iconTop GitHub Comments

14reactions
giovas94commented, Jan 26, 2022

Any update about this issue? Tks

3reactions
gsambrottacommented, Dec 8, 2021

Same here, trying to use the multi zone example and having problem with /en/blog/

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to enforce i18n locale slugs and achieve ... - Stack Overflow
Workaround #1: Set default locale to default , and redirect in middleware. As documented in the i18n Routing documentation, add a new "dummy" ......
Read more >
next.config.js: Redirects
Redirects allow you to redirect an incoming request path to a different destination path. To use Redirects you can use the redirects key...
Read more >
Next.js basePath and redirects don't work on Netlify - Support
js pages with Netlify. When we are running the project locally, all the redirects are working and basePath is added correctly to all...
Read more >
DocC home page or redirect to /documentation/<target-name>
Unfortunately, the way DocC works means I must go to my GitHub Pages URL e.g. ... with the following so that this URL...
Read more >
Router tutorial: tour of heroes - Angular
Provides a well-known location for routing service providers such as guards and resolvers; Does not declare components. Integrate routing with your applink. The ......
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