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 not working on Vercel (in production) NextJS #28863

Closed
y471n opened this issue Sep 7, 2021 · 7 comments
Closed

Rewrites not working on Vercel (in production) NextJS #28863

y471n opened this issue Sep 7, 2021 · 7 comments
Labels
bug Issue was opened via the bug report template.

Comments

@y471n
Copy link

y471n commented Sep 7, 2021

What version of Next.js are you using?

11.1.0

What version of Node.js are you using?

14.17.4

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

I have been trying to get Rewrites working in NextJS for my API Paths. It was to avoid CORS issues.

I followed the solution from: https://stackoverflow.com/questions/65058598/nextjs-cors-issue.

It is working on localhost but does not work in a production environment (I was deploying on Vercel itself).

I basically tried with all the types of rewrites:

module.exports = {
async rewrites() {
    return {
      beforeFiles: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
      afterFiles: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
      fallback: [
        {
          source: "/api/:path*",
          destination: `https://example.com/api/v1/:path*`,
          basePath: false,
        },
      ],
    };
  },
  reactStrictMode: true,
  i18n,
  };

This rewrite works on localhost but on production, the rewrite stops working and the API calls go to /api/:path* itself.

Expected Behavior

The redirects are working locally but not working in production (ie. Vercel).

To Reproduce

Add the next.config.js given above and make API calls on client-side using /api/:path*
This isn't working after building the project.

@y471n y471n added the bug Issue was opened via the bug report template. label Sep 7, 2021
@ijjk
Copy link
Member

ijjk commented Sep 7, 2021

Hi, can you provide a deployment URL or repo with a minimal reproduction where you are seeing this not work as expected? If you would like to provide this privately you can reach out here

@junbach
Copy link

junbach commented Sep 11, 2021

I got this issue as well with the rewrites config. It works well with version 11.0.x but seem to be broken at 11.1.x

@ijjk
Copy link
Member

ijjk commented Sep 19, 2021

Hi this appears to be working correctly in the latest version of Next.js v11.1.2, please update and give it a try! Also note: if the API rewrite is added in beforeFiles it doesn't need to also be added in the other rewrite types.

@ijjk ijjk closed this as completed Sep 19, 2021
@mismosmi
Copy link

mismosmi commented Sep 21, 2021 via email

@junbach
Copy link

junbach commented Sep 21, 2021

Hi this appears to be working correctly in the latest version of Next.js v11.1.2, please update and give it a try! Also note: if the API rewrite is added in beforeFiles it doesn't need to also be added in the other rewrite types.

It does not work with 11.1.2 Please double check and re-open this issue

@ijjk
Copy link
Member

ijjk commented Sep 21, 2021

Ah I see, this seems to be a duplicate of #28921 then since it's specific to i18n so we can use that issue for tracking this, thanks for the replies!

@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.
Projects
None yet
Development

No branches or pull requests

5 participants