Skip to content

fix: routes with null data routes can be filtered now #2018

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 8 commits into from
Apr 4, 2023
10 changes: 7 additions & 3 deletions packages/runtime/src/templates/edge-shared/rsc-data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { EdgeFunction } from 'https://edge.netlify.com'

// These are copied from next/dist/build. This file gets copied as part of the next
// runtime build and can't reference the next package directly.
//
// Latest types at https://github.com/vercel/next.js/blob/4a2df3c3752aeddc50fd5ab053440eccf71ae50b/packages/next/src/build/index.ts#L140
export declare type SsgRoute = {
initialRevalidateSeconds: number | false
srcRoute: string | null
dataRoute: string
dataRoute: string | null
}
export declare type DynamicSsgRoute = {
routeRegex: string
fallback: string | null | false
dataRoute: string
dataRoute: string | null
dataRouteRegex: string
}
export declare type PrerenderManifest = {
Expand All @@ -35,7 +39,7 @@ const rscifyPath = (route: string) => {
export const getRscDataRouter = ({ routes: staticRoutes, dynamicRoutes }: PrerenderManifest): EdgeFunction => {
const staticRouteSet = new Set(
Object.entries(staticRoutes)
.filter(([, { dataRoute }]) => dataRoute.endsWith('.rsc'))
.filter(([, { dataRoute }]) => dataRoute?.endsWith('.rsc'))
.map(([route]) => route),
)

Expand Down
40 changes: 40 additions & 0 deletions test/rsc-data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getRscDataRouter, PrerenderManifest } from '../packages/runtime/src/templates/edge-shared/rsc-data'

const basePrerenderManifest: PrerenderManifest = {
version: 3,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
}

describe('getRscDataRouter', () => {
it('should create a RSC data router when data routes are not present for routes', () => {
const manifest: PrerenderManifest = {
...basePrerenderManifest,
routes: {
'/': {
initialRevalidateSeconds: 1,
srcRoute: null,
dataRoute: '/index.json.rsc',
},
'/api/hello': {
initialRevalidateSeconds: false,
srcRoute: '/api/hello',
dataRoute: null,
},
},
}

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