Skip to content

Commit 883d7c8

Browse files
committed
feat: cache .next/cache between builds
1 parent 60b3489 commit 883d7c8

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

helpers/cacheBuild.js

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

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const nextOnNetlify = require('./src/index.js')
77

88
const validateNextUsage = require('./helpers/validateNextUsage')
99
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
10+
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
1011

1112
const pWriteFile = util.promisify(fs.writeFile)
1213

@@ -15,7 +16,7 @@ const pWriteFile = util.promisify(fs.writeFile)
1516
// - Between the build and postbuild steps, any functions are bundled
1617

1718
module.exports = {
18-
async onPreBuild({ netlifyConfig, packageJson, utils }) {
19+
async onPreBuild({ inputs, netlifyConfig, packageJson, utils }) {
1920
const { failBuild } = utils.build
2021

2122
validateNextUsage(failBuild)
@@ -25,6 +26,8 @@ module.exports = {
2526
return failBuild('Could not find a package.json for this project')
2627
}
2728

29+
await restoreCache({ inputs, utils })
30+
2831
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
2932
return
3033
}
@@ -58,6 +61,9 @@ module.exports = {
5861

5962
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
6063
},
64+
async onPostBuild({ inputs }) {
65+
await saveCache({ inputs, utils })
66+
},
6167
}
6268

6369
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'

0 commit comments

Comments
 (0)