From 916f1d3ab709674b6fe5cf040b488fd8fa4d0b59 Mon Sep 17 00:00:00 2001 From: Jan Hug Date: Thu, 14 Nov 2024 15:40:14 +0100 Subject: [PATCH] call handleRawCacheData in purgeTags event handler (#79) --- playground-disk/pages/cachedPageFromDisk.vue | 2 +- src/runtime/server/api/purgeTags.ts | 9 +++++++-- test/__helpers__/getRouteCacheItems.ts | 7 +++++++ test/__helpers__/purgeTags.ts | 9 +++++++++ test/fileSystemDriver.e2e.spec.ts | 20 ++++++++++++++++++-- 5 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 test/__helpers__/getRouteCacheItems.ts create mode 100644 test/__helpers__/purgeTags.ts diff --git a/playground-disk/pages/cachedPageFromDisk.vue b/playground-disk/pages/cachedPageFromDisk.vue index ba84f68..82272ed 100644 --- a/playground-disk/pages/cachedPageFromDisk.vue +++ b/playground-disk/pages/cachedPageFromDisk.vue @@ -10,6 +10,6 @@ const random = useState('random_data', () => { }) useRouteCache((helper) => { - helper.setCacheable() + helper.setCacheable().addTags(['test_tag']) }) diff --git a/src/runtime/server/api/purgeTags.ts b/src/runtime/server/api/purgeTags.ts index c2786eb..f472dfa 100644 --- a/src/runtime/server/api/purgeTags.ts +++ b/src/runtime/server/api/purgeTags.ts @@ -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' @@ -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 } diff --git a/test/__helpers__/getRouteCacheItems.ts b/test/__helpers__/getRouteCacheItems.ts new file mode 100644 index 0000000..81d5784 --- /dev/null +++ b/test/__helpers__/getRouteCacheItems.ts @@ -0,0 +1,7 @@ +import { $fetch } from '@nuxt/test-utils/e2e' + +export default function (): Promise { + return $fetch(`/__nuxt_multi_cache/stats/route`, { + method: 'get', + }) +} diff --git a/test/__helpers__/purgeTags.ts b/test/__helpers__/purgeTags.ts new file mode 100644 index 0000000..812497d --- /dev/null +++ b/test/__helpers__/purgeTags.ts @@ -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), + }) +} diff --git a/test/fileSystemDriver.e2e.spec.ts b/test/fileSystemDriver.e2e.spec.ts index 20d580b..1ee3034 100644 --- a/test/fileSystemDriver.e2e.spec.ts +++ b/test/fileSystemDriver.e2e.spec.ts @@ -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: { @@ -15,7 +19,7 @@ const multiCache: NuxtMultiCacheOptions = { api: { enabled: true, authorization: false, - cacheTagInvalidationDelay: 5000, + cacheTagInvalidationDelay: 10, }, } @@ -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) + }) })