返回 CodeWhale
next.config.ts
根目录 / web / next.config.ts
1 import type { NextConfig } from "next";
2 import { execFileSync } from "node:child_process";
3 import { dirname, resolve } from "node:path";
4 import { fileURLToPath } from "node:url";
5
6 const webRoot = dirname(fileURLToPath(import.meta.url));
7 const repoRoot = resolve(webRoot, "..");
8
9 function git(args: string[]): string | null {
10 try {
11 return execFileSync("git", args, {
12 cwd: repoRoot,
13 encoding: "utf8",
14 stdio: ["ignore", "pipe", "ignore"],
15 }).trim() || null;
16 } catch {
17 return null;
18 }
19 }
20
21 const requestedSourceRevision =
22 process.env.CODEWHALE_SOURCE_REVISION?.trim() ||
23 process.env.GITHUB_SHA?.trim();
24 const gitSourceRevision = git(["rev-parse", "HEAD"]);
25 const sourceRevision = /^[0-9a-f]{40}$/i.test(requestedSourceRevision ?? "")
26 ? requestedSourceRevision ?? ""
27 : /^[0-9a-f]{40}$/i.test(gitSourceRevision ?? "")
28 ? gitSourceRevision ?? ""
29 : "";
30 const sourceCommittedAt =
31 process.env.CODEWHALE_SOURCE_COMMITTED_AT?.trim() ||
32 (sourceRevision ? git(["show", "-s", "--format=%cI", sourceRevision]) : null) ||
33 "";
34
35 const factsGeneratedAt = new Date().toISOString();
36
37 // Security headers are set in middleware.ts (more reliable under OpenNext on
38 // Cloudflare than next.config.ts headers(), which doesn't always apply to
39 // prerendered/cached responses).
40 const nextConfig: NextConfig = {
41 outputFileTracingRoot: webRoot,
42 poweredByHeader: false,
43 reactStrictMode: true,
44 // Public, non-secret provenance for the exact source used to build the
45 // deployed worker. Next inlines these values into server output, allowing a
46 // credential-free post-deploy comparison without dirtying tracked facts.
47 env: {
48 NEXT_PUBLIC_CODEWHALE_SOURCE_REVISION: sourceRevision,
49 NEXT_PUBLIC_CODEWHALE_SOURCE_COMMITTED_AT: sourceCommittedAt,
50 NEXT_PUBLIC_CODEWHALE_FACTS_GENERATED_AT: factsGeneratedAt,
51 },
52 images: {
53 remotePatterns: [
54 { protocol: "https", hostname: "avatars.githubusercontent.com" },
55 ],
56 },
57 typedRoutes: false,
58 };
59
60 export default nextConfig;
61
62 if (process.env.NODE_ENV === "development") {
63 // Initialize Cloudflare bindings (KV, etc.) when running `next dev`.
64 // No-op in production builds.
65 void import("@opennextjs/cloudflare").then(({ initOpenNextCloudflareForDev }) => {
66 initOpenNextCloudflareForDev({ configPath: ".cloudflare/opennext.json" });
67 }).catch(() => { /* dev-only convenience */ });
68 }
69
69 lines TYPESCRIPT