Repository navigation
Release v3.8.9 — final sync (static-asset fix + contributor credits) #3135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
49dedec
796267d
6ce96cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -283,11 +283,13 @@ | |||||||||||||||||||||||||||||||
| * @param {string} outDir - assembled standalone output directory | ||||||||||||||||||||||||||||||||
| * @returns {number} number of path replacements made | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export function assemblePathSanitize(projectRoot, outDir) { | ||||||||||||||||||||||||||||||||
| export function assemblePathSanitize(projectRoot, outDir, distDir = ".next") { | ||||||||||||||||||||||||||||||||
| const buildRoot = projectRoot.replace(/\\/g, "/"); // normalise for regex safety | ||||||||||||||||||||||||||||||||
|
Check warning on line 287 in scripts/build/assembleStandalone.mjs
|
||||||||||||||||||||||||||||||||
| const sanitizeTargets = [ | ||||||||||||||||||||||||||||||||
| path.join(outDir, "server.js"), | ||||||||||||||||||||||||||||||||
| path.join(outDir, ".next", "required-server-files.json"), | ||||||||||||||||||||||||||||||||
| // required-server-files.json lives under the distDir (e.g. .build/next), not | ||||||||||||||||||||||||||||||||
| // a literal .next — the standalone preserves the configured distDir path. | ||||||||||||||||||||||||||||||||
| path.join(outDir, distDir, "required-server-files.json"), | ||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let sanitisedCount = 0; | ||||||||||||||||||||||||||||||||
|
|
@@ -295,7 +297,7 @@ | |||||||||||||||||||||||||||||||
| if (!fsSync.existsSync(filePath)) continue; | ||||||||||||||||||||||||||||||||
| let content = fsSync.readFileSync(filePath, "utf8"); | ||||||||||||||||||||||||||||||||
| // Escape special regex characters in the path | ||||||||||||||||||||||||||||||||
| const escaped = buildRoot.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||||||||||||||||||||||||||||||||
|
Check warning on line 300 in scripts/build/assembleStandalone.mjs
|
||||||||||||||||||||||||||||||||
| const re = new RegExp(escaped, "g"); | ||||||||||||||||||||||||||||||||
| const matches = content.match(re); | ||||||||||||||||||||||||||||||||
| if (matches) { | ||||||||||||||||||||||||||||||||
|
|
@@ -316,8 +318,8 @@ | |||||||||||||||||||||||||||||||
| * @param {string} outDir - assembled standalone output directory | ||||||||||||||||||||||||||||||||
| * @returns {{ patchedFiles: number, patchedMatches: number }} | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export function patchTurbopackChunks(outDir) { | ||||||||||||||||||||||||||||||||
| const serverOutput = path.join(outDir, ".next", "server"); | ||||||||||||||||||||||||||||||||
| export function patchTurbopackChunks(outDir, distDir = ".next") { | ||||||||||||||||||||||||||||||||
| const serverOutput = path.join(outDir, distDir, "server"); | ||||||||||||||||||||||||||||||||
| const HASH_RE = /(['"\\])([a-z@][a-z0-9@./_-]+?-[0-9a-f]{16}(?:\/[^'"\\]+)?)\1/g; | ||||||||||||||||||||||||||||||||
| let patchedFiles = 0; | ||||||||||||||||||||||||||||||||
| let patchedMatches = 0; | ||||||||||||||||||||||||||||||||
|
|
@@ -381,7 +383,7 @@ | |||||||||||||||||||||||||||||||
| * @param {boolean} [opts.copyNatives] - copy native assets + extra modules (default true) | ||||||||||||||||||||||||||||||||
| * @returns {void} | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export function assembleStandalone({ | ||||||||||||||||||||||||||||||||
|
Check failure on line 386 in scripts/build/assembleStandalone.mjs
|
||||||||||||||||||||||||||||||||
| distDir, | ||||||||||||||||||||||||||||||||
| outDir, | ||||||||||||||||||||||||||||||||
| projectRoot = process.cwd(), | ||||||||||||||||||||||||||||||||
|
|
@@ -392,6 +394,12 @@ | |||||||||||||||||||||||||||||||
| if (!distDir) throw new Error("[assembleStandalone] distDir is required"); | ||||||||||||||||||||||||||||||||
| if (!outDir) throw new Error("[assembleStandalone] outDir is required"); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // The standalone bundle preserves the distDir path RELATIVE to projectRoot | ||||||||||||||||||||||||||||||||
| // (the server's baked config uses e.g. "./.build/next"), so output dest paths | ||||||||||||||||||||||||||||||||
| // for static / required-server-files / server chunks must use the relative | ||||||||||||||||||||||||||||||||
| // distDir appended to outDir — never the absolute build-machine distDir. | ||||||||||||||||||||||||||||||||
| const relDistDir = path.isAbsolute(distDir) ? path.relative(projectRoot, distDir) : distDir; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const standaloneDir = path.resolve(path.join(distDir, "standalone")); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+573
to
579
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If We should resolve
Suggested change
|
||||||||||||||||||||||||||||||||
| const resolvedOutDir = path.resolve(outDir); | ||||||||||||||||||||||||||||||||
| if (!fsSync.existsSync(standaloneDir)) { | ||||||||||||||||||||||||||||||||
|
|
@@ -425,9 +433,13 @@ | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 2. Copy <distDir>/static -> resolvedOutDir/.next/static | ||||||||||||||||||||||||||||||||
| // 2. Copy <distDir>/static -> resolvedOutDir/<distDir>/static | ||||||||||||||||||||||||||||||||
| // CRITICAL: the standalone server.js is built with distDir baked into its config | ||||||||||||||||||||||||||||||||
| // (e.g. "./.build/next"), so it serves /_next/static from <outDir>/<distDir>/static, | ||||||||||||||||||||||||||||||||
| // NOT a literal <outDir>/.next/static. Copying to .next/static leaves the server's | ||||||||||||||||||||||||||||||||
| // static dir empty → every JS/CSS chunk 404s → blank page. Mirror the distDir path. | ||||||||||||||||||||||||||||||||
| const staticSrc = path.join(distDir, "static"); | ||||||||||||||||||||||||||||||||
| const staticDest = path.join(resolvedOutDir, ".next", "static"); | ||||||||||||||||||||||||||||||||
| const staticDest = path.join(resolvedOutDir, relDistDir, "static"); | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure correct path resolution when
Suggested change
|
||||||||||||||||||||||||||||||||
| if (fsSync.existsSync(staticSrc)) { | ||||||||||||||||||||||||||||||||
| fsSync.mkdirSync(path.dirname(staticDest), { recursive: true }); | ||||||||||||||||||||||||||||||||
| fsSync.cpSync(staticSrc, staticDest, { recursive: true, force: true }); | ||||||||||||||||||||||||||||||||
|
|
@@ -442,15 +454,15 @@ | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 4. Optionally sanitize abs paths | ||||||||||||||||||||||||||||||||
| if (sanitizePaths) { | ||||||||||||||||||||||||||||||||
| const count = assemblePathSanitize(projectRoot, resolvedOutDir); | ||||||||||||||||||||||||||||||||
| const count = assemblePathSanitize(projectRoot, resolvedOutDir, relDistDir); | ||||||||||||||||||||||||||||||||
| if (count > 0) { | ||||||||||||||||||||||||||||||||
| console.log(`[assembleStandalone] Sanitised ${count} hardcoded path references`); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 5. Optionally patch Turbopack hashed chunks | ||||||||||||||||||||||||||||||||
| if (doPatchChunks) { | ||||||||||||||||||||||||||||||||
| const { patchedFiles, patchedMatches } = patchTurbopackChunks(resolvedOutDir); | ||||||||||||||||||||||||||||||||
| const { patchedFiles, patchedMatches } = patchTurbopackChunks(resolvedOutDir, relDistDir); | ||||||||||||||||||||||||||||||||
| if (patchedMatches > 0) { | ||||||||||||||||||||||||||||||||
| console.log( | ||||||||||||||||||||||||||||||||
| `[assembleStandalone] Hash-strip: patched ${patchedMatches} hashed require() in ${patchedFiles} server chunk file(s)` | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
distDiris passed as an absolute path,path.join(outDir, distDir, ...)will produce an incorrect path (e.g.,outDir/absolute/path/...), causingfsSync.existsSyncto fail and silently skipping the sanitization ofrequired-server-files.json.Since
assemblePathSanitizehas access toprojectRoot, we can defensively normalizedistDirto be relative toprojectRootif it is absolute.