diff --git a/packages/hydrogen/CHANGELOG.md b/packages/hydrogen/CHANGELOG.md index a5e5b3cc87..d4b81c355d 100644 --- a/packages/hydrogen/CHANGELOG.md +++ b/packages/hydrogen/CHANGELOG.md @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). - +## Unreleased + +- Fix index routes. See [#562](https://github.com/Shopify/hydrogen/issues/562) ## 0.10.1 - 2022-01-26 diff --git a/packages/hydrogen/src/foundation/Router/DefaultRoutes.tsx b/packages/hydrogen/src/foundation/Router/DefaultRoutes.tsx index 9aec6b8a4e..eb6b93060f 100644 --- a/packages/hydrogen/src/foundation/Router/DefaultRoutes.tsx +++ b/packages/hydrogen/src/foundation/Router/DefaultRoutes.tsx @@ -61,7 +61,7 @@ export function createRoutesFromPages( const routes = Object.keys(pages) .map((key) => { - const path = key + let path = key .replace('./pages', '') .replace(/\.server\.(t|j)sx?$/, '') /** @@ -81,6 +81,9 @@ export function createRoutesFromPages( (_match, param: string) => `:${param}` ); + if (path.endsWith('/') && path !== '/') + path = path.substring(0, path.length - 1); + /** * Catch-all routes [...handle].jsx don't need an exact match * https://reactrouter.com/core/api/Route/exact-bool diff --git a/packages/hydrogen/src/foundation/Router/tests/DefaultRoutes.test.tsx b/packages/hydrogen/src/foundation/Router/tests/DefaultRoutes.test.tsx index cb14fd5d91..d169959e36 100644 --- a/packages/hydrogen/src/foundation/Router/tests/DefaultRoutes.test.tsx +++ b/packages/hydrogen/src/foundation/Router/tests/DefaultRoutes.test.tsx @@ -42,6 +42,28 @@ it('handles index pages', () => { ]); }); +it('handles nested index pages', () => { + const pages: ImportGlobEagerOutput = { + './pages/blogs/index.server.jsx': STUB_MODULE, + './pages/products/snowboards/fastones/index.server.jsx': STUB_MODULE, + }; + + const routes = createRoutesFromPages(pages); + + expect(routes).toEqual([ + { + path: '/blogs', + component: STUB_MODULE.default, + exact: true, + }, + { + path: '/products/snowboards/fastones', + component: STUB_MODULE.default, + exact: true, + }, + ]); +}); + it('handles dynamic paths', () => { const pages: ImportGlobEagerOutput = { './pages/contact.server.jsx': STUB_MODULE,