Add experimental skipMiddlewareUrlNormalize flag

parent 927bd163
...@@ -1529,6 +1529,9 @@ export default async function getBaseWebpackConfig( ...@@ -1529,6 +1529,9 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_NEW_LINK_BEHAVIOR': JSON.stringify( 'process.env.__NEXT_NEW_LINK_BEHAVIOR': JSON.stringify(
config.experimental.newNextLinkBehavior config.experimental.newNextLinkBehavior
), ),
'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': JSON.stringify(
config.experimental.skipMiddlewareUrlNormalize
),
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify( 'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify(
config.experimental.optimisticClientCache config.experimental.optimisticClientCache
), ),
......
...@@ -355,6 +355,9 @@ const configSchema = { ...@@ -355,6 +355,9 @@ const configSchema = {
sharedPool: { sharedPool: {
type: 'boolean', type: 'boolean',
}, },
skipMiddlewareUrlNormalize: {
type: 'boolean',
},
skipTrailingSlashRedirect: { skipTrailingSlashRedirect: {
type: 'boolean', type: 'boolean',
}, },
......
...@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig { ...@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
skipMiddlewareUrlNormalize?: boolean
skipTrailingSlashRedirect?: boolean skipTrailingSlashRedirect?: boolean
optimisticClientCache?: boolean optimisticClientCache?: boolean
legacyBrowsers?: boolean legacyBrowsers?: boolean
......
...@@ -103,10 +103,12 @@ export async function adapter(params: { ...@@ -103,10 +103,12 @@ export async function adapter(params: {
nextConfig: params.request.nextConfig, nextConfig: params.request.nextConfig,
}) })
if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) {
if (rewriteUrl.host === request.nextUrl.host) { if (rewriteUrl.host === request.nextUrl.host) {
rewriteUrl.buildId = buildId || rewriteUrl.buildId rewriteUrl.buildId = buildId || rewriteUrl.buildId
response.headers.set('x-middleware-rewrite', String(rewriteUrl)) response.headers.set('x-middleware-rewrite', String(rewriteUrl))
} }
}
/** /**
* When the request is a data request we must show if there was a rewrite * When the request is a data request we must show if there was a rewrite
...@@ -140,10 +142,12 @@ export async function adapter(params: { ...@@ -140,10 +142,12 @@ export async function adapter(params: {
*/ */
response = new Response(response.body, response) response = new Response(response.body, response)
if (!process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE) {
if (redirectURL.host === request.nextUrl.host) { if (redirectURL.host === request.nextUrl.host) {
redirectURL.buildId = buildId || redirectURL.buildId redirectURL.buildId = buildId || redirectURL.buildId
response.headers.set('Location', String(redirectURL)) response.headers.set('Location', String(redirectURL))
} }
}
/** /**
* When the request is a data request we can't use the location header as * When the request is a data request we can't use the location header as
......
import { NextResponse } from 'next/server'
export default function handler(req) {
if (req.nextUrl.pathname === '/middleware-rewrite-with-slash') {
return NextResponse.rewrite(new URL('/another/', req.nextUrl))
}
if (req.nextUrl.pathname === '/middleware-rewrite-without-slash') {
return NextResponse.rewrite(new URL('/another', req.nextUrl))
}
if (req.nextUrl.pathname === '/middleware-redirect-external-with') {
return NextResponse.redirect('https://example.vercel.sh/somewhere/', 307)
}
if (req.nextUrl.pathname === '/middleware-redirect-external-without') {
return NextResponse.redirect('https://example.vercel.sh/somewhere', 307)
}
return NextResponse.next()
}
...@@ -14,6 +14,7 @@ describe('skip-trailing-slash-redirect', () => { ...@@ -14,6 +14,7 @@ describe('skip-trailing-slash-redirect', () => {
nextConfig: { nextConfig: {
experimental: { experimental: {
skipTrailingSlashRedirect: true, skipTrailingSlashRedirect: true,
skipMiddlewareUrlNormalize: true,
}, },
async redirects() { async redirects() {
return [ return [
...@@ -55,6 +56,46 @@ describe('skip-trailing-slash-redirect', () => { ...@@ -55,6 +56,46 @@ describe('skip-trailing-slash-redirect', () => {
}) })
} }
it('should correct skip URL normalizing in middleware', async () => {
let res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-with-slash',
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another/')).toBe(true)
res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-without-slash',
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another')).toBe(true)
res = await fetchViaHTTP(
next.url,
'/middleware-redirect-external-with',
undefined,
{ redirect: 'manual' }
)
expect(res.status).toBe(307)
expect(res.headers.get('Location')).toBe(
'https://example.vercel.sh/somewhere/'
)
res = await fetchViaHTTP(
next.url,
'/middleware-redirect-external-without',
undefined,
{ redirect: 'manual' }
)
expect(res.status).toBe(307)
expect(res.headers.get('Location')).toBe(
'https://example.vercel.sh/somewhere'
)
})
it('should apply config redirect correctly', async () => { it('should apply config redirect correctly', async () => {
const res = await fetchViaHTTP(next.url, '/redirect-me', undefined, { const res = await fetchViaHTTP(next.url, '/redirect-me', undefined, {
redirect: 'manual', redirect: 'manual',
......
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