返回 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 // Security headers are set in middleware.ts (more reliable under OpenNext on
36 // Cloudflare than next.config.ts headers(), which doesn't always apply to
37 // prerendered/cached responses).
38 const nextConfig: NextConfig = {
39 outputFileTracingRoot: webRoot,
40 reactStrictMode: true,
41 // Public, non-secret provenance for the exact source used to build the
42 // deployed worker. Next inlines these values into server output, allowing a
43 // credential-free post-deploy comparison without dirtying tracked facts.
44 env: {
45 NEXT_PUBLIC_CODEWHALE_SOURCE_REVISION: sourceRevision,
46 NEXT_PUBLIC_CODEWHALE_SOURCE_COMMITTED_AT: sourceCommittedAt,
47 },
48 images: {
49 remotePatterns: [
50 { protocol: "https", hostname: "avatars.githubusercontent.com" },
51 ],
52 },
53 typedRoutes: false,
54 };
55
56 export default nextConfig;
57
58 if (process.env.NODE_ENV === "development") {
59 // Initialize Cloudflare bindings (KV, etc.) when running `next dev`.
60 // No-op in production builds.
61 void import("@opennextjs/cloudflare").then(({ initOpenNextCloudflareForDev }) => {
62 initOpenNextCloudflareForDev();
63 }).catch(() => { /* dev-only convenience */ });
64 }
65
65 lines TYPESCRIPT