Next.js Discord

Discord Forum

Default route not working with rewrites/redirects and i18next

Unanswered
Saarloos Wolfdog posted this in #help-forum
Open in Discord
Saarloos WolfdogOP
eg.
domain.com/en-US/test WORKS
while
domain.com/test NO WORK (404)
with
 rewrites: () => {
    return {
      beforeFiles: [
        {
          source: '/test',
          destination: `https://jsonplaceholder.typicode.com/posts/1`,
        },
      ],
    },
 }


Disabling locale with locale: false fixes the default route but then breaks all locale routes to that rewrite.

How can we get both working?

14 Replies

Saarloos WolfdogOP
Are you using domain-based i18n or path-based?
Also, have you tried doing it in a return instead of beforeFIles?

You might also be able to do

rewrites: () => {
    return {
      beforeFiles: [
        {
          source: '/:locale/test',
          destination: `https://jsonplaceholder.typicode.com/posts/1`,
        },
      ],
    },
 }
Saarloos WolfdogOP
@Marchy you can have a peak at the reproduction examples in the issue mentioned above https://github.com/vercel/next.js/issues/57616
it doesn't use beforeFiles and is just using the official nextjs example for i18n
Saarloos WolfdogOP
@Marchy Can you reread that issue? We strictly do not want to use locale in this rewrite.
browse to /test should resolve to https://jsonplaceholder.typicode.com/posts/1
we explicitly do not want /:locale/test to do anything. These should 404.
Saarloos WolfdogOP
If your developing / supporting NextJS then the repo examples are usually the most sought after for reproduction repos. It's literally how the test cases are run. Nw, I think the issue is on their radar.
@Saarloos Wolfdog If your developing / supporting NextJS then the repo examples are usually the most sought after for reproduction repos. It's literally how the test cases are run. Nw, I think the issue is on their radar.
I don't believe this is an error within Next.js. You might just need to do something like

 rewrites: () => {
    return {
      beforeFiles: [
 {
          source: '/:locale/test',
          destination: `/test`,
        },
        {
          source: '/test',
          destination: `https://jsonplaceholder.typicode.com/posts/1`,
        },
      ],
    },
 }
Saarloos WolfdogOP
@Marchy and what would be the result if then someone browsed to /en_US/test1?

https://nextjs-forum.com/post/1167473900519694386#message-1167650017385918474
@Saarloos Wolfdog <@203709756689350656> and what would be the result if then someone browsed to `/en_US/test1`? https://discord.com/channels/752553802359505017/1167473900519694386/1167650017385918474
locale: false fixes the default route but then breaks all locale routes to that rewrite.

What did you mean by this?
Saarloos WolfdogOP
Thanks for trying to help @Marchy . I think you may only have read what I wrote in the above post in discord but have yet to read the explicit github issue that I also posted. It is very detailed and completely lays out what the problem is. https://github.com/vercel/next.js/issues/57616