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

API rewrites that can respect the cookies #29488

Closed
peterbe opened this issue Sep 29, 2021 · 3 comments
Closed

API rewrites that can respect the cookies #29488

peterbe opened this issue Sep 29, 2021 · 3 comments

Comments

@peterbe
Copy link

peterbe commented Sep 29, 2021

Describe the feature you'd like to request

I have it so that a fetch('/api/foo') in the client becomes a rewrite to http://localhost:8000/api/v1/foo in the dev server. It looks something like this:

// next.config.js
module.exports = (phase, { defaultConfig }) => {
  if (phase === PHASE_DEVELOPMENT_SERVER) {
    return {
      /* development only config options here */
      async rewrites() {
        return [
          {
            source: "/api/:path*",
            destination: "http://localhost:8000/api/:path*",
          },
        ];
      },
    };
  }

  return {
    /* config options for all phases except development here */
  };
};

The problem is that if that server contains a Set-Cookie header, that is dropped when it gets to the client.

Describe the solution you'd like

Basically, that rewrites works like a proxy. Ideally, respecting all the headers and stuff.
Perhaps even more ideally, more configuration (and documentation) keys that you can add to each entry in the rewrites. Right now, there's only source, destination, and has. Would be nice to add something like:

      async rewrites() {
        return [
          {
            source: "/api/:path*",
            destination: "http://localhost:8000/api/:path*",
           proxy: true
          },
        ];
      },

Describe alternatives you've considered

https://www.npmjs.com/package/next-http-proxy-middleware

@peterbe
Copy link
Author

peterbe commented Sep 29, 2021

I noticed that #9338 inspired #10041 which is using http-proxy so it is a proxy solution. I just don't know why it's dropping set-cookie headers.

@peterbe
Copy link
Author

peterbe commented Sep 29, 2021

What the heck! It is working. Sigh. I tested it with both the browser and curl and it was clear.
But I think what might have happened is that I didn't restart the npm run dev. I'm not sure. Now things are working as expected. Weird.

@peterbe peterbe closed this as completed Sep 29, 2021
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants