Skip to content

Commit b9c7408

Browse files
committedNov 8, 2022
Apply normalize patch
1 parent 1b8ab4e commit b9c7408

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed
 

‎packages/next/build/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ export default async function build(
695695
defaultLocale: string
696696
localeDetection?: false
697697
}
698+
skipMiddlewareUrlNormalize?: boolean
698699
} = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => {
699700
const sortedRoutes = getSortedRoutes([
700701
...pageKeys.pages,
@@ -721,6 +722,8 @@ export default async function build(
721722
staticRoutes,
722723
dataRoutes: [],
723724
i18n: config.i18n || undefined,
725+
skipMiddlewareUrlNormalize:
726+
config.experimental.skipMiddlewareUrlNormalize,
724727
}
725728
})
726729

‎packages/next/server/next-server.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1714,14 +1714,21 @@ export default class NextNodeServer extends BaseServer {
17141714
const normalizedPathname = removeTrailingSlash(params.parsed.pathname || '')
17151715

17161716
// For middleware to "fetch" we must always provide an absolute URL
1717-
const query = urlQueryToSearchParams(params.parsed.query).toString()
1718-
const locale = params.parsed.query.__nextLocale
1717+
let url: string
17191718

1720-
const url = `${getRequestMeta(params.request, '_protocol')}://${
1721-
this.hostname
1722-
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
1723-
query ? `?${query}` : ''
1724-
}`
1719+
if (this.nextConfig.experimental.skipMiddlewareUrlNormalize) {
1720+
url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
1721+
} else {
1722+
// For middleware to "fetch" we must always provide an absolute URL
1723+
const query = urlQueryToSearchParams(params.parsed.query).toString()
1724+
const locale = params.parsed.query.__nextLocale
1725+
1726+
url = `${getRequestMeta(params.request, '_protocol')}://${
1727+
this.hostname
1728+
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
1729+
query ? `?${query}` : ''
1730+
}`
1731+
}
17251732

17261733
if (!url.startsWith('http')) {
17271734
throw new Error(

‎test/e2e/skip-trailing-slash-redirect/app/middleware.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { NextResponse } from 'next/server'
22

33
export default function handler(req) {
4+
console.log(req.nextUrl)
5+
46
if (req.nextUrl.pathname.startsWith('/_next/data/missing-id')) {
57
console.log(`missing-id rewrite: ${req.nextUrl.toString()}`)
68
return NextResponse.rewrite('https://example.vercel.sh')
79
}
810

9-
if (req.nextUrl.pathname === '/middleware-rewrite-with-slash') {
11+
if (
12+
req.nextUrl.pathname.startsWith('/_next/data') &&
13+
req.nextUrl.pathname.endsWith('valid.json')
14+
) {
15+
return NextResponse.rewrite('https://example.vercel.sh')
16+
}
17+
18+
if (req.nextUrl.pathname.includes('/middleware-rewrite-with-slash')) {
1019
return NextResponse.rewrite(new URL('/another/', req.nextUrl))
1120
}
1221

13-
if (req.nextUrl.pathname === '/middleware-rewrite-without-slash') {
22+
if (req.nextUrl.pathname.includes('/middleware-rewrite-without-slash')) {
1423
return NextResponse.rewrite(new URL('/another', req.nextUrl))
1524
}
1625

‎test/e2e/skip-trailing-slash-redirect/index.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ describe('skip-trailing-slash-redirect', () => {
3535
}
3636
})
3737

38+
it('should provide original _next/data URL with skipMiddlewareUrlNormalize', async () => {
39+
const res = await fetchViaHTTP(
40+
next.url,
41+
`/_next/data/${next.buildId}/valid.json`,
42+
undefined,
43+
{
44+
headers: {
45+
'x-nextjs-data': '1',
46+
},
47+
}
48+
)
49+
expect(res.status).toBe(200)
50+
expect(await res.text()).toContain('Example Domain')
51+
})
52+
3853
it('should allow response body from middleware with flag', async () => {
3954
const res = await fetchViaHTTP(next.url, '/middleware-response-body')
4055
expect(res.status).toBe(200)
@@ -88,15 +103,15 @@ describe('skip-trailing-slash-redirect', () => {
88103
it('should correct skip URL normalizing in middleware', async () => {
89104
let res = await fetchViaHTTP(
90105
next.url,
91-
'/middleware-rewrite-with-slash',
106+
`/_next/data/${next.buildId}/middleware-rewrite-with-slash.json`,
92107
undefined,
93108
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
94109
)
95110
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another/')).toBe(true)
96111

97112
res = await fetchViaHTTP(
98113
next.url,
99-
'/middleware-rewrite-without-slash',
114+
`/_next/data/${next.buildId}/middleware-rewrite-without-slash.json`,
100115
undefined,
101116
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
102117
)

0 commit comments

Comments
 (0)
Please sign in to comment.