Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Tweak to redirect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Apr 4, 2022
1 parent 63539a2 commit 156bac7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions server/not-found.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/server/fixtures/en/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<!-- ignored -->
</html>
5 changes: 5 additions & 0 deletions tests/server/getNonLocalizedURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 156bac7

Please sign in to comment.