Skip to content

fix: handle monorepo caching #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ module.exports = {
'unicorn/filename-case': 0,
'unicorn/no-array-push-push': 0,
},
env: {
jest: true,
},
overrides: [...overrides],
}
15 changes: 7 additions & 8 deletions helpers/cacheBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ const path = require('path')
const DEFAULT_DIST_DIR = '.next'

// Account for possible custom distDir
const getPath = (distDir, source) => {
return path.join(distDir || DEFAULT_DIST_DIR, source)
}
const getPath = (nextRoot, distDir, source) => path.join(nextRoot, distDir || DEFAULT_DIST_DIR, source)

const restoreCache = async ({ cache, distDir }) => {
const cacheDir = getPath(distDir, 'cache')
const restoreCache = async ({ cache, distDir, nextRoot }) => {
const cacheDir = getPath(nextRoot, distDir, 'cache')
if (await cache.restore(cacheDir)) {
console.log('Next.js cache restored.')
} else {
console.log('No Next.js cache to restore.')
}
}

const saveCache = async ({ cache, distDir }) => {
const cacheDir = getPath(distDir, 'cache')
const buildManifest = getPath(distDir, 'build-manifest.json')
const saveCache = async ({ cache, distDir, nextRoot }) => {
const cacheDir = getPath(nextRoot, distDir, 'cache')

const buildManifest = getPath(nextRoot, distDir, 'build-manifest.json')
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
console.log('Next.js cache saved.')
} else {
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global
`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.`,
)
}
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
},
async onBuild({
netlifyConfig,
Expand Down Expand Up @@ -119,7 +119,7 @@ See https://ntl.fyi/remove-plugin for instructions.
const nextRoot = getNextRoot({ netlifyConfig })

const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
utils.status.show({
title: 'Essential Next.js Build Plugin ran successfully',
Expand Down
35 changes: 8 additions & 27 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,16 @@ describe('preBuild()', () => {
test('restores cache with right paths', async () => {
await useFixture('dist_dir_next_config')

let distPath
const utils_ = {
...utils,
cache: {
restore: (x) => (distPath = x),
},
}
const spy = jest.spyOn(utils_.cache, 'restore')
const restore = jest.fn()

await plugin.onPreBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
utils: utils_,
utils: { ...utils, cache: { restore } },
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(spy).toHaveBeenCalled()
expect(path.normalize(distPath)).toBe(path.normalize('build/cache'))
expect(restore).toHaveBeenCalledWith(path.resolve('build/cache'))
})
})

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

let distPath
let manifestPath
const utils_ = {
...utils,
cache: {
save: (x, y) => {
distPath = x
manifestPath = y
},
},
}
const spy = jest.spyOn(utils_.cache, 'save')
const save = jest.fn()

await plugin.onPostBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
utils: utils_,
utils: { ...utils, cache: { save } },
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(spy).toHaveBeenCalled()
expect(path.normalize(distPath)).toBe(path.normalize('build/cache'))
expect(path.normalize(manifestPath.digests[0])).toBe(path.normalize('build/build-manifest.json'))
expect(save).toHaveBeenCalledWith(path.resolve('build/cache'), {
digests: [path.resolve('build/build-manifest.json')],
})
})
})

Expand Down