Skip to content

Commit 8361468

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

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/templates/getHandler.js

Lines changed: 41 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,24 @@ 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+
console.log('returning from cache', cacheFile)
50+
return readfileOrig(cacheFile, options)
51+
}
52+
4253
// Is it in the CDN and not local?
4354
if (staticFiles.has(filePath) && !existsSync(file)) {
4455
// This name is safe to use, because it's one that was already created by Next
45-
const cacheFile = path.join(cacheDir, filePath)
4656
// Have we already cached it? We ignore the cache if running locally to avoid staleness
4757
if ((!existsSync(cacheFile) || process.env.NETLIFY_DEV) && base) {
4858
await promises.mkdir(path.dirname(cacheFile), { recursive: true })
@@ -65,6 +75,29 @@ const makeHandler =
6575

6676
return readfileOrig(file, options)
6777
}
78+
79+
promises.writeFile = async (file, data, options) => {
80+
if (file.startsWith(pageRoot)) {
81+
const filePath = file.slice(pageRoot.length + 1)
82+
const cacheFile = path.join(cacheDir, filePath)
83+
console.log('writing', cacheFile)
84+
await promises.mkdir(path.dirname(cacheFile), { recursive: true })
85+
return writeFileOrig(cacheFile, data, options)
86+
}
87+
88+
return writeFileOrig(file, data, options)
89+
}
90+
91+
promises.mkdir = async (dir, options) => {
92+
if (dir.startsWith(pageRoot)) {
93+
const filePath = dir.slice(pageRoot.length + 1)
94+
const cachePath = path.join(cacheDir, filePath)
95+
console.log('creating', cachePath)
96+
return mkdirOrig(cachePath, options)
97+
}
98+
99+
return mkdirOrig(dir, options)
100+
}
68101
}
69102
let NextServer
70103
try {
@@ -139,9 +172,11 @@ const makeHandler =
139172
}
140173

141174
// Sending SWR headers causes undefined behaviour with the Netlify CDN
142-
if (multiValueHeaders['cache-control']?.[0]?.includes('stale-while-revalidate')) {
175+
const cacheHeader = multiValueHeaders['cache-control']?.[0]
176+
if (cacheHeader?.includes('stale-while-revalidate')) {
143177
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
144178
}
179+
multiValueHeaders['x-render-mode'] = [mode]
145180

146181
return {
147182
...result,
@@ -171,9 +206,10 @@ const path = require("path");
171206
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", config.target === "server" ? "server" : "serverless", "pages"));
172207
exports.handler = ${
173208
isODB
174-
? `builder((${makeHandler().toString()})(config, "${appDir}", pageRoot, staticManifest));`
175-
: `(${makeHandler().toString()})(config, "${appDir}", pageRoot, staticManifest);`
209+
? `builder((${makeHandler().toString()})(config, "${appDir}", pageRoot, staticManifest, 'odb'));`
210+
: `(${makeHandler().toString()})(config, "${appDir}", pageRoot, staticManifest, 'ssr');`
176211
}
177212
`
178213

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

0 commit comments

Comments
 (0)