Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: index routes #562 #566

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/hydrogen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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)
- dx: Correct Typescript issue where `as` was a default prop for all components when it should not be
- New React hook `useScriptLoader` is available to more easily load external scripts

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,52 @@ it('handles index pages', () => {
]);
});

it('handles nested index pages', () => {
const pages: ImportGlobEagerOutput = {
'./pages/products/index.server.jsx': STUB_MODULE,
'./pages/products/[handle].server.jsx': STUB_MODULE,
'./pages/blogs/index.server.jsx': STUB_MODULE,
'./pages/products/snowboards/fastones/index.server.jsx': STUB_MODULE,
'./pages/articles/index.server.jsx': STUB_MODULE,
'./pages/articles/[...handle].server.jsx': STUB_MODULE,
};

const routes = createRoutesFromPages(pages);

expect(routes).toEqual([
{
path: '/products',
component: STUB_MODULE.default,
exact: true,
},
{
path: '/blogs',
component: STUB_MODULE.default,
exact: true,
},
{
path: '/products/snowboards/fastones',
component: STUB_MODULE.default,
exact: true,
},
{
path: '/articles',
component: STUB_MODULE.default,
exact: true,
},
{
path: '/products/:handle',
component: STUB_MODULE.default,
exact: true,
},
{
path: '/articles/:handle',
component: STUB_MODULE.default,
exact: false,
},
]);
});

it('handles dynamic paths', () => {
const pages: ImportGlobEagerOutput = {
'./pages/contact.server.jsx': STUB_MODULE,
Expand Down