Skip to content

Commit a9d74ea

Browse files
Merge pull request #11 from netlify/caching
feat: build caching
2 parents 96a7c7d + 510a001 commit a9d74ea

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

src/helpers/cacheBuild.js

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

src/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
const path = require('path')
33

4+
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
45
const copyNextDist = require('./helpers/copyNextDist')
56
const generateFunctions = require('./helpers/generateFunctions')
67
const getNextConfig = require('./helpers/getNextConfig')
@@ -20,6 +21,7 @@ module.exports = {
2021
packageJson,
2122
utils: {
2223
build: { failBuild },
24+
cache,
2325
},
2426
}) {
2527
if (shouldSkipPlugin({ netlifyConfig, packageJson, failBuild })) {
@@ -36,6 +38,8 @@ module.exports = {
3638
verifyBuildTarget(target)
3739

3840
setIncludedFiles({ netlifyConfig, distDir })
41+
42+
await restoreCache({ cache, distDir, siteRoot })
3943
},
4044

4145
async onBuild({
@@ -68,4 +72,16 @@ module.exports = {
6872
copyNextDist(siteRoot)
6973
}
7074
},
75+
76+
async onPostBuild({
77+
netlifyConfig,
78+
utils: {
79+
build: { failBuild },
80+
cache,
81+
},
82+
}) {
83+
const siteRoot = getNextRoot({ netlifyConfig })
84+
const { distDir } = await getNextConfig(failBuild, siteRoot)
85+
await saveCache({ cache, distDir, siteRoot })
86+
},
7187
}

test/index.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('preBuild()', () => {
245245
).rejects.toThrow(`You set your publish directory to "${wrongPublishDir}". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.`)
246246
})
247247

248-
test('fail build if no publish dir', async () => {
248+
test('fail build if no publish dir', async () => {
249249
expect(
250250
plugin.onPreBuild({
251251
netlifyConfig: { build: { command: 'npm run build', publish: null } },
@@ -256,20 +256,21 @@ describe('preBuild()', () => {
256256
).rejects.toThrow('You set your publish directory to "null". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.')
257257
})
258258

259-
// test('restores cache with right paths', async () => {
260-
// await useFixture('dist_dir_next_config')
259+
test('restores cache with right paths', async () => {
260+
await useFixture('dist_dir_next_config')
261+
netlifyConfig.build.publish = path.join(process.cwd(), 'build')
261262

262-
// const restore = jest.fn()
263+
const restore = jest.fn()
263264

264-
// await plugin.onPreBuild({
265-
// netlifyConfig,
266-
// packageJson: DUMMY_PACKAGE_JSON,
267-
// utils: { ...utils, cache: { restore } },
268-
// constants: { FUNCTIONS_SRC: 'out_functions' },
269-
// })
265+
await plugin.onPreBuild({
266+
netlifyConfig,
267+
packageJson,
268+
utils: { ...utils, cache: { restore } },
269+
constants: { FUNCTIONS_SRC: 'out_functions' },
270+
})
270271

271-
// expect(restore).toHaveBeenCalledWith(path.resolve('build/cache'))
272-
// })
272+
expect(restore).toHaveBeenCalledWith(path.resolve('build/cache'))
273+
})
273274
})
274275

275276
describe('onBuild()', () => {
@@ -322,22 +323,22 @@ describe('onBuild()', () => {
322323
})
323324

324325
describe('onPostBuild', () => {
325-
// test('saves cache with right paths', async () => {
326-
// await useFixture('dist_dir_next_config')
327-
328-
// const save = jest.fn()
329-
330-
// await plugin.onPostBuild({
331-
// netlifyConfig,
332-
// packageJson: DUMMY_PACKAGE_JSON,
333-
// utils: { ...utils, cache: { save } },
334-
// constants: { FUNCTIONS_SRC: 'out_functions' },
335-
// })
336-
337-
// expect(save).toHaveBeenCalledWith(path.resolve('build/cache'), {
338-
// digests: [path.resolve('build/build-manifest.json')],
339-
// })
340-
// })
326+
test('saves cache with right paths', async () => {
327+
await useFixture('dist_dir_next_config')
328+
329+
const save = jest.fn()
330+
331+
await plugin.onPostBuild({
332+
netlifyConfig,
333+
packageJson,
334+
utils: { ...utils, cache: { save } },
335+
constants: { FUNCTIONS_SRC: 'out_functions' },
336+
})
337+
338+
expect(save).toHaveBeenCalledWith(path.resolve('build/cache'), {
339+
digests: [path.resolve('build/build-manifest.json')],
340+
})
341+
})
341342
})
342343

343344
describe('script parser', () => {

0 commit comments

Comments
 (0)