diff --git a/index.html b/index.html index 06b05b6..acc415e 100755 --- a/index.html +++ b/index.html @@ -30,9 +30,9 @@
Please enable javascript >.<
- gallery + gallery / - rankings + rankings
diff --git a/rankings.html b/rankings.html index c76c7db..501e067 100644 --- a/rankings.html +++ b/rankings.html @@ -30,9 +30,9 @@pick the better meal, one pair at a time, and the board updates live in this browser.
- gallery + gallery / - rankings + rankings
diff --git a/scripts/serve.js b/scripts/serve.js index 6468831..18cd2b2 100644 --- a/scripts/serve.js +++ b/scripts/serve.js @@ -94,6 +94,15 @@ function resolveRequestPath(pathname) { return path.join(absolutePath, "index.html"); } + if ( + path.extname(absolutePath) === "" && + !fs.existsSync(absolutePath) && + fs.existsSync(`${absolutePath}.html`) && + fs.statSync(`${absolutePath}.html`).isFile() + ) { + return `${absolutePath}.html`; + } + return absolutePath; } @@ -113,6 +122,14 @@ function sendText(response, statusCode, body) { response.end(body); } +function sendRedirect(response, location) { + response.writeHead(301, { + Location: location, + "Cache-Control": "no-store", + }); + response.end(); +} + function parseJsonBody(request) { return new Promise((resolve, reject) => { const chunks = []; @@ -194,14 +211,29 @@ async function handleApiRequest(request, response, pathname, options) { function createServer(options) { return http.createServer(async (request, response) => { let pathname; + let search; try { - pathname = decodeURIComponent(new URL(request.url || "/", "http://localhost").pathname); + const requestUrl = new URL(request.url || "/", "http://localhost"); + pathname = decodeURIComponent(requestUrl.pathname); + search = requestUrl.search; } catch (error) { sendText(response, 400, "Bad request"); return; } + if (request.method === "GET" || request.method === "HEAD") { + if (pathname === "/index.html") { + sendRedirect(response, `/${search}`); + return; + } + + if (pathname.endsWith(".html") && pathname !== "/index.html") { + sendRedirect(response, `${pathname.slice(0, -5)}${search}`); + return; + } + } + try { if (await handleApiRequest(request, response, pathname, options)) { return; diff --git a/templates/index.html b/templates/index.html index 3d5900c..55dc1ba 100644 --- a/templates/index.html +++ b/templates/index.html @@ -30,9 +30,9 @@Please enable javascript >.<
- gallery + gallery / - rankings + rankings
diff --git a/templates/rankings.html b/templates/rankings.html index 6a5c32b..bce32de 100644 --- a/templates/rankings.html +++ b/templates/rankings.html @@ -30,9 +30,9 @@pick the better meal, one pair at a time, and the board updates live in this browser.