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

Rewrites with i18n #28921

Closed
Aviortheking opened this issue Sep 8, 2021 · 14 comments · Fixed by #33966
Closed

Rewrites with i18n #28921

Aviortheking opened this issue Sep 8, 2021 · 14 comments · Fixed by #33966
Labels
Internationalization (i18n) Related to Internationalization with Next.js. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@Aviortheking
Copy link

What version of Next.js are you using?

11.1.2 and 11.1.3-canary.14

What version of Node.js are you using?

14 and 16

What browser are you using?

Chrome Firefox Insomnia

What operating system are you using?

Windows, Linux

How are you deploying your application?

Vercel

Describe the Bug

It seems that we cant run POST requests while using i18n on the latest version (it was working on 11.0.x and 11.1.0 but not 11.1.1+)

  • I tried running my request without i18n and it worked
  • I tried running my request with i18n but it stopped working
  • I tried adding/removing the basePath option nothing changed

Expected Behavior

request shouuld be rewrote to the other path

To Reproduce

Minimal Github repository

https://github.com/Aviortheking/tests-nextjs

@Aviortheking Aviortheking added the bug Issue was opened via the bug report template. label Sep 8, 2021
@alexbchr
Copy link

Is it possible to prioritize this issue? Rewrites not working when i18n is enabled is a pretty big issue according to me. Rewrites are especially useful for i18n projects, as it lets you group pages in folders and rewrite those folder names depending on the locale of the user. Otherwise, the only way to do so is using Dynamic Routing, but you can't have 2 dynamic routes at the same level in a folder structure, which makes it non-viable for us in the projects we are working on, as most pages are rendered in a Catch-all dynamic route, so our folder structure looks like:

pages/
  [[...slug]].tsx
  /reserver
      index.ts

If we want the /reserver path (in FR) to be /book in EN, we have no choice but to use rewrites, as using a dynamic route here, such as [bookSlug]/index.ts do weird things with Next.js before of the catch-all route.

I got here after finding the issue #28863

I have exactly the same problem... The rewrites work fine for me locally or when doing next build && next start, but they don't work at all once deployed on Vercel. Tried using locale: false, basePath: false (even though we are not using the basePath feature).

@noblica
Copy link

noblica commented Nov 30, 2021

We're experiencing the same issue. I've also tried it with next v12, but the issue perssits. As the person above me described, it only happens when deploying to vercel. Locally the production build works fine.

@timneutkens timneutkens added Internationalization (i18n) Related to Internationalization with Next.js. Navigation Related to Next.js linking (e.g., <Link>) and navigation. labels Dec 2, 2021
@GiancarlosIO
Copy link

Same here, we just configured i18n.locales and now all of our api rewrites give us 404 errors.
It works in local but not in vercel 🤔

@raphaelHuerzeler
Copy link

raphaelHuerzeler commented Dec 29, 2021

We've also run into this issue:

  • we use i18n
  • we do have a [...alias].tsx catch all route
  • we use rewrites to talk to our backend:
{
   source: `/api/${TENANT}/:path*`,
   destination: `${host}/api/${TENANT}/:path*`,
},
  • all backend requests are POST
  • up to and including nextjs 11.0.1 everything worked fine (we have several production environments running on this)
  • on any newer version the backend request fail, this is exclusive to vercel environment, we've tried amplify a while back and the issue didn't occur.
  • on a hunch i tried changing the rewrites to, which interestingly enough worked:
{
   source: `/backend/${TENANT}/:path*`,
   destination: `${host}/api/${TENANT}/:path*`,
},

So it seems the issue is a combination of:

  • vercel environment
  • i18n
  • POST request to a rewrite
  • having a catch-all route i.e. /[...alias].tsx in our case
  • a rewrite using /api route (although other comments in this thread seem to suggest this also happens in other scenarios)
  • also note that we do NOT have any wildcard routes under /api

All of the deploys below use the exact same codebase (except for nextjs version and next.config.js refconfiguration) and backend.

Deploy (next12.0.7) where dispatcher uses /api and fails (note x-matched-path: //[...alias] in dispatcher response):
https://spectra-whitelabel-dlh5epbbq-deep-impact.vercel.app/en

Deploy (next12.0.7) where dispatcher uses /backend and works:
https://spectra-whitelabel-fh2e0kxc6-deep-impact.vercel.app/en

For completeness sake heres also a deploy with next11.0.1 where /api calls work:
https://spectra-whitelabel-oygahxlxw-deep-impact.vercel.app/en

While the workaround can be made to work as described it's still problematic for some of our tenants as some also use 3rd party integrations that make use of the existing /api rewrites.

@chrisui
Copy link

chrisui commented Jan 21, 2022

Same issue here with /auth/[...nextauth].ts inside /api and a /api/graphql beforeFiles rewrite to an external api. Works locally, fails in vercel.

@MathieuAA
Copy link

It also happens with apps not deployed to Vercel :( this is a blocker

@I-Valchev
Copy link

Same issue here with /auth/[...nextauth].ts inside /api and a /api/graphql beforeFiles rewrite to an external api. Works locally, fails in vercel.

Same issue here with re-routing to an external API.

@lswainemoore
Copy link

lswainemoore commented Feb 15, 2022

I am in the same boat--rewrites working fine with i18n except for POSTs prefixed with /api/, and even then it only 404s once deployed. Does anyone more next.js savvy than me have any idea where to start to look for this bug? I'd gladly take a stab at it, if I had a sense of where to begin.

@noblica
Copy link

noblica commented Feb 15, 2022

I am in the same boat--rewrites working fine with i18n except for POSTs prefixed with /api/, and even then it only 404s once deployed. Does anyone more next.js savvy than me have any idea where to start to look for this bug? I'd gladly take a stab at it, if I had a sense of where to begin.

I think this is more of a vercel bug, than a next.js bug.

@lswainemoore
Copy link

I think this is more of a vercel bug, than a next.js bug.

That makes sense, though complicated slightly by reports in this thread that it's also a problem hosting elsewhere. Regardless, if it's not a next.js bug, are we discussing it in the right place?

@balazsorban44 balazsorban44 removed their assignment Feb 16, 2022
@ghost
Copy link

ghost commented Feb 22, 2022

We have the same issue, with the following config

  i18n: {
    locales: ['en', 'es'],
    localeDetection: false,
  },
  async rewrites() {
    return [
      {
        source: '/my/path/:slug*',
        destination: '/my/other-path/:slug*',
      },
    ]
  },

with i18n object the rewrite does not work, without it works fine

@balazsorban44
Copy link
Member

I could recreate this here, even using canary and a minimalized i18n config: https://github.com/balazsorban44/nextjs-28921

We will look into this.

@balazsorban44 balazsorban44 added kind: bug and removed bug Issue was opened via the bug report template. labels Feb 28, 2022
styfle pushed a commit that referenced this issue Mar 1, 2022
This ensures non-matching API routes can be rewritten with i18n configured as currently we bail and render the 404 page when a locale prefixed API route is requested. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

x-ref: [slack thread](https://vercel.slack.com/archives/CGU8HUTUH/p1643930049224689)
closes: #28921
@ijjk
Copy link
Member

ijjk commented Mar 2, 2022

Hi, this has been updated in v12.1.1-canary.6 of Next.js, please update and give it a try!

@github-actions
Copy link
Contributor

github-actions bot commented Apr 2, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Internationalization (i18n) Related to Internationalization with Next.js. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.