Skip to content

Commit 1a32fb7

Browse files
authored
fix: When alias contains parameters, append ext error (#1855)
1 parent 7652bd4 commit 1a32fb7

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/core/router/history/base.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2+
cleanPath,
23
getPath,
34
isAbsolutePath,
4-
stringifyQuery,
5-
cleanPath,
65
replaceSlug,
76
resolvePath,
7+
stringifyQuery,
88
} from '../util.js';
99
import { noop } from '../../util/core.js';
1010

@@ -32,11 +32,19 @@ export class History {
3232
}
3333

3434
#getFileName(path, ext) {
35-
return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path)
36-
? path
37-
: /\/$/g.test(path)
38-
? `${path}README${ext}`
39-
: `${path}${ext}`;
35+
const [basePath, query] = path.split('?');
36+
37+
const hasValidExt = new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`).test(
38+
basePath,
39+
);
40+
41+
const updatedPath = hasValidExt
42+
? basePath
43+
: /\/$/g.test(basePath)
44+
? `${basePath}README${ext}`
45+
: `${basePath}${ext}`;
46+
47+
return query ? `${updatedPath}?${query}` : updatedPath;
4048
}
4149

4250
getBasePath() {

test/unit/router-history-base.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,35 @@ describe('router/history/base', () => {
6666
expect(url).toBe('/README');
6767
});
6868
});
69+
70+
// getFile test
71+
// ---------------------------------------------------------------------------
72+
describe('getFile', () => {
73+
// Tests
74+
// -------------------------------------------------------------------------
75+
test('path is url', () => {
76+
const file = history.getFile('https://some/raw/url/README.md');
77+
78+
expect(file).toBe('https://some/raw/url/README.md');
79+
});
80+
test('path is url, but ext is .html', () => {
81+
const file = history.getFile('https://foo.com/index.html');
82+
83+
expect(file).toBe('https://foo.com/index.html');
84+
});
85+
test('path is url, but with parameters', () => {
86+
const file = history.getFile(
87+
'https://some/raw/url/README.md?token=Mytoken',
88+
);
89+
90+
expect(file).toBe('https://some/raw/url/README.md?token=Mytoken');
91+
});
92+
test('path is url, but ext is different', () => {
93+
history = new MockHistory({ ext: '.ext' });
94+
95+
const file = history.getFile('https://some/raw/url/README.md');
96+
97+
expect(file).toBe('https://some/raw/url/README.md.ext');
98+
});
99+
});
69100
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy