-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcache.ts
33 lines (26 loc) · 975 Bytes
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { existsSync } from 'node:fs'
import { rm } from 'node:fs/promises'
import { join } from 'node:path'
import type { PluginContext } from './plugin-context.js'
export const saveBuildCache = async (ctx: PluginContext) => {
const { cache } = ctx.utils
const cacheDir = join(ctx.publishDir, 'cache')
if (existsSync(cacheDir)) {
// remove the fetch responses because they are never updated once
// created at build time and would always be stale if saved
await rm(join(cacheDir, 'fetch-cache'), { recursive: true, force: true })
await cache.save(cacheDir)
console.log('Next.js cache saved')
} else {
console.log('No Next.js cache to save')
}
}
export const restoreBuildCache = async (ctx: PluginContext) => {
const { cache } = ctx.utils
const cacheDir = join(ctx.publishDir, 'cache')
if (await cache.restore(cacheDir)) {
console.log('Next.js cache restored')
} else {
console.log('No Next.js cache to restore')
}
}