Skip to content

Commit

Permalink
fix: file-system-cache revalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
gtjamesa committed Nov 15, 2023
1 parent abd47e6 commit 39fc776
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default class FileSystemCache implements CacheHandler {
let data = memoryCache?.get(key)

if (this.debug) {
console.log('get', key, tags, kindHint)
console.log('get', key, tags, kindHint, !!data)
}

// let's check the disk for seed data
Expand Down Expand Up @@ -196,7 +196,7 @@ export default class FileSystemCache implements CacheHandler {
}

if (data.value?.kind === 'FETCH') {
const storedTags = data.value?.data?.tags
const storedTags = data.value?.tags

// update stored tags if a new one is being added
// TODO: remove this when we can send the tags
Expand Down Expand Up @@ -321,7 +321,7 @@ export default class FileSystemCache implements CacheHandler {
lastModified: Date.now(),
})
if (this.debug) {
console.log('set', key, !!data)
console.log('set', key)
}

if (!this.flushToDisk) return
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/response-cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export interface CachedFetchValue {
body: string
url: string
status?: number
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
tags?: string[]
}
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
tags?: string[]
revalidate: number
}

Expand Down
80 changes: 80 additions & 0 deletions test/unit/incremental-cache/file-system-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,83 @@ describe('FileSystemCache', () => {
})
})
})

describe('FileSystemCache (isrMemory 0)', () => {
const fsCache = new FileSystemCache({
_appDir: true,
_pagesDir: true,
_requestHeaders: {},
flushToDisk: true,
fs: nodeFs,
serverDistDir: cacheDir,
revalidatedTags: [],
experimental: {
ppr: false,
},
maxMemoryCacheSize: 0, // disable memory cache
})

it('should cache fetch', async () => {
await fsCache.set(
'fetch-cache',
{
kind: 'FETCH',
data: {
headers: {},
body: 'MTcwMDA1NjM4MQ==',
status: 200,
url: 'http://my-api.local',
},
revalidate: 30,
},
{
fetchCache: true,
revalidate: 30,
fetchUrl: 'http://my-api.local',
fetchIdx: 5,
tags: ['server-time'],
}
)

const res = await fsCache.get('fetch-cache', {
tags: ['server-time'],
kindHint: 'fetch',
})

expect(res.value).toEqual({
kind: 'FETCH',
data: {
headers: {},
body: 'MTcwMDA1NjM4MQ==',
status: 200,
url: 'http://my-api.local',
},
revalidate: 30,
tags: ['server-time'],
})
})

it('should cache unstable_cache', async () => {
await fsCache.set(
'unstable-cache',
{
kind: 'FETCH',
data: { headers: {}, body: '1700056381', status: 200, url: '' },
revalidate: 30,
},
{ revalidate: 30, fetchCache: true, tags: ['server-time2'] }
)

const res = await fsCache.get('unstable-cache', {
tags: ['server-time'],
kindHint: 'fetch',
})

expect(res.value).toEqual({
kind: 'FETCH',
data: { headers: {}, body: '1700056381', status: 200, url: '' },
revalidate: 30,
tags: ['server-time2'],
})
})
})

0 comments on commit 39fc776

Please # to comment.