Skip to content

Commit c7bf60a

Browse files
committed
fix: patch fs write methods
1 parent 15daf87 commit c7bf60a

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/templates/getHandler.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines-per-function, max-lines */
12
const { promises, createWriteStream, existsSync } = require('fs')
23
const { Server } = require('http')
34
const { tmpdir } = require('os')
@@ -11,7 +12,8 @@ const fetch = require('node-fetch')
1112
const makeHandler =
1213
() =>
1314
// 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') => {
1517
// This is just so nft knows about the page entrypoints. It's not actually used
1618
try {
1719
// eslint-disable-next-line node/no-missing-require
@@ -33,16 +35,23 @@ const makeHandler =
3335
const cacheDir = path.join(tmpdir(), 'next-static-cache')
3436
// Grab the real fs.promises.readFile...
3537
const readfileOrig = promises.readFile
38+
const writeFileOrig = promises.writeFile
39+
const mkdirOrig = promises.mkdir
3640
// ...then money-patch it to see if it's requesting a CDN file
3741
promises.readFile = async (file, options) => {
3842
// We only care about page files
3943
if (file.startsWith(pageRoot)) {
4044
// We only want the part after `pages/`
4145
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+
4252
// Is it in the CDN and not local?
4353
if (staticFiles.has(filePath) && !existsSync(file)) {
4454
// This name is safe to use, because it's one that was already created by Next
45-
const cacheFile = path.join(cacheDir, filePath)
4655
// Have we already cached it? We ignore the cache if running locally to avoid staleness
4756
if ((!existsSync(cacheFile) || process.env.NETLIFY_DEV) && base) {
4857
await promises.mkdir(path.dirname(cacheFile), { recursive: true })
@@ -65,6 +74,27 @@ const makeHandler =
6574

6675
return readfileOrig(file, options)
6776
}
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+
}
6898
}
6999
let NextServer
70100
try {
@@ -139,9 +169,11 @@ const makeHandler =
139169
}
140170

141171
// 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')) {
143174
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
144175
}
176+
multiValueHeaders['x-render-mode'] = [mode]
145177

146178
return {
147179
...result,
@@ -171,9 +203,10 @@ const path = require("path");
171203
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", config.target === "server" ? "server" : "serverless", "pages"));
172204
exports.handler = ${
173205
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');`
176208
}
177209
`
178210

179211
module.exports = getHandler
212+
/* eslint-enable max-lines-per-function, max-lines */

0 commit comments

Comments
 (0)