Fix cache policy for stable public assets
This commit is contained in:
parent
ffb020ee98
commit
75592d7d7a
6 changed files with 171 additions and 40 deletions
99
server.mjs
99
server.mjs
|
|
@ -1,19 +1,16 @@
|
|||
// Rôle : sert l'application Vite construite ou les sources publiques en local.
|
||||
// Rôle : sert l'application Vite construite ou les sources publiques avec leur politique de cache.
|
||||
import { createReadStream, existsSync, statSync } from "node:fs";
|
||||
import { loadProjectEnv } from "./scripts/load-project-env.mjs";
|
||||
import { createServer } from "node:http";
|
||||
import { extname, join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const modulePath = fileURLToPath(import.meta.url);
|
||||
const projectRoot = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
const websiteRoot = join(projectRoot, "website");
|
||||
const distRoot = join(websiteRoot, "dist");
|
||||
const root = existsSync(distRoot) ? distRoot : websiteRoot;
|
||||
const publicRoot = existsSync(distRoot) ? distRoot : join(websiteRoot, "public");
|
||||
|
||||
loadProjectEnv();
|
||||
|
||||
const port = Number(process.env.PORT || 5173);
|
||||
const defaultRoot = existsSync(distRoot) ? distRoot : websiteRoot;
|
||||
const defaultPublicRoot = existsSync(distRoot) ? distRoot : join(websiteRoot, "public");
|
||||
|
||||
const types = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
|
|
@ -33,11 +30,11 @@ function isInsideRoot(parent, file) {
|
|||
return Boolean(relativePath) && !relativePath.startsWith("..") && !relativePath.startsWith("/");
|
||||
}
|
||||
|
||||
function fileIfReadable(file) {
|
||||
return isInsideRoot(root, file) && existsSync(file) && statSync(file).isFile() ? file : null;
|
||||
function fileIfReadable(parent, file) {
|
||||
return isInsideRoot(parent, file) && existsSync(file) && statSync(file).isFile() ? file : null;
|
||||
}
|
||||
|
||||
function resolvePath(url) {
|
||||
function resolvePath(url, root, publicRoot) {
|
||||
let requested;
|
||||
try {
|
||||
requested = decodeURIComponent(new URL(url, "http://localhost").pathname);
|
||||
|
|
@ -45,46 +42,68 @@ function resolvePath(url) {
|
|||
return null;
|
||||
}
|
||||
|
||||
const candidate = fileIfReadable(resolve(root, `.${requested}`));
|
||||
const candidate = fileIfReadable(root, resolve(root, `.${requested}`));
|
||||
if (candidate) return candidate;
|
||||
|
||||
const publicCandidate = fileIfReadable(resolve(publicRoot, `.${requested}`));
|
||||
if (publicCandidate && isInsideRoot(publicRoot, publicCandidate)) return publicCandidate;
|
||||
const publicCandidate = fileIfReadable(publicRoot, resolve(publicRoot, `.${requested}`));
|
||||
if (publicCandidate) return publicCandidate;
|
||||
|
||||
return extname(requested) ? null : join(root, "index.html");
|
||||
}
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
const file = resolvePath(req.url || "/");
|
||||
if (!file || !existsSync(file)) {
|
||||
res.writeHead(404);
|
||||
res.end("Not found");
|
||||
return;
|
||||
}
|
||||
function isVersionedAsset(root, file) {
|
||||
const relativePath = relative(root, file).replaceAll("\\", "/");
|
||||
const fileName = relativePath.split("/").at(-1) || "";
|
||||
return relativePath.startsWith("assets/") && /-[a-z0-9_-]{8,}\.[^.]+(?:\.map)?$/i.test(fileName);
|
||||
}
|
||||
|
||||
function getCacheControl(root, file) {
|
||||
const extension = extname(file);
|
||||
const cacheControl = extension === ".html"
|
||||
? "no-store"
|
||||
: extension === ".json"
|
||||
? "no-cache"
|
||||
: "public, max-age=31536000, immutable";
|
||||
res.writeHead(200, {
|
||||
"Content-Type": types[extname(file)] || "application/octet-stream",
|
||||
"Cache-Control": cacheControl
|
||||
if (extension === ".html") return "no-store";
|
||||
if (extension === ".json") return "no-cache";
|
||||
if (isVersionedAsset(root, file)) return "public, max-age=31536000, immutable";
|
||||
return "no-cache";
|
||||
}
|
||||
|
||||
export function createStaticServer({ root = defaultRoot, publicRoot = defaultPublicRoot } = {}) {
|
||||
return createServer((req, res) => {
|
||||
const file = resolvePath(req.url || "/", root, publicRoot);
|
||||
if (!file || !existsSync(file)) {
|
||||
res.writeHead(404, {
|
||||
"Content-Type": "text/plain; charset=utf-8",
|
||||
"Cache-Control": "no-store"
|
||||
});
|
||||
res.end("Not found");
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, {
|
||||
"Content-Type": types[extname(file)] || "application/octet-stream",
|
||||
"Cache-Control": getCacheControl(root, file)
|
||||
});
|
||||
createReadStream(file).pipe(res);
|
||||
});
|
||||
createReadStream(file).pipe(res);
|
||||
});
|
||||
}
|
||||
|
||||
server.on("error", (error) => {
|
||||
if (error.code === "EADDRINUSE") {
|
||||
console.error(`Port ${port} is already in use. Set PORT to another value.`);
|
||||
export function startServer() {
|
||||
loadProjectEnv();
|
||||
const port = Number(process.env.PORT || 5173);
|
||||
const server = createStaticServer();
|
||||
|
||||
server.on("error", (error) => {
|
||||
if (error.code === "EADDRINUSE") {
|
||||
console.error(`Port ${port} is already in use. Set PORT to another value.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
server.listen(port, () => {
|
||||
console.log(`Sokko G dev server running at http://localhost:${port}`);
|
||||
});
|
||||
return server;
|
||||
}
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`Sokko G dev server running at http://localhost:${port}`);
|
||||
});
|
||||
if (process.argv[1] && resolve(process.argv[1]) === modulePath) startServer();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue