Skip to content

Commit e810b4b

Browse files
committed
feat: cache .next/cache between builds
1 parent 2953faa commit e810b4b

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

helpers/cacheBuild.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const path = require('path')
2+
3+
const DEFAULT_DIST_DIR = '.next'
4+
5+
// Account for possible custom distDir
6+
const getPath = (distDir, source) => {
7+
return path.join(distDir || DEFAULT_DIST_DIR, source)
8+
}
9+
10+
const restoreCache = async ({ cache, distDir }) => {
11+
const cacheDir = getPath(distDir, 'cache')
12+
if (await cache.restore(cacheDir)) {
13+
console.log('Next.js cache restored.')
14+
} else {
15+
console.log('No Next.js cache to restore.')
16+
}
17+
}
18+
19+
const saveCache = async ({ cache, distDir }) => {
20+
const cacheDir = getPath(distDir, 'cache')
21+
const buildManifest = getPath(distDir, 'build-manifest.json')
22+
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
23+
console.log('Next.js cache saved.')
24+
} else {
25+
console.log('No Next.js cache to save.')
26+
}
27+
}
28+
29+
module.exports = {
30+
restoreCache,
31+
saveCache,
32+
}

helpers/isStaticExportProject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const isStaticExportProject = ({ build, scripts }) => {
1717

1818
if (isStaticExport) {
1919
console.log(
20-
`Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
20+
'NOTE: Static HTML export Next.js projects (projects that use `next export`) do not require most of this plugin. For these sites, this plugin *only* caches builds.',
2121
)
2222
}
2323

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const validateNextUsage = require('./helpers/validateNextUsage')
99
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
1010
const getNextConfig = require('./helpers/getNextConfig')
1111
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
12+
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
1213

1314
const pWriteFile = util.promisify(fs.writeFile)
1415

@@ -27,6 +28,9 @@ module.exports = {
2728
return failBuild('Could not find a package.json for this project')
2829
}
2930

31+
const nextConfig = await getNextConfig(utils.failBuild)
32+
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
33+
3034
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
3135
return
3236
}
@@ -60,12 +64,14 @@ module.exports = {
6064

6165
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
6266
},
67+
6368
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
6469
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
6570
return
6671
}
6772

6873
const nextConfig = await getNextConfig(utils.failBuild)
74+
await saveCache({cache: utils.cache, distDir: nextConfig.distDir })
6975
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
7076
},
7177
}

test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const utils = {
1818
throw new Error(message)
1919
},
2020
},
21+
cache: {
22+
save() {},
23+
restore() {},
24+
}
2125
}
2226

2327
// Temporary switch cwd

0 commit comments

Comments
 (0)