|
1 | 1 | import type { EdgeFunction } from 'https://edge.netlify.com'
|
2 | 2 |
|
3 |
| -interface PrerenderedRoute { |
| 3 | +export declare type SsgRoute = { |
4 | 4 | initialRevalidateSeconds: number | false
|
5 | 5 | srcRoute: string | null
|
6 |
| - dataRoute: string | null |
| 6 | + dataRoute: string |
| 7 | +} |
| 8 | +export declare type DynamicSsgRoute = { |
| 9 | + routeRegex: string |
| 10 | + fallback: string | null | false |
| 11 | + dataRoute: string |
| 12 | + dataRouteRegex: string |
| 13 | +} |
| 14 | +export declare type PrerenderManifest = { |
| 15 | + version: 3 |
| 16 | + routes: { |
| 17 | + [route: string]: SsgRoute |
| 18 | + } |
| 19 | + dynamicRoutes: { |
| 20 | + [route: string]: DynamicSsgRoute |
| 21 | + } |
| 22 | + notFoundRoutes: string[] |
7 | 23 | }
|
8 | 24 |
|
9 |
| -export const getRscDataRouter = (prerenderedRoutes: Record<string, PrerenderedRoute>): EdgeFunction => { |
10 |
| - const routeEntries: Array<[string, PrerenderedRoute]> = Object.entries(prerenderedRoutes) |
11 |
| - const routes = new Map(routeEntries) |
| 25 | +const noop = () => {} |
12 | 26 |
|
13 |
| - const rscDataRoutes = new Set( |
14 |
| - routeEntries.map(([, { dataRoute }]) => dataRoute).filter((dataRoute) => dataRoute?.endsWith('.rsc')), |
| 27 | +// Ensure that routes with and without a trailing slash map to different ODB paths |
| 28 | +const rscifyPath = (route: string) => { |
| 29 | + if (route.endsWith('/')) { |
| 30 | + return route + 'index.rsc' |
| 31 | + } |
| 32 | + return route + '.rsc' |
| 33 | +} |
| 34 | + |
| 35 | +export const getRscDataRouter = ({ routes: staticRoutes, dynamicRoutes }: PrerenderManifest): EdgeFunction => { |
| 36 | + const staticRouteSet = new Set( |
| 37 | + Object.entries(staticRoutes) |
| 38 | + .filter(([, { dataRoute }]) => dataRoute.endsWith('.rsc')) |
| 39 | + .map(([route]) => route), |
15 | 40 | )
|
16 | 41 |
|
| 42 | + const dynamicRouteMatcher = Object.values(dynamicRoutes) |
| 43 | + .filter(({ dataRoute }) => dataRoute.endsWith('.rsc')) |
| 44 | + .map(({ dataRouteRegex }) => new RegExp(dataRouteRegex)) |
| 45 | + |
| 46 | + const matchesDynamicRscDataRoute = (pathname: string) => { |
| 47 | + return dynamicRouteMatcher.some((matcher) => matcher.test(pathname)) |
| 48 | + } |
| 49 | + |
| 50 | + const matchesStaticRscDataRoute = (pathname: string) => { |
| 51 | + const key = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname |
| 52 | + return staticRouteSet.has(key) |
| 53 | + } |
| 54 | + |
| 55 | + const matchesRscRoute = (pathname: string) => { |
| 56 | + return matchesStaticRscDataRoute(pathname) || matchesDynamicRscDataRoute(pathname) |
| 57 | + } |
| 58 | + |
17 | 59 | return (request, context) => {
|
18 | 60 | const debug = request.headers.has('x-next-debug-logging')
|
19 |
| - const log = (...args: unknown[]) => { |
20 |
| - if (debug) { |
21 |
| - console.log(...args) |
22 |
| - } |
23 |
| - } |
| 61 | + const log = debug ? (...args: unknown[]) => console.log(...args) : noop |
24 | 62 | const url = new URL(request.url)
|
25 |
| - // Manifest always has the route without a trailing slash |
26 |
| - const pathname = url.pathname.endsWith('/') ? url.pathname.slice(0, -1) : url.pathname |
27 | 63 | // If this is a static RSC request, rewrite to the data route
|
28 | 64 | if (request.headers.get('rsc') === '1') {
|
29 | 65 | log('Is rsc request')
|
30 |
| - const route = routes.get(pathname) |
31 |
| - if (route?.dataRoute) { |
| 66 | + if (matchesRscRoute(url.pathname)) { |
32 | 67 | request.headers.set('x-rsc-route', url.pathname)
|
33 |
| - return context.rewrite(new URL(route.dataRoute, request.url)) |
| 68 | + const target = rscifyPath(url.pathname) |
| 69 | + log('Rewriting to', target) |
| 70 | + return context.rewrite(target) |
34 | 71 | }
|
35 |
| - } else if (rscDataRoutes.has(pathname)) { |
36 |
| - log('Is rsc data request. Adding rsc header') |
37 |
| - request.headers.set('rsc', '1') |
38 | 72 | }
|
39 | 73 | }
|
40 | 74 | }
|
0 commit comments