Skip to content

Commit

Permalink
Apply normalize patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Nov 8, 2022
1 parent 1b8ab4e commit b9c7408
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -721,6 +722,8 @@ export default async function build(
staticRoutes,
dataRoutes: [],
i18n: config.i18n || undefined,
skipMiddlewareUrlNormalize:
config.experimental.skipMiddlewareUrlNormalize,
}
})

Expand Down
21 changes: 14 additions & 7 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions test/e2e/skip-trailing-slash-redirect/app/middleware.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
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))
}

Expand Down
19 changes: 17 additions & 2 deletions test/e2e/skip-trailing-slash-redirect/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -88,15 +103,15 @@ 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' } }
)
expect(res.headers.get('x-nextjs-rewrite').endsWith('/another/')).toBe(true)

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' } }
)
Expand Down

0 comments on commit b9c7408

Please sign in to comment.