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

Unexpected redirection of API routes with localization #20439

Closed
ariesclark opened this issue Dec 24, 2020 · 9 comments
Closed

Unexpected redirection of API routes with localization #20439

ariesclark opened this issue Dec 24, 2020 · 9 comments
Labels
bug Issue was opened via the bug report template. please add a complete reproduction Please add a complete reproduction.

Comments

@ariesclark
Copy link

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:

  1. In your next.config.js, declare under i18n the defaultLocale and other applicable supported locales.
  2. Define an API route within pages/api/.
  3. Visit said route.
  4. It'll return a redirection code.

Expected behavior

Next.js shouldn't redirect API routes to localized variants, at least by default.

System information

  • OS: Ubuntu 20.04.1 LTS x86_64
  • Version of Next.js: v10.0.3
  • Version of Node.js: v15.2.1
  • Deployment: Vercel, next dev

Additional context

Add any other context about the problem here.

@ariesclark ariesclark added the bug Issue was opened via the bug report template. label Dec 24, 2020
@ijjk
Copy link
Member

ijjk commented Dec 24, 2020

Hi, can you provide a repo with a minimal reproduction? I tried reproducing with the steps provided above but was unable to.

Configured next.config.js

module.exports = {
  i18n: {
    locales: ['fr', 'en'],
    defaultLocale: 'en'
  }
}

Next.js versions tested v10.0.3 and v10.0.4

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

@ijjk ijjk added the please add a complete reproduction Please add a complete reproduction. label Dec 24, 2020
@ariesclark
Copy link
Author

Hi, can you provide a repo with a minimal reproduction? I tried reproducing with the steps provided above but was unable to.

Configured next.config.js

module.exports = {
  i18n: {
    locales: ['fr', 'en'],
    defaultLocale: 'en'
  }
}

Next.js versions tested v10.0.3 and v10.0.4

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.

@ijjk
Copy link
Member

ijjk commented Dec 24, 2020

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

@ariesclark
Copy link
Author

Oddly enough, I can't seem to reproduce in a fresh repository, however in the current project, It surely does redirect.


curl -v http://localhost:3000/api/v1/auth/login
*   Trying ::1...
* TCP_NODELAY set
* Expire in 149995 ms for 3 (transfer 0x5720b52f10)
* Expire in 200 ms for 4 (transfer 0x5720b52f10)
* Connected to localhost (::1) port 3000 (#0)
> GET /api/v1/auth/login HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 308 Permanent Redirect
< Location: /en/api/v1/auth/login/
< Refresh: 0;url=/en/api/v1/auth/login/
< Date: Thu, 24 Dec 2020 16:49:30 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Content-Length: 0
< 
* Connection #0 to host localhost left intact
* ```

@ariesclark
Copy link
Author

Strangely, it might be something from within my own project, I'll close this issue for now.

@ijjk
Copy link
Member

ijjk commented Dec 24, 2020

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

@ariesclark
Copy link
Author

fixed by #20751

@matthieudesprez
Copy link

matthieudesprez commented Oct 14, 2021

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",
  },
  ...

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. please add a complete reproduction Please add a complete reproduction.
Projects
None yet
Development

No branches or pull requests

4 participants