Skip to content

Commit 8f4aaec

Browse files
committed
fix: correctly cache when using next export
1 parent b8834a4 commit 8f4aaec

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/helpers/cache.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
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+
if (!shouldSkip()) {
8+
return publish
9+
}
10+
// In this situation, the user has disabled the plugin, which means that they might be using next export,
11+
// so we'll look in a few places to finx the site root
12+
for (const root of [resolve(publish, '..'), resolve(publish, '..', '..')]) {
13+
if (existsSync(join(root, 'next.config.js'))) {
14+
return join(root, '.next')
15+
}
16+
}
17+
return null
18+
}
519

6-
if (await cache.restore(cacheDir)) {
20+
export const restoreCache = async ({ cache, publish }) => {
21+
const distDir = findDistDir(publish)
22+
if (!distDir) {
23+
return
24+
}
25+
if (await cache.restore(join(distDir, 'cache'))) {
726
console.log('Next.js cache restored.')
827
} else {
928
console.log('No Next.js cache to restore.')
1029
}
1130
}
1231

1332
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] })) {
33+
const distDir = findDistDir(publish)
34+
if (!distDir) {
35+
return
36+
}
37+
if (await cache.save(join(distDir, 'cache'))) {
1838
console.log('Next.js cache saved.')
1939
} else {
2040
console.log('No Next.js cache to save.')

0 commit comments

Comments
 (0)