Skip to content

Commit

Permalink
fix: index routes #562
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Jan 27, 2022
1 parent 4998760 commit ead09d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/hydrogen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
## Unreleased

- Fix index routes. See [#562](https://github.com/Shopify/hydrogen/issues/562)

## 0.10.1 - 2022-01-26

Expand Down
5 changes: 4 additions & 1 deletion packages/hydrogen/src/foundation/Router/DefaultRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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?$/, '')
/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ead09d0

Please sign in to comment.