1
+ /* eslint-disable max-lines-per-function, max-lines */
1
2
const { promises, createWriteStream, existsSync } = require ( 'fs' )
2
3
const { Server } = require ( 'http' )
3
4
const { tmpdir } = require ( 'os' )
@@ -11,7 +12,8 @@ const fetch = require('node-fetch')
11
12
const makeHandler =
12
13
( ) =>
13
14
// We return a function and then call `toString()` on it to serialise it as the launcher function
14
- ( conf , app , pageRoot , staticManifest = [ ] ) => {
15
+ // eslint-disable-next-line max-params
16
+ ( conf , app , pageRoot , staticManifest = [ ] , mode = 'ssr' ) => {
15
17
// This is just so nft knows about the page entrypoints. It's not actually used
16
18
try {
17
19
// eslint-disable-next-line node/no-missing-require
@@ -33,16 +35,23 @@ const makeHandler =
33
35
const cacheDir = path . join ( tmpdir ( ) , 'next-static-cache' )
34
36
// Grab the real fs.promises.readFile...
35
37
const readfileOrig = promises . readFile
38
+ const writeFileOrig = promises . writeFile
39
+ const mkdirOrig = promises . mkdir
36
40
// ...then money-patch it to see if it's requesting a CDN file
37
41
promises . readFile = async ( file , options ) => {
38
42
// We only care about page files
39
43
if ( file . startsWith ( pageRoot ) ) {
40
44
// We only want the part after `pages/`
41
45
const filePath = file . slice ( pageRoot . length + 1 )
46
+ const cacheFile = path . join ( cacheDir , filePath )
47
+
48
+ if ( existsSync ( cacheFile ) ) {
49
+ return readfileOrig ( cacheFile , options )
50
+ }
51
+
42
52
// Is it in the CDN and not local?
43
53
if ( staticFiles . has ( filePath ) && ! existsSync ( file ) ) {
44
54
// This name is safe to use, because it's one that was already created by Next
45
- const cacheFile = path . join ( cacheDir , filePath )
46
55
// Have we already cached it? We ignore the cache if running locally to avoid staleness
47
56
if ( ( ! existsSync ( cacheFile ) || process . env . NETLIFY_DEV ) && base ) {
48
57
await promises . mkdir ( path . dirname ( cacheFile ) , { recursive : true } )
@@ -65,6 +74,27 @@ const makeHandler =
65
74
66
75
return readfileOrig ( file , options )
67
76
}
77
+
78
+ promises . writeFile = async ( file , data , options ) => {
79
+ if ( file . startsWith ( pageRoot ) ) {
80
+ const filePath = file . slice ( pageRoot . length + 1 )
81
+ const cacheFile = path . join ( cacheDir , filePath )
82
+ await promises . mkdir ( path . dirname ( cacheFile ) , { recursive : true } )
83
+ return writeFileOrig ( cacheFile , data , options )
84
+ }
85
+
86
+ return writeFileOrig ( file , data , options )
87
+ }
88
+
89
+ promises . mkdir = async ( dir , options ) => {
90
+ if ( dir . startsWith ( pageRoot ) ) {
91
+ const filePath = dir . slice ( pageRoot . length + 1 )
92
+ const cachePath = path . join ( cacheDir , filePath )
93
+ return mkdirOrig ( cachePath , options )
94
+ }
95
+
96
+ return mkdirOrig ( dir , options )
97
+ }
68
98
}
69
99
let NextServer
70
100
try {
@@ -139,9 +169,11 @@ const makeHandler =
139
169
}
140
170
141
171
// Sending SWR headers causes undefined behaviour with the Netlify CDN
142
- if ( multiValueHeaders [ 'cache-control' ] ?. [ 0 ] ?. includes ( 'stale-while-revalidate' ) ) {
172
+ const cacheHeader = multiValueHeaders [ 'cache-control' ] ?. [ 0 ]
173
+ if ( cacheHeader ?. includes ( 'stale-while-revalidate' ) ) {
143
174
multiValueHeaders [ 'cache-control' ] = [ 'public, max-age=0, must-revalidate' ]
144
175
}
176
+ multiValueHeaders [ 'x-render-mode' ] = [ mode ]
145
177
146
178
return {
147
179
...result ,
@@ -171,9 +203,10 @@ const path = require("path");
171
203
const pageRoot = path.resolve(path.join(__dirname, "${ publishDir } ", config.target === "server" ? "server" : "serverless", "pages"));
172
204
exports.handler = ${
173
205
isODB
174
- ? `builder((${ makeHandler ( ) . toString ( ) } )(config, "${ appDir } ", pageRoot, staticManifest));`
175
- : `(${ makeHandler ( ) . toString ( ) } )(config, "${ appDir } ", pageRoot, staticManifest);`
206
+ ? `builder((${ makeHandler ( ) . toString ( ) } )(config, "${ appDir } ", pageRoot, staticManifest, 'odb' ));`
207
+ : `(${ makeHandler ( ) . toString ( ) } )(config, "${ appDir } ", pageRoot, staticManifest, 'ssr' );`
176
208
}
177
209
`
178
210
179
211
module . exports = getHandler
212
+ /* eslint-enable max-lines-per-function, max-lines */
0 commit comments