Fix Next static asset serving

This commit is contained in:
2026-08-05 21:33:19 +02:00
parent 971d493015
commit 964bd44a82
2 changed files with 59 additions and 2 deletions
+2 -2
View File
@@ -5,8 +5,8 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "NEXT_TELEMETRY_DISABLED=1 next dev -H 0.0.0.0", "dev": "NEXT_TELEMETRY_DISABLED=1 next dev -H 0.0.0.0",
"build": "NEXT_TELEMETRY_DISABLED=1 next build", "build": "NEXT_TELEMETRY_DISABLED=1 next build --webpack",
"start": "NEXT_TELEMETRY_DISABLED=1 next start -H 0.0.0.0", "start": "NEXT_TELEMETRY_DISABLED=1 NODE_ENV=production node server.mjs",
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
+57
View File
@@ -0,0 +1,57 @@
import { existsSync, statSync } from "node:fs";
import { createServer } from "node:http";
import path from "node:path";
import { fileURLToPath, parse } from "node:url";
import next from "next";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const port = Number.parseInt(process.env.PORT ?? "3910", 10);
const hostname = process.env.HOSTNAME ?? "0.0.0.0";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
const nextStaticRoot = path.resolve(dirname, ".next/static");
function isMissingNextStaticAsset(pathname) {
if (!pathname.startsWith("/_next/static/")) {
return false;
}
let relativePath;
try {
relativePath = decodeURIComponent(pathname.replace("/_next/static/", ""));
} catch {
return true;
}
const assetPath = path.resolve(nextStaticRoot, relativePath);
if (!assetPath.startsWith(`${nextStaticRoot}${path.sep}`)) {
return true;
}
try {
return !existsSync(assetPath) || !statSync(assetPath).isFile();
} catch {
return true;
}
}
await app.prepare();
createServer((req, res) => {
const parsedUrl = parse(req.url ?? "/", true);
const pathname = parsedUrl.pathname ?? "/";
if (isMissingNextStaticAsset(pathname)) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Not found");
return;
}
handle(req, res, parsedUrl);
}).listen(port, hostname, () => {
console.log(`TicketTracker frontend listening on http://${hostname}:${port}`);
});