-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Unexpected redirection of API routes with localization #20439
Comments
Hi, can you provide a repo with a minimal reproduction? I tried reproducing with the steps provided above but was unable to. Configured module.exports = {
i18n: {
locales: ['fr', 'en'],
defaultLocale: 'en'
}
} Next.js versions tested API request curl -v 'http://localhost:3000/en/api/hello'
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /en/api/hello HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< ETag: "13-5j0ZZR0tI549fSRsYxl8c9vAU78"
< Content-Length: 19
< Vary: Accept-Encoding
< Date: Thu, 24 Dec 2020 05:02:25 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
* Connection #0 to host localhost left intact
{"name":"John Doe"}* Closing connection 0 |
In the curl request you did, you called the localized endpoint, calling /api/hello redirects to /en/api/hello, which I'm pretty sure it shouldn't. |
That was on example request, I tested the other variants as well to see if there were any redirects occurring there. Note: we only apply locale redirects for the index route (/) currently so the API routes should not be affected. I added the results from the other checks below curl -v 'http://localhost:3000/api/hello'
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /api/hello HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< ETag: "13-5j0ZZR0tI549fSRsYxl8c9vAU78"
< Content-Length: 19
< Vary: Accept-Encoding
< Date: Thu, 24 Dec 2020 16:22:38 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
* Connection #0 to host localhost left intact
{"name":"John Doe"}* Closing connection 0 curl -v 'http://localhost:3000/fr/api/hello'
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /fr/api/hello HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< ETag: "13-5j0ZZR0tI549fSRsYxl8c9vAU78"
< Content-Length: 19
< Vary: Accept-Encoding
< Date: Thu, 24 Dec 2020 16:22:57 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
* Connection #0 to host localhost left intact
{"name":"John Doe"}* Closing connection 0 |
Oddly enough, I can't seem to reproduce in a fresh repository, however in the current project, It surely does redirect.
|
Strangely, it might be something from within my own project, I'll close this issue for now. |
It looks like it's applying the trailingSlash redirect and prepending the locale unexpectedly which has been fixed in the latest version of Next.js V10.0.4. If you want to avoid the redirect completely you can make sure the request does or doesn't have the trailing slash per the config in next.config.js |
fixed by #20751 |
still experiencing api routes redirection with the following configuration which aims to redirect all / routes to /fr for SEO reasons, it works well but now all api routes are redirected to /fr/api async redirects() {
return [
{
source: "/catchAll",
destination: "/fr",
locale: false,
permanent: true,
},
{
source: "/catchAll/:slug*",
destination: "/fr/:slug*",
locale: false,
permanent: true,
}
}
},
i18n: {
locales: ["nl", "fr", "catchAll"],
defaultLocale: "catchAll",
},
... |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
Expected behavior
Next.js shouldn't redirect API routes to localized variants, at least by default.
System information
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: