Repository: kyeotic/raviger (ts)

Issue:
matchTrailingSlash doesn't work with "/"
Just a nit – when matchTrailingSlash is enabled, the "/" route needs to be renamed to "" for matching to work. This is a bit unexpected!

Pull request:
special case '/' when matchTrailingSlash is set
fixes #69 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ad20392..53c4fec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [1.5.1] - 2020-10-09
+### Fixed
+- `matchTrailingSlash` not matching on the root route `/`
+
 ## [1.5.0] - 2020-10-09
 ### Added
 - `query` prop to `<Redirect>`
diff --git a/src/router.js b/src/router.js
index b876273..e914ccb 100644
--- a/src/router.js
+++ b/src/router.js
@@ -44,7 +44,13 @@ function matchRoute(
   path,
   { routeProps, overridePathParams, matchTrailingSlash }
 ) {
-  if (matchTrailingSlash && path && path[path.length - 1] === '/') {
+  // path.length > 1 ensure we still match on the root route "/" when matchTrailingSlash is set
+  if (
+    matchTrailingSlash &&
+    path &&
+    path[path.length - 1] === '/' &&
+    path.length > 1
+  ) {
     path = path.substring(0, path.length - 1)
   }
   const routeMatchers = useMemo(


Test patch:
diff --git a/test/router.spec.js b/test/router.spec.js
index 078c2b0..c91c75c 100644
--- a/test/router.spec.js
+++ b/test/router.spec.js
@@ -73,6 +73,13 @@ describe('useRoutes', () => {
     act(() => navigate('/about/'))
     expect(getByTestId('label')).toHaveTextContent('about')
   })
+  test('matches trailing slash on "/"', async () => {
+    const { getByTestId } = render(
+      <Harness routes={routes} options={{ matchTrailingSlash: true }} />
+    )
+    act(() => navigate('/'))
+    expect(getByTestId('label')).toHaveTextContent('home')
+  })
   test('matches route with parameters', async () => {
     const { getByTestId } = render(<Harness routes={routes} />)