Skip to content

fix: now data routes for dynamic routes filter even when null when creating the RSC data router #2041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2023
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
2 changes: 1 addition & 1 deletion packages/runtime/src/templates/edge-shared/rsc-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the description of the PR this wasn't being caught during compilation of the Next runtime as this file is copied.

.map(({ routeRegex }) => new RegExp(routeRegex))

const matchesDynamicRscDataRoute = (pathname: string) => {
Expand Down
34 changes: 33 additions & 1 deletion test/rsc-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: '^\\/(?<slug>[^\\/]+?)(?:\\/)?$',
fallback: null,
dataRoute: null,
dataRouteRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)?$',
},
'/[slug]/[slug2]': {
routeRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)(?<slug2>[^\\/]+?)(?:\\/)?$',
fallback: null,
dataRoute: '/[slug]/[slug2].json.rsc',
dataRouteRegex: '^\\/(?<slug>[^\\/]+?)(?:\\/)(?<slug2>[^\\/]+?)(?:\\/)?$',
},
},
}

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')
})
})