From 156bac7052cfee9e043e4619d4d5a1d0c9b022b8 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Mon, 4 Apr 2022 15:18:58 -0400 Subject: [PATCH] Tweak to redirect logic --- server/not-found.js | 10 +++++++--- tests/server/fixtures/en/index.html | 3 +++ tests/server/getNonLocalizedURL.js | 5 +++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 tests/server/fixtures/en/index.html diff --git a/server/not-found.js b/server/not-found.js index 4f4681fba1ab..46445ad47599 100644 --- a/server/not-found.js +++ b/server/not-found.js @@ -64,15 +64,19 @@ const getNonLocalizedURL = ( // This throws if the path is invalid, and returns undefined if it's valid. // (existsSync() is deprecated.) fs.accessSync(possibleIndexPath); - // We have a valid file! // Setting the first item to '' will remove the default locale prefix from // the URL we redirect to, while ensuring the URL starts with '/'. pathParts[0] = ''; - return pathParts.join('/'); + // If there's at least one valid string, join it with '/' and redirect. + if (pathParts.some(part => part !== '')) { + return pathParts.join('/'); + } } catch (err) { // There was no index.html at the default locale path. - return null; } + + // If we get here, return null to indicate there's no non-localized URL. + return null; }; /** diff --git a/tests/server/fixtures/en/index.html b/tests/server/fixtures/en/index.html new file mode 100644 index 000000000000..975adaf15ca9 --- /dev/null +++ b/tests/server/fixtures/en/index.html @@ -0,0 +1,3 @@ + + + diff --git a/tests/server/getNonLocalizedURL.js b/tests/server/getNonLocalizedURL.js index 47344acb606a..9c1c87489e6b 100644 --- a/tests/server/getNonLocalizedURL.js +++ b/tests/server/getNonLocalizedURL.js @@ -29,6 +29,11 @@ test('returns null if no redirect is possible, with an index.html suffix', t => t.is(result, null); }); +test('returns null if no redirect is possible, without a trailing /', t => { + const result = getNonLocalizedURL('/doesnotexist', ROOT_DIR, DEFAULT_LOCALE); + t.is(result, null); +}); + test('returns null if the URL starts with the default locale', t => { const result = getNonLocalizedURL('/en/ignored/', ROOT_DIR, DEFAULT_LOCALE); t.is(result, null);