From ba7a7232c52994079e36dfa94cc7986cddcdd402 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 15 Jan 2023 15:02:00 -0400 Subject: force trailing slash on --live-dev-server --- src/write/build-modes/live-dev-server.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/write/build-modes') 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); -- cgit 1.3.0-6-gf8a5