1
+ import { basename , extname } from 'path'
2
+
1
3
import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
2
4
import bridgeFile from '@vercel/node-bridge'
3
5
import chalk from 'chalk'
4
6
import destr from 'destr'
5
7
import { copyFile , ensureDir , existsSync , readJSON , writeFile , writeJSON , stat } from 'fs-extra'
8
+ import type { PrerenderManifest } from 'next/dist/build'
6
9
import type { ImageConfigComplete , RemotePattern } from 'next/dist/shared/lib/image-config'
7
10
import { outdent } from 'outdent'
8
11
import { join , relative , resolve , dirname } from 'pathe'
@@ -300,6 +303,34 @@ const getBundleWeight = async (patterns: string[]) => {
300
303
return sum ( sizes . flat ( 1 ) )
301
304
}
302
305
306
+ const changeExtension = ( file : string , extension : string ) => {
307
+ const base = basename ( file , extname ( file ) )
308
+ return join ( dirname ( file ) , base + extension )
309
+ }
310
+
311
+ const getSSRDependencies = async ( publish : string ) : Promise < string [ ] > => {
312
+ const prerenderManifest : PrerenderManifest = await readJSON ( join ( publish , 'prerender-manifest.json' ) )
313
+
314
+ return Object . entries ( prerenderManifest . routes ) . flatMap ( ( [ route , ssgRoute ] ) => {
315
+ if ( ssgRoute . initialRevalidateSeconds === false ) {
316
+ return [ ]
317
+ }
318
+
319
+ if ( ssgRoute . dataRoute . endsWith ( '.rsc' ) ) {
320
+ return [
321
+ join ( publish , 'server' , 'app' , ssgRoute . dataRoute ) ,
322
+ join ( publish , 'server' , 'app' , changeExtension ( ssgRoute . dataRoute , '.html' ) ) ,
323
+ ]
324
+ }
325
+
326
+ const trimmedPath = route === '/' ? 'index' : route . slice ( 1 )
327
+ return [
328
+ join ( publish , 'server' , 'pages' , `${ trimmedPath } .html` ) ,
329
+ join ( publish , 'server' , 'pages' , `${ trimmedPath } .json` ) ,
330
+ ]
331
+ } )
332
+ }
333
+
303
334
export const getSSRLambdas = async ( publish : string , baseDir : string ) : Promise < SSRLambda [ ] > => {
304
335
const commonDependencies = await getCommonDependencies ( publish , baseDir )
305
336
const ssrRoutes = await getSSRRoutes ( publish )
@@ -308,6 +339,8 @@ export const getSSRLambdas = async (publish: string, baseDir: string): Promise<S
308
339
const nonOdbRoutes = ssrRoutes
309
340
const odbRoutes = ssrRoutes
310
341
342
+ const ssrDependencies = await getSSRDependencies ( publish )
343
+
311
344
return [
312
345
{
313
346
functionName : HANDLER_FUNCTION_NAME ,
@@ -316,14 +349,14 @@ export const getSSRLambdas = async (publish: string, baseDir: string): Promise<S
316
349
} ,
317
350
{
318
351
functionName : ODB_FUNCTION_NAME ,
319
- includedFiles : [ ...commonDependencies , ...odbRoutes . flatMap ( ( route ) => route . includedFiles ) ] ,
352
+ includedFiles : [ ...commonDependencies , ...odbRoutes . flatMap ( ( route ) => route . includedFiles ) , ... ssrDependencies ] ,
320
353
routes : odbRoutes ,
321
354
} ,
322
355
]
323
356
}
324
357
325
358
// TODO: check if there's any other glob specialties missing
326
- const escapeGlob = ( path : string ) => path . replace ( / \[ / g, '*' ) . replace ( / \ ]/ g, '*' )
359
+ const escapeGlob = ( path : string ) => path . replace ( / \[ / g, '*' ) . replace ( / ] / g, '*' )
327
360
328
361
const getSSRRoutes = async ( publish : string ) : Promise < RouteConfig [ ] > => {
329
362
const pages = ( await readJSON ( join ( publish , 'server' , 'pages-manifest.json' ) ) ) as Record < string , string >
0 commit comments