google search index
All checks were successful
CI — Website / Website — typecheck, build (pull_request) Successful in 57s
All checks were successful
CI — Website / Website — typecheck, build (pull_request) Successful in 57s
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user