Skip to content

Commit c6af678

Browse files
authoredFeb 16, 2024
fix: don't run middleware on rewrite target (#270)
1 parent 6a35de6 commit c6af678

File tree

7 files changed

+250
-2
lines changed

7 files changed

+250
-2
lines changed
 

‎edge-runtime/lib/response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export const buildResponse = async ({
190190
}
191191
res.headers.set('x-middleware-rewrite', relativeUrl)
192192
request.headers.set('x-middleware-rewrite', target)
193-
return addMiddlewareHeaders(fetch(new Request(target, request), { redirect: 'manual' }), res)
193+
return addMiddlewareHeaders(context.rewrite(target), res)
194194
}
195195

196196
// If we are redirecting a request that had a locale in the URL, we need to add it back in

‎package-lock.json

+189
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/e2e/edge-middleware.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,21 @@ test('Does not run edge middleware at the origin', async ({ page, middleware })
2323
await expect(h1).toHaveText('Message from middleware: hello')
2424
})
2525

26+
test('does not run middleware again for rewrite target', async ({ page, middleware }) => {
27+
const direct = await page.goto(`${middleware.url}/test/rewrite-target`)
28+
expect(await direct?.headerValue('x-added-rewrite-target')).toBeTruthy()
29+
30+
const rewritten = await page.goto(`${middleware.url}/test/rewrite-loop-detect`)
31+
32+
expect(await rewritten?.headerValue('x-added-rewrite-target')).toBeNull()
33+
const h1 = page.locator('h1')
34+
await expect(h1).toHaveText('Hello rewrite')
35+
})
36+
2637
test('Supports CJS dependencies in Edge Middleware', async ({ page, middleware }) => {
2738
const res = await page.goto(`${middleware.url}/test/next`)
2839

29-
expect(await res?.headerValue('x-cjs-module-works')).toEqual("true")
40+
expect(await res?.headerValue('x-cjs-module-works')).toEqual('true')
3041
})
3142

3243
// adaptation of https://github.com/vercel/next.js/blob/8aa9a52c36f338320d55bd2ec292ffb0b8c7cb35/test/e2e/app-dir/metadata-edge/index.test.ts#L24C5-L31C7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Rewrite() {
2+
return (
3+
<main>
4+
<h1>Hello rewrite</h1>
5+
</main>
6+
)
7+
}

‎tests/fixtures/middleware/middleware.ts

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ const getResponse = (request: NextRequest) => {
4545
})
4646
}
4747

48+
if (request.nextUrl.pathname === '/test/rewrite-target') {
49+
const response = NextResponse.next()
50+
response.headers.set('x-added-rewrite-target', 'true')
51+
return response
52+
}
53+
4854
if (request.nextUrl.pathname === '/test/rewrite-internal') {
4955
return NextResponse.rewrite(new URL('/rewrite-target', request.url), {
5056
request: {
@@ -53,6 +59,14 @@ const getResponse = (request: NextRequest) => {
5359
})
5460
}
5561

62+
if (request.nextUrl.pathname === '/test/rewrite-loop-detect') {
63+
return NextResponse.rewrite(new URL('/test/rewrite-target', request.url), {
64+
request: {
65+
headers: requestHeaders,
66+
},
67+
})
68+
}
69+
5670
if (request.nextUrl.pathname === '/test/rewrite-external') {
5771
const requestURL = new URL(request.url)
5872
const externalURL = new URL(requestURL.searchParams.get('external-url') as string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cool

‎tests/integration/edge-handler.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,32 @@ describe('page router', () => {
334334
expect(response.status).toBe(200)
335335
})
336336

337+
test<FixtureTestContext>('middleware should leave non-data requests untouched', async (ctx) => {
338+
await createFixture('middleware-pages', ctx)
339+
await runPlugin(ctx)
340+
const origin = await LocalServer.run(async (req, res) => {
341+
res.write(
342+
JSON.stringify({
343+
url: req.url,
344+
headers: req.headers,
345+
}),
346+
)
347+
res.end()
348+
})
349+
ctx.cleanup?.push(() => origin.stop())
350+
const response = await invokeEdgeFunction(ctx, {
351+
functions: ['___netlify-edge-handler-middleware'],
352+
origin,
353+
url: `/_next/static/build-id/_devMiddlewareManifest.json?foo=1`,
354+
})
355+
const res = await response.json()
356+
const url = new URL(res.url, 'http://n/')
357+
expect(url.pathname).toBe('/_next/static/build-id/_devMiddlewareManifest.json')
358+
expect(url.search).toBe('?foo=1')
359+
expect(res.headers['x-nextjs-data']).toBeUndefined()
360+
expect(response.status).toBe(200)
361+
})
362+
337363
test<FixtureTestContext>('should rewrite un-rewritten data requests to page route', async (ctx) => {
338364
await createFixture('middleware-pages', ctx)
339365
await runPlugin(ctx)

0 commit comments

Comments
 (0)
Please sign in to comment.