Skip to content
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
7 changes: 6 additions & 1 deletion openframe-frontend-core/scripts/route-styles-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ function routePattern(route) {
return { route, dynamic, regex: new RegExp(`^${source || '/'}$`) };
}

/**
* The page's path as a route is written. `/index` is the home page: Next calls it that on the server (Vercel serves it
* as the `index` function, and a page regenerated there renders with `/index` as its path), and no app route is
* named `index` because Next answers `/index` itself.
*/
function normalize(pathname) {
const path = `/${String(pathname || '').replace(/^\/+/, '')}`.replace(/\/+$/, '');
return path === '' ? '/' : path;
return path === '' || path === '/index' ? '/' : path;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion openframe-frontend-core/src/__tests__/route-styles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('RouteStyles: the rules a page gets on the server', () => {
rmSync(root, { recursive: true, force: true });
});

it('matches the page path to its route, a static route before a dynamic one, and the complete sheet otherwise', async () => {
it('matches the page path to its route, a static route before a dynamic one, the home page by either name, and the complete sheet otherwise', async () => {
const { RouteStyles } = await import('../../scripts/route-styles-server.mjs');
const element = RouteStyles({}) as { props: { build: string } };
expect(element.props.build).toBe('b2');
Expand All @@ -345,6 +345,8 @@ describe('RouteStyles: the rules a page gets on the server', () => {
)[Symbol.for('flamingo.next-styles')].get('b2');
if (!resolve) throw new Error('RouteStyles registered no resolver');
expect(resolve('/', false).css).toBe('.home{}');
// A home page Vercel regenerates renders as its `index` function: the path it sees is `/index`.
expect(resolve('/index', false).css).toBe('.home{}');
expect(resolve('/blog/', false).css).toBe('.blog{}');
expect(resolve('/blog/a-post', false)).toEqual({ css: '.post{}', fullUrl: '/_next/static/next-styles/full.css' });
expect(resolve('/docs/a/b', false).css).toBe('.docs{}');
Expand Down
Loading