1
- /* eslint-disable import/no-extraneous-dependencies */
2
1
import { cpus } from 'os'
3
2
import path from 'path'
4
3
5
4
import { NetlifyPluginConstants } from '@netlify/build'
6
5
import { copy , move , remove } from 'fs-extra/esm'
7
6
import { globby } from 'globby'
8
- import { outdent } from 'outdent'
9
7
import pLimit from 'p-limit'
10
8
11
- import { getNormalizedBlobKey , netliBlob } from './blobs/blobs.cjs '
12
- import { removeFileDir , formatBlobContent } from './blobs/cacheFormat .js'
13
- import { NEXT_DIR , STANDALONE_BUILD_DIR } from './constants.js'
9
+ import { netliBlob } from './blobs.js '
10
+ import { buildCacheValue } from './cache .js'
11
+ import { BUILD_DIR } from './constants.js'
14
12
15
13
/**
16
14
* Move the Next.js build output from the publish dir to a temp dir
17
15
*/
18
16
export const stashBuildOutput = async ( { PUBLISH_DIR } : NetlifyPluginConstants ) => {
19
- await move ( PUBLISH_DIR , `${ NEXT_DIR } ` , { overwrite : true } )
17
+ await move ( PUBLISH_DIR , `${ BUILD_DIR } /.next ` , { overwrite : true } )
20
18
21
19
// remove prerendered content from the standalone build (it's also in the main build dir)
22
- const prerenderedContent = await getPrerenderedContent ( STANDALONE_BUILD_DIR )
20
+ const prerenderedContent = await getPrerenderedContent ( ` ${ BUILD_DIR } /.next/standalone` , false )
23
21
await Promise . all (
24
- prerenderedContent . map ( ( file : string ) => remove ( `${ STANDALONE_BUILD_DIR } / ${ file } ` ) ) ) . catch ( ( error ) => console . error ( error ) )
25
-
22
+ prerenderedContent . map ( ( file : string ) => remove ( `${ BUILD_DIR } /.next/standalone/ ${ file } ` ) ) ,
23
+ ) . catch ( ( error ) => console . error ( error ) )
26
24
}
27
25
28
26
/**
29
27
* Glob for prerendered content in the build output
30
28
*/
31
- const getPrerenderedContent = async ( cwd : string ) : Promise < string [ ] > => {
29
+ const getPrerenderedContent = async ( cwd : string , get = true ) : Promise < string [ ] > => {
32
30
// TODO: test this
33
- return await globby ( [
34
- `cache/fetch-cache/*` ,
35
- `server/+(app|pages)/**/*.+(html|json|rsc|body|meta)` ,
36
- `!server/**/*.js.nft.{html,json}` ,
37
- ] , { cwd, extglob : true } )
31
+ return await globby (
32
+ get
33
+ ? [ `cache/fetch-cache/*` , `server/+(app|pages)/**/*.+(html|body)` ]
34
+ : [
35
+ `cache/fetch-cache/*` ,
36
+ `server/+(app|pages)/**/*.+(html|json|rsc|body|meta)` ,
37
+ `!server/**/*.js.nft.{html,json}` ,
38
+ ] ,
39
+ { cwd, extglob : true } ,
40
+ )
38
41
}
39
42
40
43
/**
41
44
* Upload prerendered content from the main build dir to the blob store
42
45
*/
43
- export const storePrerenderedContent = async ( { NETLIFY_API_TOKEN , SITE_ID } :
44
- {
45
- NETLIFY_API_TOKEN : string ,
46
- SITE_ID : string
47
- } ) => {
48
- const deployID = `${ process . env . DEPLOY_ID } `
49
- const blob = netliBlob ( NETLIFY_API_TOKEN , deployID , SITE_ID )
50
- // todo: Check out setFiles within Blobs.js to see how to upload files to blob storage
51
- const limit = pLimit ( Math . max ( 2 , cpus ( ) . length ) )
52
- console . log (
53
- outdent `
54
- Uploading Files to Blob Storage...
55
- ` )
56
-
57
- const uploadFilesToBlob = async ( pathName : string , file : string ) => {
58
- const key = path . basename ( pathName , path . extname ( pathName ) )
59
- const content = await formatBlobContent ( pathName , key , file )
60
- try {
61
- await blob . setJSON ( getNormalizedBlobKey ( key ) , content )
62
- } catch ( error ) {
63
- console . error ( error )
64
- }
65
- }
66
-
67
- // todo: test it on demo with pages dir
68
- // eslint-disable-next-line unicorn/no-await-expression-member
69
- ( await getPrerenderedContent ( NEXT_DIR ) ) . map ( ( rawPath : string ) => {
70
- const cacheFile = rawPath . startsWith ( 'cache' )
71
- const pathKey = removeFileDir ( rawPath , ( cacheFile ? 2 : 1 ) )
72
- const files = findCacheFiles ( pathKey , rawPath )
73
-
74
- if ( files ) {
75
- return limit ( uploadFilesToBlob , pathKey , rawPath )
76
- }
77
- } )
78
- }
79
-
80
- const findCacheFiles = ( pathKey : string , rawPath : string ) => {
81
- const errorPages = pathKey . includes ( '404' ) || pathKey . includes ( '500' )
82
- const isFetchCache = rawPath . startsWith ( 'cache/fetch-cache' )
83
- const isRoute = rawPath . endsWith ( '.body' )
84
- const hasHtml = rawPath . endsWith ( '.html' )
46
+ export const storePrerenderedContent = async ( {
47
+ NETLIFY_API_TOKEN ,
48
+ SITE_ID ,
49
+ } : {
50
+ NETLIFY_API_TOKEN : string
51
+ SITE_ID : string
52
+ } ) => {
53
+ const deployID = `${ process . env . DEPLOY_ID } `
54
+ const blob = netliBlob ( NETLIFY_API_TOKEN , deployID , SITE_ID )
55
+ // todo: Check out setFiles within Blobs.js to see how to upload files to blob storage
56
+ const limit = pLimit ( Math . max ( 2 , cpus ( ) . length ) )
85
57
86
- if ( hasHtml && ! errorPages || isFetchCache || isRoute ) {
87
- return true
88
- }
58
+ const prerenderedContent = await getPrerenderedContent ( `${ BUILD_DIR } /.next` )
59
+ return Promise . all (
60
+ prerenderedContent . map ( async ( rawPath : string ) => {
61
+ // TODO: test this with files that have a double extension
62
+ const ext = path . extname ( rawPath )
63
+ const key = rawPath . replace ( ext , '' )
64
+ const value = await buildCacheValue ( key , ext )
65
+ return limit ( ( ) => blob . setJSON ( key , value ) )
66
+ } ) ,
67
+ )
89
68
}
90
69
91
70
/**
@@ -94,6 +73,6 @@ const findCacheFiles = (pathKey: string, rawPath: string) => {
94
73
export const publishStaticAssets = ( { PUBLISH_DIR } : NetlifyPluginConstants ) => {
95
74
return Promise . all ( [
96
75
copy ( 'public' , PUBLISH_DIR ) ,
97
- copy ( `${ NEXT_DIR } /static/` , `${ PUBLISH_DIR } /_next/static` ) ,
76
+ copy ( `${ BUILD_DIR } /.next /static/` , `${ PUBLISH_DIR } /_next/static` ) ,
98
77
] )
99
78
}
0 commit comments