|
1 |
| -import { join } from 'path' |
| 1 | +import { existsSync } from 'fs' |
| 2 | +import { join, resolve } from 'path' |
2 | 3 |
|
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 | +} |
5 | 20 |
|
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'))) { |
7 | 27 | console.log('Next.js cache restored.')
|
8 | 28 | } else {
|
9 | 29 | console.log('No Next.js cache to restore.')
|
10 | 30 | }
|
11 | 31 | }
|
12 | 32 |
|
13 | 33 | 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'))) { |
18 | 39 | console.log('Next.js cache saved.')
|
19 | 40 | } else {
|
20 | 41 | console.log('No Next.js cache to save.')
|
|
0 commit comments