1
1
/* eslint-disable import/no-extraneous-dependencies */
2
- import { existsSync } from 'node:fs'
3
- import { resolve , join } from 'node:path'
4
2
import { cpus } from 'os'
3
+ import path from 'path'
5
4
6
5
import { NetlifyPluginConstants } from '@netlify/build'
7
- import type { NetlifyConfig } from '@netlify/build'
8
- import pkg from 'fs-extra'
9
- import { copy , move , remove , readJSON } from 'fs-extra/esm'
6
+ import { copy , move , remove } from 'fs-extra/esm'
10
7
import { globby } from 'globby'
11
8
import { outdent } from 'outdent'
12
9
import pLimit from 'p-limit'
13
10
14
- import { netliBlob , getNormalizedBlobKey } from './blobs.cjs'
11
+ import { getNormalizedBlobKey , netliBlob } from './blobs/blobs.cjs'
12
+ import { removeFileDir , formatPageData } from './blobs/cacheFormat.js'
15
13
import { BUILD_DIR , STANDALONE_BUILD_DIR } from './constants.js'
16
14
17
- // readfile not available in esm version of fs-extra
18
- const { readFile } = pkg
19
15
/**
20
16
* Move the Next.js build output from the publish dir to a temp dir
21
17
*/
@@ -57,10 +53,12 @@ export const storePrerenderedContent = async ({ NETLIFY_API_TOKEN, SITE_ID }:
57
53
Uploading Files to Blob Storage...
58
54
` )
59
55
60
- const uploadFilesToBlob = async ( key : string , file : string ) => {
56
+ const uploadFilesToBlob = async ( pathName : string , file : string ) => {
57
+ const key = path . basename ( pathName , path . extname ( pathName ) )
58
+ const content = await formatPageData ( pathName , key , file )
59
+ console . log ( content )
61
60
try {
62
- const content = await readFile ( join ( BUILD_DIR , file ) , 'utf8' )
63
- await blob . set ( getNormalizedBlobKey ( key ) , content )
61
+ await blob . setJSON ( getNormalizedBlobKey ( key ) , content )
64
62
} catch ( error ) {
65
63
console . error ( error )
66
64
}
@@ -71,24 +69,18 @@ export const storePrerenderedContent = async ({ NETLIFY_API_TOKEN, SITE_ID }:
71
69
72
70
prerenderedContent . map ( ( rawPath ) => {
73
71
const cacheFile = rawPath . startsWith ( 'cache' )
72
+ const hasHtml = Boolean ( rawPath . endsWith ( '.html' ) )
73
+
74
74
// Removing app, page, and cache/fetch-cache from file path
75
75
const pathKey = removeFileDir ( rawPath , ( cacheFile ? 2 : 1 ) )
76
+ const errorPages = pathKey . includes ( '404' ) || pathKey . includes ( '500' )
77
+
76
78
// Checking for blob access before uploading
77
- if ( blob ) {
79
+ if ( hasHtml && ! errorPages ) {
78
80
return limit ( uploadFilesToBlob , pathKey , rawPath )
79
81
}
80
82
} )
81
- }
82
-
83
- const removeFileDir = ( file : string , num : number ) => {
84
- return file . split ( '/' ) . slice ( num ) . join ( '/' )
85
- }
86
-
87
- const maybeLoadJson = < T > ( path : string ) : Promise < T > | null => existsSync ( path ) ? readJSON ( path ) : null
88
-
89
- // use this to load any manifest file by passing in name and/or dir if needed
90
- export const loadManifest = ( netlifyConfig : NetlifyConfig , manifest : string , dir ?: string ) : any =>
91
- maybeLoadJson ( resolve ( netlifyConfig . build . publish , dir ?? '' , manifest ) )
83
+ }
92
84
93
85
/**
94
86
* Move static assets to the publish dir so they are uploaded to the CDN
0 commit comments