Skip to content

Commit 6cfff8b

Browse files
feat(router): remove all trailing slashes for path (#26)
* feat(router): remove all trailing slashes for path * test: Add test for trailing slashes in path --------- Co-authored-by: Ryan Christian <[email protected]>
1 parent bd83e97 commit 6cfff8b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function LocationProvider(props) {
7070

7171
const value = useMemo(() => {
7272
const u = new URL(url, location.origin);
73-
const path = u.pathname.replace(/(.)\/$/g, '$1');
73+
const path = u.pathname.replace(/\/+$/g, '') || '/';
7474
// @ts-ignore-next
7575
return {
7676
url,

test/router.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ describe('Router', () => {
2424
history.replaceState(null, null, '/');
2525
});
2626

27+
it('should strip trailing slashes from path', async () => {
28+
let loc;
29+
render(
30+
html`
31+
<${LocationProvider} url=${'/a/'}>
32+
<${() => {
33+
loc = useLocation();
34+
}} />
35+
<//>
36+
`,
37+
scratch
38+
);
39+
40+
expect(loc).toMatchObject({
41+
url: '/a/',
42+
path: '/a',
43+
query: {},
44+
route: expect.any(Function)
45+
});
46+
});
47+
2748
it('should allow passing props to a route', async () => {
2849
const Home = jest.fn(() => html`<h1>Home</h1>`);
2950
const stack = [];

0 commit comments

Comments
 (0)