Skip to content

Commit ce90e19

Browse files
fix: Rename proxy.js to middleware.js in NFT file (#86214)
### Why? When Proxy self-references itself, e.g., `__filename`, it is traced to the NFT file. However, since Next.js renames 'proxy.js' to 'middleware.js' during webpack build, the files in NFT will differ from the actual outputs. This can cause a problem when verifying the NFT. Current Behavior: https://github.com/vercel/next.js/actions/runs/19483162554/job/55760669267?pr=86214#step:34:91 ### How? Therefore, also rename the self-reference of `'proxy.js'` in `middleware.js.nft.json` to `'middleware.js'`. I don't expect other modules to be referencing the build output of Proxy, that is not recommended. Closes NEXT-4777 Closes NAR-518 --------- Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
1 parent cab4fc6 commit ce90e19

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

packages/next/src/build/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,6 +4079,34 @@ export default async function build(
40794079
path.join(distDir, SERVER_DIRECTORY, 'proxy.js.nft.json'),
40804080
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json')
40814081
)
4082+
4083+
const middlewareNft = JSON.parse(
4084+
await fs.readFile(
4085+
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json'),
4086+
'utf8'
4087+
)
4088+
)
4089+
4090+
// When Proxy self-reference itself e.g. __filename, it is traced to
4091+
// the NFT file. However, since we rename 'proxy.js' to 'middleware.js',
4092+
// the files in NFT will differ from the actual outputs, which will fail
4093+
// for the providers like Vercel that uses NFT. Therefore also rename
4094+
// the 'proxy.js' to 'middleware.js' in the NFT file.
4095+
let hasProxyJsInNft = false
4096+
middlewareNft.files = middlewareNft.files.map((file: string) => {
4097+
if (file === 'proxy.js') {
4098+
hasProxyJsInNft = true
4099+
return 'middleware.js'
4100+
}
4101+
return file
4102+
})
4103+
4104+
if (hasProxyJsInNft) {
4105+
await fs.writeFile(
4106+
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json'),
4107+
JSON.stringify(middlewareNft)
4108+
)
4109+
}
40824110
}
40834111

40844112
if (isCompileMode) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
import fs from 'fs'
3+
import path from 'path'
4+
5+
// This test verifies a case when the "proxy.ts" bundle is being traced into the NFT file as "proxy.js".
6+
// As Next.js renames "proxy.js" to "middleware.js" during webpack build, the files in NFT will differ
7+
// from the actual outputs, which will fail for the providers like Vercel that checks for the files in NFT.
8+
9+
describe('proxy-nfc-traced', () => {
10+
const { next, isTurbopack, isNextStart } = nextTestSetup({
11+
files: __dirname,
12+
})
13+
14+
// 'middleware.js' won't be traced because Turbopack doesn't bundle all code to .next/server/middleware.js
15+
if (isNextStart && !isTurbopack) {
16+
it('should have renamed trace file as middleware instead of proxy', async () => {
17+
const nfc = JSON.parse(
18+
fs.readFileSync(
19+
path.join(next.testDir, '.next/server/middleware.js.nft.json'),
20+
'utf-8'
21+
)
22+
)
23+
expect(nfc.files).toContain('middleware.js')
24+
expect(nfc.files).not.toContain('proxy.js')
25+
})
26+
}
27+
28+
// Previously, the deployment tests failed because of the traced file name mismatch.
29+
it('should successfully build and be redirected from proxy', async () => {
30+
const $ = await next.render$('/home')
31+
expect($('p').text()).toBe('hello world')
32+
})
33+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export default function proxy(request: NextRequest) {
5+
if (request.nextUrl.pathname === '/home') {
6+
return NextResponse.redirect(new URL('/', request.url))
7+
}
8+
9+
// `__filename` included in the bundle makes the NFT to trace it.
10+
// This will result creating "proxy.js" to be traced into the NFT file.
11+
// However, as Next.js renames "proxy.js" to "middleware.js" during build,
12+
// the files in NFT will differ from the actual outputs, which will fail for
13+
// the providers like Vercel that checks for the files in NFT.
14+
console.log(__filename)
15+
16+
return NextResponse.next()
17+
}

0 commit comments

Comments
 (0)