Skip to content

Instantly share code, notes, and snippets.

@sladg
Created November 20, 2023 14:54
Show Gist options
  • Save sladg/4e7beed6d45c4401247cf3cc72e6d617 to your computer and use it in GitHub Desktop.
Save sladg/4e7beed6d45c4401247cf3cc72e6d617 to your computer and use it in GitHub Desktop.
Code to put on top of `server.js` to alter behaviour of Next's internal caching
const fs = require('fs')
const path = require('path')
const { patchFs } = require('@aleung/fs-monkey')
const resolveToTmp = (pathLike) => {
const isCacheDir = pathLike.includes('.next/cache')
if (!isCacheDir) return pathLike
const endPath = pathLike.split('.next/cache/')[1]
console.log('patcher.js: resolveToTmp', endPath)
return path.join('/tmp', endPath)
}
const defaultRewrite =
(fn) =>
(pathLike, ...rest) =>
fn(resolveToTmp(pathLike), ...rest)
const nextFs = {
existsSync: defaultRewrite(fs.existsSync),
mkdir: defaultRewrite(fs.mkdir),
mkdirSync: defaultRewrite(fs.mkdirSync),
readFile: defaultRewrite(fs.readFile),
readFileSync: defaultRewrite(fs.readFileSync),
stat: defaultRewrite(fs.stat),
statSync: defaultRewrite(fs.statSync),
writeFile: defaultRewrite(fs.writeFile),
writeFileSync: defaultRewrite(fs.writeFileSync),
rm: defaultRewrite(fs.rm),
rmSync: defaultRewrite(fs.rmSync),
promises: {
mkdir: defaultRewrite(fs.promises.mkdir),
readFile: defaultRewrite(fs.promises.readFile),
stat: defaultRewrite(fs.promises.stat),
writeFile: defaultRewrite(fs.promises.writeFile),
rm: defaultRewrite(fs.promises.rm),
},
}
patchFs(nextFs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment