Next.js Discord

Discord Forum

NextResponse.rewrite wiping out response headers

Unanswered
Blue swimming crab posted this in #help-forum
Open in Discord
Blue swimming crabOP
I wrote a middleware which is calling external API like this:
import { NextAuthRequest } from 'next-auth/lib';
import { NextResponse } from 'next/server';

import { auth } from './auth';

export default auth((req: NextAuthRequest) => {
  // retrieve the azure idToken
  const token = req.auth?.idToken;
  const pathname = req.nextUrl.pathname.replace('/api/wg', '');

  // rewrite the api url to the WunderGraph API
  const url = new URL(
    pathname + req.nextUrl.search,
    process.env.NEXT_WUNDERGRAPH_URL || 'http://localhost:9991',
  );

  // add the token to the Authorization header
  const newHeaders = new Headers(req.headers);
  newHeaders.set('Authorization', `Bearer ${token}`);

  // rewrite the request to the WunderGraph API
  const response = NextResponse.rewrite(url, {
    request: {
      headers: newHeaders,
    },
  });

  return response;
});

export const config = {
  matcher: ['/api/wg/:function*'],
};


Everything seems to be working fine for simple cases but whenever it is calling Streaming Api which is using EventSource the responses headers like Connection: keep-alive from external API are wiped and I'm getting Connection: closed. When I change it to fetch instead of NextResponse.rewrite:
  ...
  // add the token to the Authorization header
  const newHeaders = new Headers(req.headers);
  newHeaders.set('Authorization', `Bearer ${token}`);

  const data = await fetch(url, { ...req, headers: newHeaders });

  return data;
});
  ...

I'm getting response headers from external API correctly. Am I missing something with NextResponse.rewrite. I think it should not remove response headers from external api.

5 Replies

@Blue swimming crab I wrote a middleware which is calling external API like this: import { NextAuthRequest } from 'next-auth/lib'; import { NextResponse } from 'next/server'; import { auth } from './auth'; export default auth((req: NextAuthRequest) => { // retrieve the azure idToken const token = req.auth?.idToken; const pathname = req.nextUrl.pathname.replace('/api/wg', ''); // rewrite the api url to the WunderGraph API const url = new URL( pathname + req.nextUrl.search, process.env.NEXT_WUNDERGRAPH_URL || 'http://localhost:9991', ); // add the token to the Authorization header const newHeaders = new Headers(req.headers); newHeaders.set('Authorization', `Bearer ${token}`); // rewrite the request to the WunderGraph API const response = NextResponse.rewrite(url, { request: { headers: newHeaders, }, }); return response; }); export const config = { matcher: ['/api/wg/:function*'], }; Everything seems to be working fine for simple cases but whenever it is calling Streaming Api which is using EventSource the responses headers like `Connection: keep-alive` from external API are wiped and I'm getting `Connection: closed`. When I change it to `fetch` instead of `NextResponse.rewrite`: ... // add the token to the Authorization header const newHeaders = new Headers(req.headers); newHeaders.set('Authorization', `Bearer ${token}`); const data = await fetch(url, { ...req, headers: newHeaders }); return data; }); ... I'm getting response headers from external API correctly. Am I missing something with `NextResponse.rewrite`. I think it should not remove response headers from external api.
try setting the header like this
 // rewrite the request to the WunderGraph API
 const response = NextResponse.rewrite(url);
  // add the token to the Authorization header
 response.headers.set('Authorization', `Bearer ${token}`)

 return response;
Blue swimming crabOP
@Ray it's not a problem of authorization. Authorization works - it's just the headers send from external api are not set in NextResponse. For example header send by external api is Connection: keep-alive while the one in NextResponse is Connection: close. It's like the NextResponse is not setting headers passed by external API.
Blue swimming crabOP
@Ray that way I would wipe out Response Headers with Request Headers. I want the headers from External API - not headers set by me.