From 779cae66606d75006f59d51867c38ca6a8342c0c Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Thu, 13 Apr 2023 16:07:26 -0400 Subject: [PATCH 1/2] fix: now data routes for dynamic routes filter even when null when creating the RSC data router --- .../src/templates/edge-shared/rsc-data.ts | 2 +- test/rsc-data.spec.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/runtime/src/templates/edge-shared/rsc-data.ts b/packages/runtime/src/templates/edge-shared/rsc-data.ts index b54c3a6d2c..1d2dcd89d6 100644 --- a/packages/runtime/src/templates/edge-shared/rsc-data.ts +++ b/packages/runtime/src/templates/edge-shared/rsc-data.ts @@ -44,7 +44,7 @@ export const getRscDataRouter = ({ routes: staticRoutes, dynamicRoutes }: Preren ) const dynamicRouteMatcher = Object.values(dynamicRoutes) - .filter(({ dataRoute }) => dataRoute.endsWith('.rsc')) + .filter(({ dataRoute }) => dataRoute?.endsWith('.rsc')) .map(({ routeRegex }) => new RegExp(routeRegex)) const matchesDynamicRscDataRoute = (pathname: string) => { diff --git a/test/rsc-data.spec.ts b/test/rsc-data.spec.ts index 9071ccf9c4..377cc94cc5 100644 --- a/test/rsc-data.spec.ts +++ b/test/rsc-data.spec.ts @@ -37,4 +37,36 @@ describe('getRscDataRouter', () => { expect(typeof rscDataRouter).toBe('function') }) + + it('should filter dynamic routes when creating the RSC data router', () => { + const manifest: PrerenderManifest = { + ...basePrerenderManifest, + dynamicRoutes: { + '/[slug]': { + routeRegex: '^\\/(?[^\\/]+?)(?:\\/)?$', + fallback: null, + dataRoute: null, + dataRouteRegex: '^\\/(?[^\\/]+?)(?:\\/)?$', + }, + '/[slug]/[slug2]': { + routeRegex: '^\\/(?[^\\/]+?)(?:\\/)(?[^\\/]+?)(?:\\/)?$', + fallback: null, + dataRoute: '/[slug]/[slug2].json.rsc', + dataRouteRegex: '^\\/(?[^\\/]+?)(?:\\/)(?[^\\/]+?)(?:\\/)?$', + }, + }, + } + + let rscDataRouter + + // Normally type checking would pick this up, but because this file is copied when generating + // edge functions for the build, we need to make sure it's valid for builds. + // + // See https://github.com/netlify/next-runtime/issues/1940 + expect(() => { + rscDataRouter = getRscDataRouter(manifest) + }).not.toThrow() + + expect(typeof rscDataRouter).toBe('function') + }) }) From 2d71951d5a7e380c5ce7dc140bfa590c042170e8 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Thu, 13 Apr 2023 16:09:44 -0400 Subject: [PATCH 2/2] test: updated test descriptions --- test/rsc-data.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/rsc-data.spec.ts b/test/rsc-data.spec.ts index 377cc94cc5..27418c72ec 100644 --- a/test/rsc-data.spec.ts +++ b/test/rsc-data.spec.ts @@ -8,7 +8,7 @@ const basePrerenderManifest: PrerenderManifest = { } describe('getRscDataRouter', () => { - it('should create a RSC data router when data routes are not present for routes', () => { + it('should filter static routes when creating the RSC data router', () => { const manifest: PrerenderManifest = { ...basePrerenderManifest, routes: {