Unverified Commit 812cd757 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

[backport] Fix relative same host redirects in node middleware #88253 (#88630)

Backport https://github.com/vercel/next.js/pull/88253 into 16.1
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent cb436b36
...@@ -455,7 +455,10 @@ export async function adapter( ...@@ -455,7 +455,10 @@ export async function adapter(
if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) { if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) {
if (redirectURL.host === requestURL.host) { if (redirectURL.host === requestURL.host) {
redirectURL.buildId = buildId || redirectURL.buildId redirectURL.buildId = buildId || redirectURL.buildId
response.headers.set('Location', redirectURL.toString()) response.headers.set(
'Location',
getRelativeURL(redirectURL, requestURL)
)
} }
} }
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
export async function middleware(request) { export default async function middleware(request) {
const url = request.nextUrl const url = request.nextUrl
// this is needed for tests to get the BUILD_ID // this is needed for tests to get the BUILD_ID
......
...@@ -15,7 +15,15 @@ describe('Middleware Redirect', () => { ...@@ -15,7 +15,15 @@ describe('Middleware Redirect', () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(join(__dirname, '../app/pages')), pages: new FileRef(join(__dirname, '../app/pages')),
'middleware.js': new FileRef(join(__dirname, '../app/middleware.js')), ...(process.env.TEST_NODE_MIDDLEWARE
? {
'proxy.js': new FileRef(join(__dirname, '../app/middleware.js')),
}
: {
'middleware.js': new FileRef(
join(__dirname, '../app/middleware.js')
),
}),
'next.config.js': new FileRef(join(__dirname, '../app/next.config.js')), 'next.config.js': new FileRef(join(__dirname, '../app/next.config.js')),
}, },
}) })
...@@ -39,6 +47,15 @@ describe('Middleware Redirect', () => { ...@@ -39,6 +47,15 @@ describe('Middleware Redirect', () => {
) )
}) })
it('should have relative path for same host redirect', async () => {
const res = await next.fetch('/to?pathname=/another', {
// workaround for https://github.com/node-fetch/node-fetch/issues/417
redirect: 'manual-dont-change' as any,
})
expect(res.status).toBe(302)
expect(res.headers.get('Location')).toBe('/another')
})
it(`should redirect to data urls with data requests and internal redirects`, async () => { it(`should redirect to data urls with data requests and internal redirects`, async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
next.url, next.url,
......
process.env.TEST_NODE_MIDDLEWARE = 'true'
require('./index.test')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment