Skip to content
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

fix: correctly cache when using next export #1223

Merged
merged 1 commit into from
Feb 17, 2022
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
37 changes: 29 additions & 8 deletions src/helpers/cache.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
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.')
}
}

export const saveCache = async ({ cache, publish }) => {
const cacheDir = join(publish, 'cache')

const buildManifest = join(publish, 'build-manifest.json')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm: was removing build-manifest.json as the digests option intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, because it seems like it's not an accurate indicator of whether the cache has changed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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.')
Expand Down
4 changes: 1 addition & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down