forked from opennextjs/opennextjs-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-manifest.ts
More file actions
131 lines (118 loc) · 4.12 KB
/
load-manifest.ts
File metadata and controls
131 lines (118 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Inline `loadManifest` and `evalManifest` from `load-manifest.js`
*
* They rely on `readFileSync` that is not supported by workerd.
*/
import { readFile } from "node:fs/promises";
import { join, posix, relative, sep } from "node:path";
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { patchCode, type RuleConfig } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
import { glob } from "glob";
import { normalizePath } from "../../../utils/normalize-path.js";
export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOptions): Plugin {
return updater.updateContent("inline-load-manifest", [
{
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/load-manifest(\.external)?\.js$`, {
escape: false,
}),
contentFilter: /function loadManifest\(/,
callback: async ({ contents }) => {
contents = await patchCode(contents, await getLoadManifestRule(buildOpts));
contents = await patchCode(contents, await getEvalManifestRule(buildOpts));
return contents;
},
},
]);
}
async function getLoadManifestRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
const dotNextDir = join(baseDir, ".next");
const manifests = await glob(join(dotNextDir, "**/{*-manifest,required-server-files}.json"), {
windowsPathsNoEscape: true,
});
const returnManifests = (
await Promise.all(
manifests.map(
async (manifest) => `
if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
return ${await readFile(manifest, "utf-8")};
}`
)
)
).join("\n");
return {
rule: {
pattern: `
function loadManifest($PATH, $$$ARGS) {
$$$_
}`,
},
fix: `
function loadManifest($PATH, $$$ARGS) {
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
if ($PATH.endsWith(".next/BUILD_ID")) {
return process.env.NEXT_BUILD_ID;
}
${returnManifests}
// Known optional manifests \u2014 Next.js loads these with handleMissing: true
// (see vercel/next.js packages/next/src/server/route-modules/route-module.ts).
// Return {} to match Next.js behaviour instead of crashing the worker.
if ($PATH.endsWith("react-loadable-manifest.json") ||
$PATH.endsWith("subresource-integrity-manifest.json") ||
$PATH.endsWith("server-reference-manifest.json") ||
$PATH.endsWith("dynamic-css-manifest.json") ||
$PATH.endsWith("fallback-build-manifest.json")) {
return {};
}
throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}
async function getEvalManifestRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next");
const appDir = join(baseDir, "server/app");
const manifests = await glob(join(baseDir, "**/*_client-reference-manifest.js"), {
windowsPathsNoEscape: true,
});
const returnManifests = manifests
.map((manifest) => {
const endsWith = normalizePath(relative(baseDir, manifest));
const key = normalizePath("/" + relative(appDir, manifest)).replace(
"_client-reference-manifest.js",
""
);
return `
if ($PATH.endsWith("${endsWith}")) {
require(${JSON.stringify(manifest)});
return {
__RSC_MANIFEST: {
"${key}": globalThis.__RSC_MANIFEST["${key}"],
},
};
}`;
})
.join("\n");
return {
rule: {
pattern: `
function evalManifest($PATH, $$$ARGS) {
$$$_
}`,
},
fix: `
function evalManifest($PATH, $$$ARGS) {
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
${returnManifests}
// client-reference-manifest is optional for static metadata routes
// (see vercel/next.js route-module.ts, loaded with handleMissing: true)
if ($PATH.endsWith("_client-reference-manifest.js")) {
return { __RSC_MANIFEST: {} };
}
throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}