Skip to content

Commit

Permalink
fix: intercept routes being unintentionally stripped (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Jun 13, 2023
1 parent 016b8d3 commit d835933
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/young-beers-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Prevent the route group stripping regex from removing intercept routes.
5 changes: 4 additions & 1 deletion src/utils/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { normalizePath } from './fs';
* The build output config does not rewrite requests to route groups, so we need to strip route
* groups from the path name for file system matching.
*
* Does not strip brackets containing `.` characters, as these are used for route intercepts.
* https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes
*
* @param path Path name to strip route groups from.
* @returns Path name with route groups stripped.
*/
export function stripRouteGroups(path: string) {
return path.replace(/\/\(([^)]+)\)/g, '');
return path.replace(/\/\(([^).]+)\)/g, '');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/src/utils/routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ describe('routing', () => {
expect(stripRouteGroups('/(route-group)/path')).toEqual('/path');
});

test('stripRouteGroups does not strip intercepts', () => {
expect(stripRouteGroups('/(.)intercept')).toEqual('/(.)intercept');
expect(stripRouteGroups('/(..)(..)intercept')).toEqual(
'/(..)(..)intercept'
);
});

test('stripIndexRoute', () => {
expect(stripIndexRoute('/index')).toEqual('/');
expect(stripIndexRoute('/path/index')).toEqual('/path');
Expand Down

0 comments on commit d835933

Please sign in to comment.