Repository navigation
Expand file tree
/
Copy pathnext.config.ts
More file actions
247 lines (226 loc) · 8.46 KB
/
Copy pathnext.config.ts
File metadata and controls
247 lines (226 loc) · 8.46 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import type { NextConfig } from "next";
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
import { getBuildRevision } from "./scripts/build-revision.mjs";
if (process.env.NODE_ENV === "development") {
initOpenNextCloudflareForDev();
}
// Same constant the sync script emits asset URLs against, so the allowlist
// below can never drift away from the host those URLs actually use.
import { SITE_ORIGIN as VIN_TOGETHER_ORIGIN } from "./scripts/vin-together-site.js";
type RemotePattern = NonNullable<NonNullable<NextConfig["images"]>["remotePatterns"]>[number];
function getAzureBaseUrlFromEnv() {
const explicitBaseUrl = process.env.AZURE_BLOB_PUBLIC_BASE_URL?.trim();
if (explicitBaseUrl) {
return explicitBaseUrl;
}
const container = process.env.AZURE_STORAGE_CONTAINER?.trim();
if (!container) {
return "";
}
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING?.trim();
if (connectionString) {
const segments = new Map<string, string>();
for (const part of connectionString.split(";")) {
const trimmed = part.trim();
if (!trimmed) {
continue;
}
const separatorIndex = trimmed.indexOf("=");
if (separatorIndex === -1) {
continue;
}
const key = trimmed.slice(0, separatorIndex).trim();
const value = trimmed.slice(separatorIndex + 1).trim();
segments.set(key, value);
}
const blobEndpoint = segments.get("BlobEndpoint");
if (blobEndpoint) {
return `${blobEndpoint.replace(/\/+$/, "")}/${container}`;
}
const accountName = segments.get("AccountName");
const endpointSuffix = segments.get("EndpointSuffix") || "core.windows.net";
if (accountName) {
return `https://${accountName}.blob.${endpointSuffix}/${container}`;
}
}
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME?.trim();
if (!accountName) {
return "";
}
return `https://${accountName}.blob.core.windows.net/${container}`;
}
function buildRemotePattern(baseUrl: string): RemotePattern | null {
if (!baseUrl) {
return null;
}
try {
const parsed = new URL(baseUrl);
const protocol =
parsed.protocol === "http:" || parsed.protocol === "https:"
? (parsed.protocol.replace(/:$/, "") as "http" | "https")
: null;
if (!protocol) {
return null;
}
const pathnameBase = parsed.pathname.replace(/\/+$/, "");
return {
protocol,
hostname: parsed.hostname,
pathname: pathnameBase ? `${pathnameBase}/**` : "/**",
};
} catch {
return null;
}
}
const configuredAzureBaseUrl = getAzureBaseUrlFromEnv();
const fallbackAzureBaseUrls = [
"https://examcookerdevsi.blob.core.windows.net/exam-assets",
"https://examcookerprodsi.blob.core.windows.net/exam-assets",
];
// VInTogether course assets (topic "Visual" diagrams, PDFs) are served from
// this origin. Without it in the allowlist next/image rejects every optimizer
// request and diagram-only blocks render as an empty box.
const vinTogetherRemotePattern = buildRemotePattern(VIN_TOGETHER_ORIGIN);
const configuredRemotePatterns = Array.from(
new Map(
[configuredAzureBaseUrl, ...fallbackAzureBaseUrls]
.map((baseUrl) => baseUrl.trim())
.filter(Boolean)
.map((baseUrl) => [baseUrl, buildRemotePattern(baseUrl)]),
).values(),
).filter((pattern): pattern is RemotePattern => pattern !== null);
// Deployment builds upload browser maps to PostHog and remove them before packaging.
const uploadSourceMaps = process.env.POSTHOG_SOURCEMAP_UPLOAD === "true";
const nextConfig: NextConfig = {
env: {
NEXT_PUBLIC_APP_REVISION: getBuildRevision(),
NEXT_PUBLIC_HOSTING_BACKEND: process.env.EC_CLOUDFLARE_BUILD === "1" ? "cloudflare" : "azure",
NEXT_PUBLIC_ASSET_BASE_URL: process.env.EC_CLOUDFLARE_BUILD === "1"
? "https://ec-assets.acmvit.in"
: "",
},
headers() {
return [{
source: "/vendor/embedpdf/immutable/:path*",
headers: [{ key: "Cache-Control", value: "public, max-age=31536000, immutable" }],
}];
},
output: "standalone",
// pg loads this transport only inside Workers; Node's build trace misses it.
outputFileTracingIncludes: {
"/*": ["./node_modules/pg-cloudflare/**/*"],
},
productionBrowserSourceMaps: uploadSourceMaps,
cacheComponents: true,
partialPrefetching: true,
compiler: {
removeConsole:
process.env.NODE_ENV === "production"
? {
exclude: ["error", "warn"],
}
: false,
},
experimental: {
agentUpgrade: "security",
// Next inflates PPR state with a 5x output limit. Its 100 MB default
// exceeds Workers' 128 MiB zlib limit even for tiny cached payloads.
maxPostponedStateSize: "5mb",
instantInsights: {
validationLevel: "warning",
},
serverActions: {
bodySizeLimit: "10mb",
},
useTypeScriptCli: true,
},
turbopack: {
root: __dirname,
resolveAlias: {
// Workers use AppState Durable Objects/R2. Exclude the unused Node
// Redis/Entra SDK graph from this deployment's server bundle.
...(process.env.EC_CLOUDFLARE_BUILD === "1" ? {
"@/lib/redis": "./cloudflare/redis-unavailable.ts",
} : {}),
canvas: {
browser: "./lib/shims/canvas",
},
},
},
serverExternalPackages: ["@azure/identity", "canvas"],
images: {
formats: ["image/avif", "image/webp"],
minimumCacheTTL: 60 * 60 * 24 * 30,
deviceSizes: [360, 414, 640, 750, 828, 1080, 1200, 1920],
imageSizes: [16, 24, 32, 48, 64, 96, 128, 256, 384],
remotePatterns: [
{
protocol: "https",
hostname: "storage.googleapis.com",
pathname: "/**",
},
{
protocol: "https",
hostname: "i.ytimg.com",
pathname: "/vi/**",
},
{
protocol: "https",
hostname: "www.everything-assistant.com",
pathname: "/onboarding-artwork/**",
},
...(vinTogetherRemotePattern ? [vinTogetherRemotePattern] : []),
...configuredRemotePatterns,
],
},
async redirects() {
return [
{ source: "/courses", destination: "/past_papers", permanent: true },
{ source: "/courses/:code", destination: "/past_papers/:code", permanent: true },
{
source: "/courses/:code/:exam",
destination: "/past_papers/:code/:exam",
permanent: true,
},
];
},
async rewrites() {
const proxyPath =
process.env.NEXT_PUBLIC_POSTHOG_PROXY_PATH?.trim() || "/ecp";
const normalizedProxyPath = proxyPath.startsWith("/")
? proxyPath
: `/${proxyPath}`;
const posthogHost =
process.env.NEXT_PUBLIC_POSTHOG_HOST?.trim() ||
"https://eu.i.posthog.com";
const normalizedPosthogHost = posthogHost.replace(/\/$/, "");
const isUsRegion = normalizedPosthogHost.includes("us.i.posthog.com");
const isEuRegion = normalizedPosthogHost.includes("eu.i.posthog.com");
const assetsHost = isUsRegion
? "https://us-assets.i.posthog.com"
: isEuRegion
? "https://eu-assets.i.posthog.com"
: normalizedPosthogHost;
return [
{
source: `${normalizedProxyPath}/static/:path*`,
destination: `${assetsHost}/static/:path*`,
},
{
source: `${normalizedProxyPath}/array/:path*`,
destination: `${assetsHost}/array/:path*`,
},
{
source: `${normalizedProxyPath}/:path*`,
destination: `${normalizedPosthogHost}/:path*`,
},
];
},
webpack(config, { dev, isServer }) {
if (dev && !isServer) {
config.devtool = "cheap-module-source-map";
}
return config;
},
};
export default nextConfig;