Apply normalize patch

parent 1b8ab4e3
......@@ -695,6 +695,7 @@ export default async function build(
defaultLocale: string
localeDetection?: false
}
skipMiddlewareUrlNormalize?: boolean
} = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => {
const sortedRoutes = getSortedRoutes([
...pageKeys.pages,
......@@ -721,6 +722,8 @@ export default async function build(
staticRoutes,
dataRoutes: [],
i18n: config.i18n || undefined,
skipMiddlewareUrlNormalize:
config.experimental.skipMiddlewareUrlNormalize,
}
})
......
......@@ -1714,14 +1714,21 @@ export default class NextNodeServer extends BaseServer {
const normalizedPathname = removeTrailingSlash(params.parsed.pathname || '')
// For middleware to "fetch" we must always provide an absolute URL
const query = urlQueryToSearchParams(params.parsed.query).toString()
const locale = params.parsed.query.__nextLocale
let url: string
const url = `${getRequestMeta(params.request, '_protocol')}://${
this.hostname
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
query ? `?${query}` : ''
}`
if (this.nextConfig.experimental.skipMiddlewareUrlNormalize) {
url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
} else {
// For middleware to "fetch" we must always provide an absolute URL
const query = urlQueryToSearchParams(params.parsed.query).toString()
const locale = params.parsed.query.__nextLocale
url = `${getRequestMeta(params.request, '_protocol')}://${
this.hostname
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
query ? `?${query}` : ''
}`
}
if (!url.startsWith('http')) {
throw new Error(
......
import { NextResponse } from 'next/server'
export default function handler(req) {
console.log(req.nextUrl)
if (req.nextUrl.pathname.startsWith('/_next/data/missing-id')) {
console.log(`missing-id rewrite: ${req.nextUrl.toString()}`)
return NextResponse.rewrite('https://example.vercel.sh')
}
if (req.nextUrl.pathname === '/middleware-rewrite-with-slash') {
if (
req.nextUrl.pathname.startsWith('/_next/data') &&
req.nextUrl.pathname.endsWith('valid.json')
) {
return NextResponse.rewrite('https://example.vercel.sh')
}
if (req.nextUrl.pathname.includes('/middleware-rewrite-with-slash')) {
return NextResponse.rewrite(new URL('/another/', req.nextUrl))
}
if (req.nextUrl.pathname === '/middleware-rewrite-without-slash') {
if (req.nextUrl.pathname.includes('/middleware-rewrite-without-slash')) {
return NextResponse.rewrite(new URL('/another', req.nextUrl))
}
......
......@@ -35,6 +35,21 @@ describe('skip-trailing-slash-redirect', () => {
}
})
it('should provide original _next/data URL with skipMiddlewareUrlNormalize', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/valid.json`,
undefined,
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('Example Domain')
})
it('should allow response body from middleware with flag', async () => {
const res = await fetchViaHTTP(next.url, '/middleware-response-body')
expect(res.status).toBe(200)
......@@ -88,7 +103,7 @@ describe('skip-trailing-slash-redirect', () => {
it('should correct skip URL normalizing in middleware', async () => {
let res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-with-slash',
`/_next/data/${next.buildId}/middleware-rewrite-with-slash.json`,
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
......@@ -96,7 +111,7 @@ describe('skip-trailing-slash-redirect', () => {
res = await fetchViaHTTP(
next.url,
'/middleware-rewrite-without-slash',
`/_next/data/${next.buildId}/middleware-rewrite-without-slash.json`,
undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
)
......
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