diff --git a/src/router.js b/src/router.js index 0f29443..3068c87 100644 --- a/src/router.js +++ b/src/router.js @@ -70,7 +70,7 @@ export function LocationProvider(props) { const value = useMemo(() => { const u = new URL(url, location.origin); - const path = u.pathname.replace(/(.)\/$/g, '$1'); + const path = u.pathname.replace(/\/+$/g, '') || '/'; // @ts-ignore-next return { url, diff --git a/test/router.test.js b/test/router.test.js index 7acc9b8..08a10b0 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -24,6 +24,27 @@ describe('Router', () => { history.replaceState(null, null, '/'); }); + it('should strip trailing slashes from path', async () => { + let loc; + render( + html` + <${LocationProvider} url=${'/a/'}> + <${() => { + loc = useLocation(); + }} /> + + `, + scratch + ); + + expect(loc).toMatchObject({ + url: '/a/', + path: '/a', + query: {}, + route: expect.any(Function) + }); + }); + it('should allow passing props to a route', async () => { const Home = jest.fn(() => html`

Home

`); const stack = [];