Repository navigation
Expand file tree
/
Copy pathmount-site-pages.mjs
More file actions
97 lines (91 loc) · 4.25 KB
/
Copy pathmount-site-pages.mjs
File metadata and controls
97 lines (91 loc) · 4.25 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
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { publishSiteDiscovery } from "./site-discovery.mjs";
const output = new URL("../.vercel/output/", import.meta.url);
const docs = new URL("../", import.meta.url);
const manifest = new URL("config.json", output);
const config = JSON.parse(readFileSync(manifest, "utf8"));
const publicPagesFile = new URL("./public-pages.json", import.meta.url);
const publicPages = new Map(Object.entries(JSON.parse(readFileSync(publicPagesFile, "utf8"))));
const homeLocales = JSON.parse(readFileSync(new URL("./home-locales.json", import.meta.url), "utf8"));
const productDates = JSON.parse(readFileSync(new URL(".blume-content/.product-dates.json", docs), "utf8"));
const adoptionDestinations = new Set(["/docs/adoption", "/docs/adoption/open-source"]);
const snapshotPath = "/api/oss-snapshot";
const telemetryFunctions = [
{ path: "/api/v1/adoption", handler: "adoption-collector.mjs", modules: [] },
{ path: snapshotPath, handler: "oss-snapshot.mjs", modules: ["oss-sources.mjs"] },
];
if (!Array.isArray(config.routes)) throw new Error("Vercel build manifest has no routes.");
const siteRoutes = [...publicPages].map(([path, destination]) => {
const src = path === "/" ? "^/$" : `^${path}/?$`;
if (adoptionDestinations.has(destination)) {
// Vercel resolves the destination without matching another route.
const adoption = config.routes.find(({ src, dest }) => src === `^${destination}$` && dest === "_render");
if (!adoption || !existsSync(new URL(`functions/${adoption.dest}.func/`, output))) {
throw new Error("Missing adoption render function.");
}
return { src, dest: adoption.dest };
}
const file = new URL(`static${destination}/index.html`, output);
if (!existsSync(file)) throw new Error(`Missing prerendered site page: ${fileURLToPath(file)}`);
return { src, dest: destination };
});
const telemetryRoutes = telemetryFunctions.map(({ path, handler, modules }) => {
const directory = new URL(`functions${path}.func/`, output);
mkdirSync(directory, { recursive: true });
for (const module of [handler, ...modules]) {
copyFileSync(new URL(`lib/${module}`, docs), new URL(module, directory));
}
const functionConfig = {
runtime: "nodejs24.x",
handler,
launcherType: "Nodejs",
maxDuration: 10,
supportsResponseStreaming: true,
};
writeFileSync(new URL(".vc-config.json", directory), JSON.stringify(functionConfig));
return { src: `^${path}$`, dest: path };
});
const agentIndexRedirects = ["", "-full"].map((suffix) => ({
src: `^/llms${suffix}\\.txt$`,
status: 308,
headers: { Location: `/docs/llms${suffix}.txt` },
}));
const aliasRedirects = [...publicPages].map(([path, destination]) => ({
src: `^${destination}/?$`,
status: 308,
headers: { Location: path },
}));
const translationRedirects = publishSiteDiscovery(new URL("static/", output), {
root: [...publicPages.keys()],
aliases: [...publicPages.values()],
homeLocales,
productDates,
});
const publicPaths = new Set(siteRoutes.map(({ src }) => src));
config.routes = [
...agentIndexRedirects,
...aliasRedirects,
...translationRedirects,
...telemetryRoutes,
...siteRoutes,
...config.routes.filter(({ src }) => !publicPaths.has(src)),
];
config.crons = [...(config.crons ?? []), { path: snapshotPath, schedule: "30 2 * * *" }];
copyFileSync(new URL("../llms-install.md", docs), new URL("static/docs/llms-install.md", output));
if (process.env.VERCEL_TARGET_ENV === "production") {
const revision = process.env.VERCEL_GIT_COMMIT_SHA;
if (!revision || !/^[0-9a-f]{40}$/.test(revision)) throw new Error("Invalid production revision.");
const wellKnown = new URL("static/.well-known/", output);
mkdirSync(wellKnown, { recursive: true });
writeFileSync(new URL("loom-revision.txt", wellKnown), `${revision}\n`);
config.routes.unshift({
src: "^/\\.well-known/loom-revision\\.txt$",
headers: { "Cache-Control": "no-store", "X-Robots-Tag": "noindex" },
continue: true,
});
}
writeFileSync(manifest, `${JSON.stringify(config, null, "\t")}\n`);
for (const file of readdirSync(docs).filter((name) => /^[0-9a-f]{32}\.txt$/.test(name))) {
copyFileSync(new URL(file, docs), new URL(`static/${file}`, output));
}