-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
369 lines (331 loc) · 12.2 KB
/
server.js
File metadata and controls
369 lines (331 loc) · 12.2 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// @ts-check
/**
* server.js
* Minimal Express server to serve a React-Router prerendered SPA build
*
* Features:
* - Loads PORT from Vite's loadEnv (so values from .env.* are available)
* - rate limiting (global window)
* - compression
* - sirv for static assets at ./build/client/assets
* - CORS (flexible, echoes origin so cookies/credentials work)
* - COOP, CORP, X-Frame-Options and other security headers
* - special Cache-Control for images & fonts (long max-age & immutable)
*
* Usage: node server.js
*/
import compression from "compression";
import cors from "cors";
import express from "express";
import rateLimit from "express-rate-limit";
import fs from "fs/promises";
import helmet from "helmet";
import morgan from "morgan";
import path from "path";
import sirv from "sirv";
import { loadEnv } from "vite";
import cspHashes from "./build/csp-hashes.json" with { type: "json" };
/** Load environment via Vite's loadEnv so .env, .env.production etc. are picked up */
const mode = process.env.NODE_ENV || "production";
const env = loadEnv(mode, process.cwd(), ""); // returns map of strings
const PORT = Number(env.PORT || process.env.PORT || 3000);
/** Build output layout used in template:
* - Static assets: ./build/client/assets
* - Prerendered HTML: ./build/... (we try multiple candidate locations)
*/
const BUILD_DIR = path.resolve(process.cwd(), "build");
const CLIENT_PUBLIC_DIR = path.join(BUILD_DIR, "client");
const ASSETS_DIR = path.resolve(CLIENT_PUBLIC_DIR, "assets");
const DEFAULT_HTML_INDEX = path.join(BUILD_DIR, "client", "__spa-fallback.html");
const app = express();
// Use the Proxy level config
if (env.PROXY_LEVEL && Number(env.PROXY_LEVEL) > 0) {
app.set("trust proxy", Number(env.PROXY_LEVEL));
}
/** Compression: gzip/deflate for responses */
app.use(compression());
/** Log every request to the console */
morgan.token("date", (req) => {
const date = "_startTime" in req && req._startTime instanceof Date
? req._startTime
: new Date();
const paddedDay = date.getDate().toString().padStart(2, "0");
const paddedMonth = (date.getMonth() + 1).toString().padStart(2, "0");
const paddedHours = date.getHours().toString().padStart(2, "0");
const paddedMinutes = date.getMinutes().toString().padStart(2, "0");
const paddedSeconds = date.getSeconds().toString().padStart(2, "0");
return `${paddedDay}/${paddedMonth}/${date.getFullYear()} ${paddedHours}:${paddedMinutes}:${paddedSeconds}`;
});
app.use(morgan("\x1b[94m[:date]\x1b[0m \x1b[93m:remote-addr\x1b[0m :method :status :url - :response-time ms"));
/** Rate limiter: global window. Values can be overridden via env:
* RATE_WINDOW_MS (ms) and RATE_MAX (requests)
*/
const RATE_WINDOW_MS = Number(env.RATE_WINDOW_MS || process.env.RATE_WINDOW_MS) || 15 * 60 * 1000; // 15m
const RATE_MAX = Number(env.RATE_MAX || process.env.RATE_MAX) || 500;
const limiter = rateLimit({
windowMs: RATE_WINDOW_MS,
max: RATE_MAX,
standardHeaders: true,
legacyHeaders: false,
message: "Too many requests — try again later.",
});
app.use(limiter);
const MATOMO_DOMAIN = new URL(env.VITE_MATOMO_URL).origin;
const CLOUDFLARE_DOMAIN = "https://static.cloudflareinsights.com";
/** Helmet for common security headers */
app.use(
helmet({
// Content-Security-Policy (CSP) configuration
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
baseUri: ["'self'"],
connectSrc: ["'self'", env.PUBLIC_URL, env.VITE_API_URL, CLOUDFLARE_DOMAIN, MATOMO_DOMAIN],
frameAncestors: ["'self'"],
fontSrc: ["'self'", "https:"],
imgSrc: ["'self'", "data:", "https://cdn.discordapp.com"],
objectSrc: ["'none'"],
scriptSrc: [
"'self'",
MATOMO_DOMAIN,
CLOUDFLARE_DOMAIN,
...Object.values(cspHashes).flatMap((hashes) => hashes.map((hash) => `'${hash}'`)),
].filter((origin) => typeof origin === "string" && origin.trim().length > 0),
scriptSrcAttr: ["'none'"],
styleSrc: ["'self'", "https:", "'unsafe-inline'"],
upgradeInsecureRequests: [],
},
},
// Cross-Origin-Embedder-Policy (COEP) - ensures only CORS-safe resources are loaded
crossOriginEmbedderPolicy: {
policy: "credentialless",
},
// Cross-Origin-Opener-Policy (COOP) - isolates top-level browsing context
crossOriginOpenerPolicy: {
policy: "same-origin",
},
// Cross-Origin-Resource-Policy (CORP) - restricts which origins can load resources
crossOriginResourcePolicy: {
policy: "same-site",
},
// Referrer policy
referrerPolicy: {
policy: ["no-referrer", "strict-origin"],
},
// Strict-Transport-Security - enforce HTTPS
strictTransportSecurity: {
maxAge: 63072000,
includeSubDomains: true,
preload: true,
},
// Prevent MIME sniffing
noSniff: true,
// Prevent clickjacking
xFrameOptions: {
action: "deny",
},
})
);
/** Custom X-Robots-Tag headers */
app.use((_, res, next) => {
if (env.NO_INDEX === "true") {
res.setHeader("X-Robots-Tag", "noindex");
}
next();
});
/** Fully-functional CORS:
* - Echoes Origin (so credentials are allowed when needed)
* - Credentials allowed
* - Common HTTP methods and headers allowed
*
* You can replace the origin function to whitelist allowed origins.
*/
app.use(
cors({
origin: [env.PUBLIC_URL, env.VITE_API_URL, CLOUDFLARE_DOMAIN, MATOMO_DOMAIN]
.filter((origin) => typeof origin === "string" && origin.trim().length > 0),
credentials: true,
methods: ["GET", "OPTIONS", "HEAD"],
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With", "Accept", "Origin"],
exposedHeaders: ["Content-Length"],
optionsSuccessStatus: 204,
})
);
/** -------------------------
* Static assets with sirv + custom cache logic for images & fonts
* ------------------------- */
/**
* Cache strategy:
* - Images & fonts: long cache (1 year) + immutable
* - Other assets: moderate cache (1 hour)
* - HTML: no-cache / must-revalidate (served below)
*/
const ONE_HOUR = 60 * 60;
const ONE_YEAR = 365 * 24 * 60 * 60;
/** Middleware to set cache headers for assets by extension */
app.use((req, res, next) => {
// Only apply to asset requests (we map sirv under /assets)
// Use URL path (without query) to determine extension
const urlPath = req.path || "";
// images
if (/\.(png|jpe?g|gif|webp|avif|svg|ico|bmp|tiff)(\?.*)?$/i.test(urlPath)) {
res.setHeader("Cache-Control", `public, max-age=${ONE_YEAR}, immutable`);
} else if (/\.(ttf|otf|woff2?|eot)(\?.*)?$/i.test(urlPath)) {
// fonts
res.setHeader("Cache-Control", `public, max-age=${ONE_YEAR}, immutable`);
} else if (/\.(js|css|map)(\?.*)?$/i.test(urlPath)) {
// JS/CSS bundles - shorter but cachable
res.setHeader("Cache-Control", `public, max-age=${ONE_HOUR}, must-revalidate`);
}
// else leave for sirv or route handler (HTML will be set explicitly)
next();
});
/** Use sirv to serve the client assets folder */
try {
// only mount sirv if assets directory exists - fail gracefully otherwise
await fs.access(ASSETS_DIR);
app.use(
"/assets",
sirv(ASSETS_DIR, {
dev: false,
// sirv's own caching is okay — we've already set our headers where needed
etag: true,
maxAge: ONE_HOUR,
setHeaders: (res) => {
// Keep sirv from overwriting headers we've set above for fonts/images
// Only set a default Cache-Control if none set
if (!res.getHeader("Cache-Control")) {
res.setHeader("Cache-Control", `public, max-age=${ONE_HOUR}`);
}
},
})
);
} catch (err) {
console.warn(`[server] assets directory not found at ${ASSETS_DIR} — skipping sirv mount`, err);
}
await fs.access(CLIENT_PUBLIC_DIR);
app.use(
sirv(CLIENT_PUBLIC_DIR, {
dev: false,
etag: true,
single: false,
setHeaders: (res, pathname) => {
// Long cache for icons, images, fonts, sitemap
if (/\.(png|jpe?g|gif|webp|avif|svg|ico|xml|json|woff2?|ttf|otf|eot)$/i.test(pathname)) {
res.setHeader("Cache-Control", `public, max-age=${ONE_YEAR}, immutable`);
} else {
// conservative cache for other root files
res.setHeader("Cache-Control", `public, max-age=${ONE_HOUR}, must-revalidate`);
}
},
})
);
/**
* -------------------------
* Helper: resolve prerendered HTML for a route
* -------------------------
* Given a URL path like "/", "/terms" or "/some/page",
* try to find the best prerendered HTML file:
* - build/client/<route>/index.html
* - build/client/<route>.html
* - build/__spa-fallback.html (fallback)
*
* Returns absolute path to HTML file.
*
* @param {string} urlPath
*/
async function resolveHtmlForRoute(urlPath) {
// normalize paths
const safe = (/** @type {string} */ p) =>
p
.replace(/^\//, "")
.replace(/\?.*$/, "")
.replace(/\/$/, ""); // "terms/sub" etc.
/** @type {string[]} */
const candidatePaths = [];
// if path is root
if (urlPath === "/" || urlPath === "") {
candidatePaths.push("index.html");
} else {
const clean = safe(urlPath);
// try <clean>/index.html or <clean>.html (relative to PRERENDER_ROOT)
candidatePaths.push(path.join(clean, "index.html"));
candidatePaths.push(`${clean}.html`);
// fallback to __spa-fallback.html
candidatePaths.push("__spa-fallback.html");
}
for (const rel of candidatePaths) {
try {
const candidate = await fs.realpath(path.resolve(CLIENT_PUBLIC_DIR, rel));
// Resolve symlinks and normalize the path, then ensure it stays within CLIENT_PUBLIC_DIR
if (!candidate.startsWith(CLIENT_PUBLIC_DIR + path.sep) && candidate !== CLIENT_PUBLIC_DIR) {
continue;
}
await fs.access(candidate);
return candidate;
} catch {
// not found — try next
}
}
return DEFAULT_HTML_INDEX;
}
// Define URL aliases for the /terms route
app.get("/tos", (_, res) => res.redirect(301, "/terms"));
app.get("/legal-notices", (_, res) => res.redirect(301, "/terms"));
/** -------------------------
* Explicitly serve prerendered pages for your preloaded list
* ------------------------- */
/**
* Dynamically load prerendered routes from react-router.config.ts
*/
async function loadPrerenderedRoutes() {
try {
const configPath = path.resolve(process.cwd(), "react-router.config.ts");
const configModule = await import(configPath);
const prerender = configModule?.default?.prerender;
if (Array.isArray(prerender)) {
return prerender;
}
} catch (err) {
console.warn("[server] Failed to load react-router.config.ts prerender list", err);
}
return [];
}
const PRELOADED_ROUTES = await loadPrerenderedRoutes();
console.debug(`[server] Preloaded routes: ${PRELOADED_ROUTES.join(", ")}`);
/** Serve prerendered HTML for each preloaded route */
for (const route of PRELOADED_ROUTES) {
app.get(route, async (req, res, next) => {
try {
const htmlPath = await resolveHtmlForRoute(route);
// best effort: read and send the HTML file with conservative caching for HTML
const html = await fs.readFile(htmlPath, { encoding: "utf8" });
// HTML should be fresh
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.send(html);
} catch (err) {
// if file doesn't exist or read fails, pass to fallback (SPA index) or next
const errMsg = err instanceof Error ? err.message : String(err);
console.error(`[server] Failed to serve prerendered page for ${route}:`, errMsg);
next();
}
});
}
/** Health check */
app.get("/_health", (_, res) => res.json({ ok: true }));
/** Fallback for anything else to build/spa_fallback.html to let the client SPA handle routing */
app.get("*", async (_, res) => {
// serve HTML with conservative caching
const data = await fs.readFile(DEFAULT_HTML_INDEX, "utf8");
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.send(data);
});
/** Start server */
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
console.log(`Mode: ${mode}`);
console.log(`Serving build dir: ${BUILD_DIR}`);
console.log(`Assets dir (sirv): ${ASSETS_DIR}`);
});