Skip to content

Commit 9477e0e

Browse files
authored
fix: handle monorepo caching (#538)
* fix: handle monorepo caching * fix: update tests
1 parent d829d12 commit 9477e0e

File tree

4 files changed

+20
-37
lines changed

4 files changed

+20
-37
lines changed

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ module.exports = {
2525
'unicorn/filename-case': 0,
2626
'unicorn/no-array-push-push': 0,
2727
},
28+
env: {
29+
jest: true,
30+
},
2831
overrides: [...overrides],
2932
}

helpers/cacheBuild.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ const path = require('path')
33
const DEFAULT_DIST_DIR = '.next'
44

55
// Account for possible custom distDir
6-
const getPath = (distDir, source) => {
7-
return path.join(distDir || DEFAULT_DIST_DIR, source)
8-
}
6+
const getPath = (nextRoot, distDir, source) => path.join(nextRoot, distDir || DEFAULT_DIST_DIR, source)
97

10-
const restoreCache = async ({ cache, distDir }) => {
11-
const cacheDir = getPath(distDir, 'cache')
8+
const restoreCache = async ({ cache, distDir, nextRoot }) => {
9+
const cacheDir = getPath(nextRoot, distDir, 'cache')
1210
if (await cache.restore(cacheDir)) {
1311
console.log('Next.js cache restored.')
1412
} else {
1513
console.log('No Next.js cache to restore.')
1614
}
1715
}
1816

19-
const saveCache = async ({ cache, distDir }) => {
20-
const cacheDir = getPath(distDir, 'cache')
21-
const buildManifest = getPath(distDir, 'build-manifest.json')
17+
const saveCache = async ({ cache, distDir, nextRoot }) => {
18+
const cacheDir = getPath(nextRoot, distDir, 'cache')
19+
20+
const buildManifest = getPath(nextRoot, distDir, 'build-manifest.json')
2221
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
2322
console.log('Next.js cache saved.')
2423
} else {

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global
6666
`The Essential Next.js plugin now supports reading image domains from your Next config, so using NEXT_IMAGE_ALLOWED_DOMAINS is now deprecated. Please set images.domains in next.config.js instead, and remove the NEXT_IMAGE_ALLOWED_DOMAINS variable.`,
6767
)
6868
}
69-
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
69+
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
7070
},
7171
async onBuild({
7272
netlifyConfig,
@@ -119,7 +119,7 @@ See https://ntl.fyi/remove-plugin for instructions.
119119
const nextRoot = getNextRoot({ netlifyConfig })
120120

121121
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
122-
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
122+
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
123123
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
124124
utils.status.show({
125125
title: 'Essential Next.js Build Plugin ran successfully',

test/index.js

+8-27
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,16 @@ describe('preBuild()', () => {
235235
test('restores cache with right paths', async () => {
236236
await useFixture('dist_dir_next_config')
237237

238-
let distPath
239-
const utils_ = {
240-
...utils,
241-
cache: {
242-
restore: (x) => (distPath = x),
243-
},
244-
}
245-
const spy = jest.spyOn(utils_.cache, 'restore')
238+
const restore = jest.fn()
246239

247240
await plugin.onPreBuild({
248241
netlifyConfig,
249242
packageJson: DUMMY_PACKAGE_JSON,
250-
utils: utils_,
243+
utils: { ...utils, cache: { restore } },
251244
constants: { FUNCTIONS_SRC: 'out_functions' },
252245
})
253246

254-
expect(spy).toHaveBeenCalled()
255-
expect(path.normalize(distPath)).toBe(path.normalize('build/cache'))
247+
expect(restore).toHaveBeenCalledWith(path.resolve('build/cache'))
256248
})
257249
})
258250

@@ -316,29 +308,18 @@ describe('onPostBuild', () => {
316308
test('saves cache with right paths', async () => {
317309
await useFixture('dist_dir_next_config')
318310

319-
let distPath
320-
let manifestPath
321-
const utils_ = {
322-
...utils,
323-
cache: {
324-
save: (x, y) => {
325-
distPath = x
326-
manifestPath = y
327-
},
328-
},
329-
}
330-
const spy = jest.spyOn(utils_.cache, 'save')
311+
const save = jest.fn()
331312

332313
await plugin.onPostBuild({
333314
netlifyConfig,
334315
packageJson: DUMMY_PACKAGE_JSON,
335-
utils: utils_,
316+
utils: { ...utils, cache: { save } },
336317
constants: { FUNCTIONS_SRC: 'out_functions' },
337318
})
338319

339-
expect(spy).toHaveBeenCalled()
340-
expect(path.normalize(distPath)).toBe(path.normalize('build/cache'))
341-
expect(path.normalize(manifestPath.digests[0])).toBe(path.normalize('build/build-manifest.json'))
320+
expect(save).toHaveBeenCalledWith(path.resolve('build/cache'), {
321+
digests: [path.resolve('build/build-manifest.json')],
322+
})
342323
})
343324
})
344325

0 commit comments

Comments
 (0)