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: call handleRawCacheData in purgeTags event handler #82

Merged
merged 1 commit into from
Nov 14, 2024
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
2 changes: 1 addition & 1 deletion playground-disk/pages/cachedPageFromDisk.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const random = useState('random_data', () => {
})

useRouteCache((helper) => {
helper.setCacheable()
helper.setCacheable().addTags(['test_tag'])
})
</script>
9 changes: 7 additions & 2 deletions src/runtime/server/api/purgeTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readBody, defineEventHandler, createError } from 'h3'
import {
decodeComponentCacheItem,
decodeRouteCacheItem,
handleRawCacheData,
} from '../../helpers/cacheItem'
import { useMultiCacheApp } from '../utils/useMultiCacheApp'
import { onlyUnique } from '../../helpers/server'
Expand Down Expand Up @@ -94,12 +95,16 @@ export class DebouncedInvalidator {
return (item as any).cacheTags
}
} else if (cacheName === 'route') {
const cached = await this.cacheContext?.[cacheName]?.getItemRaw(key)
const cached = handleRawCacheData(
await this.cacheContext?.[cacheName]?.getItemRaw(key),
)
if (cached) {
return decodeRouteCacheItem(cached)?.cacheTags
}
} else if (cacheName === 'component') {
const cached = await this.cacheContext?.[cacheName]?.getItemRaw(key)
const cached = handleRawCacheData(
await this.cacheContext?.[cacheName]?.getItemRaw(key),
)
if (cached) {
return decodeComponentCacheItem(cached)?.cacheTags
}
Expand Down
7 changes: 7 additions & 0 deletions test/__helpers__/getRouteCacheItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { $fetch } from '@nuxt/test-utils/e2e'

export default function (): Promise<any> {
return $fetch(`/__nuxt_multi_cache/stats/route`, {
method: 'get',
})
}
9 changes: 9 additions & 0 deletions test/__helpers__/purgeTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { $fetch } from '@nuxt/test-utils/e2e'

export default function purgeTags(tags: string[] | string) {
const body: string[] = Array.isArray(tags) ? tags : [tags]
return $fetch(`/__nuxt_multi_cache/purge/tags`, {
method: 'post',
body: JSON.stringify(body),
})
}
20 changes: 18 additions & 2 deletions test/fileSystemDriver.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import path from 'path'
import { setup } from '@nuxt/test-utils/e2e'
import { describe, test, expect } from 'vitest'
import type { NuxtMultiCacheOptions } from '../src/runtime/types'
import { createPageWithoutHydration } from './__helpers__'
import { createPageWithoutHydration, sleep } from './__helpers__'
import purgeAll from './__helpers__/purgeAll'
import getRouteCacheItems from './__helpers__/getRouteCacheItems'
import purgeByKey from './__helpers__/purgeByKey'
import purgeTags from './__helpers__/purgeTags'
import exp from 'constants'

const multiCache: NuxtMultiCacheOptions = {
route: {
Expand All @@ -15,7 +19,7 @@ const multiCache: NuxtMultiCacheOptions = {
api: {
enabled: true,
authorization: false,
cacheTagInvalidationDelay: 5000,
cacheTagInvalidationDelay: 10,
},
}

Expand Down Expand Up @@ -66,4 +70,16 @@ describe('Caching with the file system driver', () => {
const text3 = await page3.locator('#cached-component-number').innerText()
expect(text3).to.not.equal(text1)
})

test('correctly invalidates by tag for FS cache items', async () => {
await purgeAll()
await createPageWithoutHydration('/cachedPageFromDisk', 'en')
const data1 = await getRouteCacheItems()
expect(data1?.rows).toHaveLength(1)
await purgeTags('test_tag')
await sleep(1000)

const data2 = await getRouteCacheItems()
expect(data2?.rows).toHaveLength(0)
})
})