diff --git a/packages/runtime/src/templates/edge-shared/rsc-data.ts b/packages/runtime/src/templates/edge-shared/rsc-data.ts index 07fc3e232b..b54c3a6d2c 100644 --- a/packages/runtime/src/templates/edge-shared/rsc-data.ts +++ b/packages/runtime/src/templates/edge-shared/rsc-data.ts @@ -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 = { @@ -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), ) diff --git a/test/rsc-data.spec.ts b/test/rsc-data.spec.ts new file mode 100644 index 0000000000..9071ccf9c4 --- /dev/null +++ b/test/rsc-data.spec.ts @@ -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') + }) +})