Skip to content

Commit 8774be3

Browse files
authored
Merge 7c31d90 into b8834a4
2 parents b8834a4 + 7c31d90 commit 8774be3

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/helpers/cache.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1-
import { join } from 'path'
1+
import { existsSync } from 'fs'
2+
import { join, resolve } from 'path'
23

3-
export const restoreCache = async ({ cache, publish }) => {
4-
const cacheDir = join(publish, 'cache')
4+
import { shouldSkip } from './utils'
5+
6+
const findDistDir = (publish) => {
7+
// In normal operation, the dist dir is the same as the publish dir
8+
if (!shouldSkip()) {
9+
return publish
10+
}
11+
// In this situation, the user has disabled the plugin, which means that they might be using next export,
12+
// so we'll look in a few places to find the site root. This allows us to find the .next directory.
13+
for (const root of [resolve(publish, '..'), resolve(publish, '..', '..')]) {
14+
if (existsSync(join(root, 'next.config.js'))) {
15+
return join(root, '.next')
16+
}
17+
}
18+
return null
19+
}
520

6-
if (await cache.restore(cacheDir)) {
21+
export const restoreCache = async ({ cache, publish }) => {
22+
const distDir = findDistDir(publish)
23+
if (!distDir) {
24+
return
25+
}
26+
if (await cache.restore(join(distDir, 'cache'))) {
727
console.log('Next.js cache restored.')
828
} else {
929
console.log('No Next.js cache to restore.')
1030
}
1131
}
1232

1333
export const saveCache = async ({ cache, publish }) => {
14-
const cacheDir = join(publish, 'cache')
15-
16-
const buildManifest = join(publish, 'build-manifest.json')
17-
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
34+
const distDir = findDistDir(publish)
35+
if (!distDir) {
36+
return
37+
}
38+
if (await cache.save(join(distDir, 'cache'))) {
1839
console.log('Next.js cache saved.')
1940
} else {
2041
console.log('No Next.js cache to save.')

test/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,7 @@ describe('onPostBuild', () => {
438438
utils: { ...utils, cache: { save }, functions: { list: jest.fn().mockResolvedValue([]) } },
439439
})
440440

441-
expect(save).toHaveBeenCalledWith(path.resolve('.next/cache'), {
442-
digests: [path.resolve('.next/build-manifest.json')],
443-
})
441+
expect(save).toHaveBeenCalledWith(path.resolve('.next/cache'))
444442
})
445443

446444
test('warns if old functions exist', async () => {

0 commit comments

Comments
 (0)