Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure skip normalize is handled correctly #42642

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -761,6 +761,7 @@ export default async function build(
header: typeof RSC
varyHeader: typeof RSC_VARY_HEADER
}
skipMiddlewareUrlNormalize?: boolean
} = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => {
const sortedRoutes = getSortedRoutes([
...pageKeys.pages,
Expand Down Expand Up @@ -791,6 +792,8 @@ export default async function build(
header: RSC,
varyHeader: RSC_VARY_HEADER,
},
skipMiddlewareUrlNormalize:
config.experimental.skipMiddlewareUrlNormalize,
}
})

Expand Down
13 changes: 11 additions & 2 deletions test/e2e/skip-trailing-slash-redirect/app/middleware.js
@@ -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
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