Skip to content

Commit 1bbfecb

Browse files
committed
fix: never add trailing slashes to error pages
1 parent 014cb20 commit 1bbfecb

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/utils/path.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ describe('handleTrailingSlash', () => {
7777
expect(expectation).toBe('https://example.com/');
7878
});
7979

80-
it('should never add trailing slashes to paths with file extensions', () => {
81-
const expectation = handleTrailingSlash('404.html', 'always');
80+
it('should never add trailing slashes to error pages', () => {
81+
let expectation = handleTrailingSlash('404.html', 'always');
8282
expect(expectation).toBe('404.html');
83+
expectation = handleTrailingSlash('https://example.com/403.html/403.html', 'always');
84+
expect(expectation).toBe('https://example.com/403.html/403.html');
8385
});
8486
});
8587

src/utils/path.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { posix as nodePath } from 'path';
22
import { PluginOptions } from 'gatsby';
33

4+
const ERROR_PAGE_PATH_PATTERN = /[0-9]{3}\.([0-9a-z]+)(?:[?#]|$)/gim;
5+
46
export const handleTrailingSlash = (path: string, trailingSlashOption: PluginOptions['trailingSlash'] = 'always') => {
5-
if (path === '/' || path.length < 1) {
7+
if (path === '/' || path.length < 1 || path.match(ERROR_PAGE_PATH_PATTERN)) {
68
return path;
79
}
810

0 commit comments

Comments
 (0)