Skip to content

Commit 5fa1435

Browse files
authored
fix: now data routes for dynamic routes filter even when null when creating the RSC data router (#2041)
* fix: now data routes for dynamic routes filter even when null when creating the RSC data router * test: updated test descriptions
1 parent b7b9392 commit 5fa1435

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/runtime/src/templates/edge-shared/rsc-data.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const getRscDataRouter = ({ routes: staticRoutes, dynamicRoutes }: Preren
4444
)
4545

4646
const dynamicRouteMatcher = Object.values(dynamicRoutes)
47-
.filter(({ dataRoute }) => dataRoute.endsWith('.rsc'))
47+
.filter(({ dataRoute }) => dataRoute?.endsWith('.rsc'))
4848
.map(({ routeRegex }) => new RegExp(routeRegex))
4949

5050
const matchesDynamicRscDataRoute = (pathname: string) => {

test/rsc-data.spec.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const basePrerenderManifest: PrerenderManifest = {
88
}
99

1010
describe('getRscDataRouter', () => {
11-
it('should create a RSC data router when data routes are not present for routes', () => {
11+
it('should filter static routes when creating the RSC data router', () => {
1212
const manifest: PrerenderManifest = {
1313
...basePrerenderManifest,
1414
routes: {
@@ -37,4 +37,36 @@ describe('getRscDataRouter', () => {
3737

3838
expect(typeof rscDataRouter).toBe('function')
3939
})
40+
41+
it('should filter dynamic routes when creating the RSC data router', () => {
42+
const manifest: PrerenderManifest = {
43+
...basePrerenderManifest,
44+
dynamicRoutes: {
45+
'/[slug]': {
46+
routeRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)?$',
47+
fallback: null,
48+
dataRoute: null,
49+
dataRouteRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)?$',
50+
},
51+
'/[slug]/[slug2]': {
52+
routeRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)(?<slug2>[^\\/]+?)(?:\\/)?$',
53+
fallback: null,
54+
dataRoute: '/[slug]/[slug2].json.rsc',
55+
dataRouteRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)(?<slug2>[^\\/]+?)(?:\\/)?$',
56+
},
57+
},
58+
}
59+
60+
let rscDataRouter
61+
62+
// Normally type checking would pick this up, but because this file is copied when generating
63+
// edge functions for the build, we need to make sure it's valid for builds.
64+
//
65+
// See https://github.com/netlify/next-runtime/issues/1940
66+
expect(() => {
67+
rscDataRouter = getRscDataRouter(manifest)
68+
}).not.toThrow()
69+
70+
expect(typeof rscDataRouter).toBe('function')
71+
})
4072
})

0 commit comments

Comments
 (0)