diff --git a/src/helpers/cache.ts b/src/helpers/cache.ts index 0f17d33523..ec6b6300e4 100644 --- a/src/helpers/cache.ts +++ b/src/helpers/cache.ts @@ -1,9 +1,29 @@ -import { join } from 'path' +import { existsSync } from 'fs' +import { join, resolve } from 'path' -export const restoreCache = async ({ cache, publish }) => { - const cacheDir = join(publish, 'cache') +import { shouldSkip } from './utils' + +const findDistDir = (publish) => { + // In normal operation, the dist dir is the same as the publish dir + if (!shouldSkip()) { + return publish + } + // In this situation, the user has disabled the plugin, which means that they might be using next export, + // so we'll look in a few places to find the site root. This allows us to find the .next directory. + for (const root of [resolve(publish, '..'), resolve(publish, '..', '..')]) { + if (existsSync(join(root, 'next.config.js'))) { + return join(root, '.next') + } + } + return null +} - if (await cache.restore(cacheDir)) { +export const restoreCache = async ({ cache, publish }) => { + const distDir = findDistDir(publish) + if (!distDir) { + return + } + if (await cache.restore(join(distDir, 'cache'))) { console.log('Next.js cache restored.') } else { console.log('No Next.js cache to restore.') @@ -11,10 +31,11 @@ export const restoreCache = async ({ cache, publish }) => { } export const saveCache = async ({ cache, publish }) => { - const cacheDir = join(publish, 'cache') - - const buildManifest = join(publish, 'build-manifest.json') - if (await cache.save(cacheDir, { digests: [buildManifest] })) { + const distDir = findDistDir(publish) + if (!distDir) { + return + } + if (await cache.save(join(distDir, 'cache'))) { console.log('Next.js cache saved.') } else { console.log('No Next.js cache to save.') diff --git a/test/index.js b/test/index.js index 04f27bf565..990ec59fc3 100644 --- a/test/index.js +++ b/test/index.js @@ -438,9 +438,7 @@ describe('onPostBuild', () => { utils: { ...utils, cache: { save }, functions: { list: jest.fn().mockResolvedValue([]) } }, }) - expect(save).toHaveBeenCalledWith(path.resolve('.next/cache'), { - digests: [path.resolve('.next/build-manifest.json')], - }) + expect(save).toHaveBeenCalledWith(path.resolve('.next/cache')) }) test('warns if old functions exist', async () => {