Skip to content

Commit

Permalink
Ensure skip normalize is handled correctly (#42642)
Browse files Browse the repository at this point in the history
x-ref: [slack
thread](https://vercel.slack.com/archives/C01224Q5M99/p1667927545637489?thread_ts=1664536480.045539&cid=C01224Q5M99)
x-ref: vercel/vercel#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)
  • Loading branch information
ijjk committed Nov 8, 2022
1 parent 677c04f commit 85b200a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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

0 comments on commit 85b200a

Please sign in to comment.