Skip to content

Commit

Permalink
Fix middleware experimental trace case (#70372)
Browse files Browse the repository at this point in the history
This ensures we don't overwrite/copy stale middleware chunks with
experimental tracing.
  • Loading branch information
ijjk authored Sep 23, 2024
1 parent 17bdfd8 commit b6a695b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1776,9 +1776,12 @@ export default async function build(
{
filter(item) {
// we copy page chunks separately to not copy stale entries
return !item.match(/^[/\\](pages|app)[/\\]/)
return (
!item.startsWith('/middleware.js') &&
!item.match(/^[/\\](pages|app)[/\\]/)
)
},
overwrite: true,
overwrite: false,
}
)
}
Expand Down
13 changes: 8 additions & 5 deletions packages/next/src/lib/recursive-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ export async function recursiveCopy(
// we remove the base path (from) and replace \ by / (windows)
filter(item.replace(from, '').replace(/\\/g, '/'))
) {
await promises.copyFile(
item,
target,
overwrite ? undefined : COPYFILE_EXCL
)
await promises
.copyFile(item, target, overwrite ? undefined : COPYFILE_EXCL)
.catch((err) => {
// if overwrite is false we shouldn't fail on EEXIST
if (err.code !== 'EEXIST') {
throw err
}
})
sema.release()
} else {
sema.release()
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/app-dir/app/flying-shuttle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,28 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
await next.patchFile(dataPath, originalDataContent)
}
})

it('should have updated middleware on change', async () => {
await next.stop()

const dataPath = 'middleware.js'
const originalDataContent = await next.readFile(dataPath)

try {
await next.patchFile(
dataPath,
originalDataContent.replace(
`'x-flying-shuttle': '1'`,
`'x-flying-shuttle': '2'`
)
)
await nextStart()

const res = await next.fetch('/flying-shuttle')
expect(res.headers.get('x-flying-shuttle')).toBe('2')
} finally {
await next.patchFile(dataPath, originalDataContent)
}
})
}
)
8 changes: 8 additions & 0 deletions test/e2e/app-dir/app/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { NextResponse } from 'next/server'
* @returns {Promise<NextResponse | undefined>}
*/
export async function middleware(request) {
if (request.nextUrl.pathname === '/flying-shuttle') {
return NextResponse.next({
headers: {
'x-flying-shuttle': '1',
},
})
}

if (request.nextUrl.pathname === '/searchparams-normalization-bug') {
const headers = new Headers(request.headers)
headers.set('test', request.nextUrl.searchParams.get('val') || '')
Expand Down

0 comments on commit b6a695b

Please sign in to comment.