Skip to content

Commit 2f31acb

Browse files
fix: ensure appDir routes with null dataRoutes are not added to edge manifest for rsc-data (#2044)
* fix: ensure falsy dataRoutes are not included in the edge manifest * test: add tests for falsy dataRoutes in the edge manifest * chore: rename rsc data edge manifest function * chore: update dataroute regex test description --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 447b7d4 commit 2f31acb

File tree

2 files changed

+122
-4
lines changed

2 files changed

+122
-4
lines changed

packages/runtime/src/helpers/edge.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export const writeDevEdgeFunction = async ({
263263
* Writes an edge function that routes RSC data requests to the `.rsc` route
264264
*/
265265

266-
export const writeRscDataEdgeFunction = async ({
266+
export const generateRscDataEdgeManifest = async ({
267267
prerenderManifest,
268268
appPathRoutesManifest,
269269
}: {
@@ -275,14 +275,14 @@ export const writeRscDataEdgeFunction = async ({
275275
}
276276
const staticAppdirRoutes: Array<string> = []
277277
for (const [path, route] of Object.entries(prerenderManifest.routes)) {
278-
if (isAppDirRoute(route.srcRoute, appPathRoutesManifest)) {
278+
if (isAppDirRoute(route.srcRoute, appPathRoutesManifest) && route.dataRoute) {
279279
staticAppdirRoutes.push(path, route.dataRoute)
280280
}
281281
}
282282
const dynamicAppDirRoutes: Array<string> = []
283283

284284
for (const [path, route] of Object.entries(prerenderManifest.dynamicRoutes)) {
285-
if (isAppDirRoute(path, appPathRoutesManifest)) {
285+
if (isAppDirRoute(path, appPathRoutesManifest) && route.dataRouteRegex) {
286286
dynamicAppDirRoutes.push(route.routeRegex, route.dataRouteRegex)
287287
}
288288
}
@@ -368,7 +368,7 @@ export const writeEdgeFunctions = async ({
368368
return
369369
}
370370

371-
const rscFunctions = await writeRscDataEdgeFunction({
371+
const rscFunctions = await generateRscDataEdgeManifest({
372372
prerenderManifest: await loadPrerenderManifest(netlifyConfig),
373373
appPathRoutesManifest: await loadAppPathRoutesManifest(netlifyConfig),
374374
})

test/edge.spec.ts

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { generateRscDataEdgeManifest } from '../packages/runtime/src/helpers/edge'
2+
import type { PrerenderManifest } from 'next/dist/build'
3+
4+
const basePrerenderManifest: PrerenderManifest = {
5+
version: 3,
6+
routes: {},
7+
dynamicRoutes: {},
8+
notFoundRoutes: [],
9+
preview: {
10+
previewModeId: '',
11+
previewModeSigningKey: '',
12+
previewModeEncryptionKey: '',
13+
},
14+
}
15+
16+
describe('generateRscDataEdgeManifest', () => {
17+
it('should return manifest entries for static appDir routes', async () => {
18+
const prerenderManifest: PrerenderManifest = {
19+
...basePrerenderManifest,
20+
routes: {
21+
'/': {
22+
initialRevalidateSeconds: false,
23+
srcRoute: '/',
24+
dataRoute: '/index.rsc',
25+
},
26+
},
27+
}
28+
const appPathRoutesManifest = {
29+
'/page': '/',
30+
}
31+
const edgeManifest = await generateRscDataEdgeManifest({ prerenderManifest, appPathRoutesManifest })
32+
33+
expect(edgeManifest).toEqual([
34+
{
35+
function: 'rsc-data',
36+
name: 'RSC data routing',
37+
path: '/',
38+
},
39+
{
40+
function: 'rsc-data',
41+
name: 'RSC data routing',
42+
path: '/index.rsc',
43+
},
44+
])
45+
})
46+
47+
it('should not return manifest entries for static appDir routes without dataRoutes', async () => {
48+
const prerenderManifest: PrerenderManifest = {
49+
...basePrerenderManifest,
50+
routes: {
51+
'/api/hello': {
52+
initialRevalidateSeconds: false,
53+
srcRoute: '/api/hello',
54+
dataRoute: null,
55+
},
56+
},
57+
}
58+
const appPathRoutesManifest = {
59+
'/api/hello/route': '/api/hello',
60+
}
61+
const edgeManifest = await generateRscDataEdgeManifest({ prerenderManifest, appPathRoutesManifest })
62+
63+
expect(edgeManifest).toEqual([])
64+
})
65+
66+
it('should return manifest entries for dynamic appDir routes', async () => {
67+
const prerenderManifest: PrerenderManifest = {
68+
...basePrerenderManifest,
69+
dynamicRoutes: {
70+
'/blog/[author]': {
71+
routeRegex: '^/blog/([^/]+?)(?:/)?$',
72+
dataRoute: '/blog/[author].rsc',
73+
fallback: null,
74+
dataRouteRegex: '^/blog/([^/]+?)\\.rsc$',
75+
},
76+
},
77+
}
78+
79+
const appPathRoutesManifest = {
80+
'/blog/[author]/page': '/blog/[author]',
81+
}
82+
const edgeManifest = await generateRscDataEdgeManifest({ prerenderManifest, appPathRoutesManifest })
83+
84+
expect(edgeManifest).toEqual([
85+
{
86+
function: 'rsc-data',
87+
name: 'RSC data routing',
88+
pattern: '^/blog/([^/]+?)(?:/)?$',
89+
},
90+
{
91+
function: 'rsc-data',
92+
name: 'RSC data routing',
93+
pattern: '^/blog/([^/]+?)\\.rsc$',
94+
},
95+
])
96+
})
97+
98+
it('should not return manifest entries for dynamic appDir routes without dataRouteRegex', async () => {
99+
const prerenderManifest: PrerenderManifest = {
100+
...basePrerenderManifest,
101+
dynamicRoutes: {
102+
'/api/[endpoint]': {
103+
routeRegex: '^/api/([^/]+?)(?:/)?$',
104+
dataRoute: '/api/[endpoint].rsc',
105+
fallback: null,
106+
dataRouteRegex: null,
107+
},
108+
},
109+
}
110+
111+
const appPathRoutesManifest = {
112+
'/api/[endpoint]/route': '/api/[endpoint]',
113+
}
114+
const edgeManifest = await generateRscDataEdgeManifest({ prerenderManifest, appPathRoutesManifest })
115+
116+
expect(edgeManifest).toEqual([])
117+
})
118+
})

0 commit comments

Comments
 (0)