« get me outta code hell

force trailing slash on --live-dev-server - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-01-15 15:02:00 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-01-15 15:10:01 -0400
commitba7a7232c52994079e36dfa94cc7986cddcdd402 (patch)
tree0b492e9c94e2c02cc58603a6d1ee64e33d619b97
parent13c81e08805b1de24119c589872dc594af588dcf (diff)
force trailing slash on --live-dev-server
-rw-r--r--src/write/build-modes/live-dev-server.js22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index 0e2b6160..81ae5b8e 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -236,6 +236,20 @@ export async function go({
       return;
     }
 
+    // All pages expect to be served at a URL with a trailing slash, which must
+    // be fulfilled for relative URLs (ex. href="../lofam5/") to work. Redirect
+    // if there is no trailing slash in the request URL.
+    if (!pathname.endsWith('/')) {
+      const target = pathname + '/';
+      response.writeHead(301, {
+        ...contentTypePlain,
+        'Location': target,
+      });
+      response.end(`Redirecting to: ${target}\n`);
+      console.log(`${requestHead} [301] (trl. slash) ${pathname}`);
+      return;
+    }
+
     const {
       baseDirectory,
       language,
@@ -256,9 +270,13 @@ export async function go({
 
     try {
       if (page.type === 'redirect') {
-        response.writeHead(301, contentTypeHTML);
-
         const target = to('localized.' + page.toPath[0], ...page.toPath.slice(1));
+
+        response.writeHead(301, {
+          ...contentTypeHTML,
+          'Location': target,
+        });
+
         const redirectHTML = generateRedirectHTML(page.title, target, {language});
 
         response.end(redirectHTML);