Unverified Commit 85b200a8 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure skip normalize is handled correctly (#42642)

x-ref: [slack
thread](https://vercel.slack.com/archives/C01224Q5M99/p1667927545637489?thread_ts=1664536480.045539&cid=C01224Q5M99)
x-ref: https://github.com/vercel/vercel/pull/8873

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 677c04f0
...@@ -761,6 +761,7 @@ export default async function build( ...@@ -761,6 +761,7 @@ export default async function build(
header: typeof RSC header: typeof RSC
varyHeader: typeof RSC_VARY_HEADER varyHeader: typeof RSC_VARY_HEADER
} }
skipMiddlewareUrlNormalize?: boolean
} = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => { } = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => {
const sortedRoutes = getSortedRoutes([ const sortedRoutes = getSortedRoutes([
...pageKeys.pages, ...pageKeys.pages,
...@@ -791,6 +792,8 @@ export default async function build( ...@@ -791,6 +792,8 @@ export default async function build(
header: RSC, header: RSC,
varyHeader: RSC_VARY_HEADER, varyHeader: RSC_VARY_HEADER,
}, },
skipMiddlewareUrlNormalize:
config.experimental.skipMiddlewareUrlNormalize,
} }
}) })
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
export default function handler(req) { export default function handler(req) {
console.log(req.nextUrl)
if (req.nextUrl.pathname.startsWith('/_next/data/missing-id')) { if (req.nextUrl.pathname.startsWith('/_next/data/missing-id')) {
console.log(`missing-id rewrite: ${req.nextUrl.toString()}`) console.log(`missing-id rewrite: ${req.nextUrl.toString()}`)
return NextResponse.rewrite('https://example.vercel.sh') 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)) 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)) return NextResponse.rewrite(new URL('/another', req.nextUrl))
} }
......
...@@ -35,6 +35,21 @@ describe('skip-trailing-slash-redirect', () => { ...@@ -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 () => { it('should allow response body from middleware with flag', async () => {
const res = await fetchViaHTTP(next.url, '/middleware-response-body') const res = await fetchViaHTTP(next.url, '/middleware-response-body')
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -88,7 +103,7 @@ describe('skip-trailing-slash-redirect', () => { ...@@ -88,7 +103,7 @@ describe('skip-trailing-slash-redirect', () => {
it('should correct skip URL normalizing in middleware', async () => { it('should correct skip URL normalizing in middleware', async () => {
let res = await fetchViaHTTP( let res = await fetchViaHTTP(
next.url, next.url,
'/middleware-rewrite-with-slash', `/_next/data/${next.buildId}/middleware-rewrite-with-slash.json`,
undefined, undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } } { redirect: 'manual', headers: { 'x-nextjs-data': '1' } }
) )
...@@ -96,7 +111,7 @@ describe('skip-trailing-slash-redirect', () => { ...@@ -96,7 +111,7 @@ describe('skip-trailing-slash-redirect', () => {
res = await fetchViaHTTP( res = await fetchViaHTTP(
next.url, next.url,
'/middleware-rewrite-without-slash', `/_next/data/${next.buildId}/middleware-rewrite-without-slash.json`,
undefined, undefined,
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } } { 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