Skip to content

Commit

Permalink
feat(router): remove all trailing slashes for path (#26)
Browse files Browse the repository at this point in the history
* feat(router): remove all trailing slashes for path

* test: Add test for trailing slashes in path

---------

Co-authored-by: Ryan Christian <[email protected]>
  • Loading branch information
donkeyDau and rschristian authored Jul 7, 2024
1 parent bd83e97 commit 6cfff8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`<h1>Home</h1>`);
const stack = [];
Expand Down

0 comments on commit 6cfff8b

Please sign in to comment.