google search index
All checks were successful
CI — Website / Website — typecheck, build (pull_request) Successful in 57s

This commit is contained in:
john kevin asprec
2026-09-03 21:40:55 +08:00
parent 2eb6eed16a
commit 3150e16248
23 changed files with 406 additions and 29 deletions

View File

@@ -20,8 +20,21 @@ const MIME = {
'.webp': 'image/webp',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.xml': 'application/xml; charset=utf-8',
};
const SPA_ROUTES = new Set(['/', '/docs', '/contribute', '/community']);
function requestPath(urlPath) {
const decoded = decodeURIComponent((urlPath ?? '/').split('?')[0] || '/');
if (decoded.length > 1 && decoded.endsWith('/')) return decoded.slice(0, -1) || '/';
return decoded || '/';
}
function isSpaRoute(urlPath) {
return SPA_ROUTES.has(requestPath(urlPath));
}
function fileUnderRoot(urlPath) {
const decoded = decodeURIComponent((urlPath ?? '/').split('?')[0] || '/');
const relative = decoded.replace(/^\/+/, '').replaceAll('/', sep);
@@ -54,8 +67,14 @@ const server = createServer((req, res) => {
}
let file = candidate;
if (!existsSync(file) || statSync(file).isDirectory()) {
file = join(root, 'index.html');
let status = 200;
if (existsSync(file) && statSync(file).isDirectory()) {
const nested = join(file, 'index.html');
file = existsSync(nested) ? nested : join(root, 'index.html');
} else if (!existsSync(file)) {
const notFound = join(root, '404.html');
file = existsSync(notFound) && !isSpaRoute(req.url ?? '/') ? notFound : join(root, 'index.html');
if (!isSpaRoute(req.url ?? '/')) status = 404;
}
if (!existsSync(file)) {
send(res, 500, 'dist/ missing — run npm run build before start');
@@ -63,7 +82,7 @@ const server = createServer((req, res) => {
}
const type = MIME[extname(file).toLowerCase()] ?? 'application/octet-stream';
res.writeHead(200, { 'Content-Type': type });
res.writeHead(status, { 'Content-Type': type });
if (req.method === 'HEAD') {
res.end();
return;