« get me outta code hell

Merge pull request #135 from hsmusic/lds-redirects - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-01-15 15:14:19 -0400
committerGitHub <noreply@github.com>2023-01-15 15:14:19 -0400
commit09d909cad62d325acdce46710e188853e3ef608a (patch)
tree0b492e9c94e2c02cc58603a6d1ee64e33d619b97 /src
parent13c81e08805b1de24119c589872dc594af588dcf (diff)
parentba7a7232c52994079e36dfa94cc7986cddcdd402 (diff)
Merge pull request #135 from hsmusic/lds-redirects
Live dev server: Force trailing slash for served paths
Diffstat (limited to 'src')
-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);