Commit 7780025a authored by Jiwon Choi's avatar Jiwon Choi Committed by Jiachi Liu

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: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 2eda33ce
...@@ -4174,6 +4174,34 @@ export default async function build( ...@@ -4174,6 +4174,34 @@ export default async function build(
path.join(distDir, SERVER_DIRECTORY, 'proxy.js.nft.json'), path.join(distDir, SERVER_DIRECTORY, 'proxy.js.nft.json'),
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json') path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json')
) )
const middlewareNft = JSON.parse(
await fs.readFile(
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json'),
'utf8'
)
)
// When Proxy self-reference itself e.g. __filename, it is traced to
// the NFT file. However, since we rename 'proxy.js' to 'middleware.js',
// the files in NFT will differ from the actual outputs, which will fail
// for the providers like Vercel that uses NFT. Therefore also rename
// the 'proxy.js' to 'middleware.js' in the NFT file.
let hasProxyJsInNft = false
middlewareNft.files = middlewareNft.files.map((file: string) => {
if (file === 'proxy.js') {
hasProxyJsInNft = true
return 'middleware.js'
}
return file
})
if (hasProxyJsInNft) {
await fs.writeFile(
path.join(distDir, SERVER_DIRECTORY, 'middleware.js.nft.json'),
JSON.stringify(middlewareNft)
)
}
} }
if (isCompileMode) { if (isCompileMode) {
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import fs from 'fs'
import path from 'path'
// This test verifies a case when the "proxy.ts" bundle is being traced into the NFT file as "proxy.js".
// As Next.js renames "proxy.js" to "middleware.js" during webpack build, the files in NFT will differ
// from the actual outputs, which will fail for the providers like Vercel that checks for the files in NFT.
describe('proxy-nfc-traced', () => {
const { next, isTurbopack, isNextStart } = nextTestSetup({
files: __dirname,
})
// 'middleware.js' won't be traced because Turbopack doesn't bundle all code to .next/server/middleware.js
if (isNextStart && !isTurbopack) {
it('should have renamed trace file as middleware instead of proxy', async () => {
const nfc = JSON.parse(
fs.readFileSync(
path.join(next.testDir, '.next/server/middleware.js.nft.json'),
'utf-8'
)
)
expect(nfc.files).toContain('middleware.js')
expect(nfc.files).not.toContain('proxy.js')
})
}
// Previously, the deployment tests failed because of the traced file name mismatch.
it('should successfully build and be redirected from proxy', async () => {
const $ = await next.render$('/home')
expect($('p').text()).toBe('hello world')
})
})
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
export default function proxy(request: NextRequest) {
if (request.nextUrl.pathname === '/home') {
return NextResponse.redirect(new URL('/', request.url))
}
// `__filename` included in the bundle makes the NFT to trace it.
// This will result creating "proxy.js" to be traced into the NFT file.
// However, as Next.js renames "proxy.js" to "middleware.js" during build,
// the files in NFT will differ from the actual outputs, which will fail for
// the providers like Vercel that checks for the files in NFT.
console.log(__filename)
return NextResponse.next()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment