diff --git a/playground/server/api/testApiCompression.ts b/playground/server/api/testApiCompression.ts new file mode 100644 index 0000000..bb2df83 --- /dev/null +++ b/playground/server/api/testApiCompression.ts @@ -0,0 +1,10 @@ +import { defineEventHandler } from 'h3' +import { useRouteCache } from '#nuxt-multi-cache/composables' + +export default defineEventHandler((event) => { + useRouteCache((helper) => { + helper.setCacheable().setMaxAge(234234) + }, event) + const number = Math.round(Math.random() * 1000000000) + return 'This is a compressed API response: ' + number +}) diff --git a/playground/server/plugins/compression.ts b/playground/server/plugins/compression.ts index 97bd225..2e9212c 100644 --- a/playground/server/plugins/compression.ts +++ b/playground/server/plugins/compression.ts @@ -3,7 +3,10 @@ import { defineNitroPlugin } from 'nitropack/runtime' export default defineNitroPlugin((nitro) => { nitro.hooks.hook('beforeResponse', async (event, response) => { - if (!event.path.startsWith('/testCompression')) { + if ( + !event.path.startsWith('/testCompression') && + !event.path.startsWith('/api/testApiCompression') + ) { return } diff --git a/test/routeCacheWithCompression.e2e.spec.ts b/test/routeCacheWithCompression.e2e.spec.ts index 9b19c2c..c43541e 100644 --- a/test/routeCacheWithCompression.e2e.spec.ts +++ b/test/routeCacheWithCompression.e2e.spec.ts @@ -35,42 +35,50 @@ await setup({ nuxtConfig, }) -describe.only('The route cache with compression enabled', () => { - test('caches a page', async () => { - await purgeAll() +async function testResponse(path: string) { + // First call puts it into cache. + const first = await fetch(path, { + method: 'get', + headers: { + 'accept-encoding': 'br', + }, + }) + + // Get the encoding. + const firstEncoding = first.headers.get('content-encoding') - // First call puts it into cache. - const first = await fetch('/testCompression', { - method: 'get', - headers: { - 'accept-encoding': 'br', - }, - }) + // Test that the compression feature has actually compressed the response. + expect(firstEncoding).toEqual('br') - // Get the encoding. - const firstEncoding = first.headers.get('content-encoding') + // Second call should get it from cache. + const second = await fetch(path, { + method: 'get', + headers: { + 'accept-encoding': 'br', + }, + }) - // Test that the compression feature has actually compressed the response. - expect(firstEncoding).toEqual('br') + const responseFirst = await first.text() + const responseSecond = await second.text() - // Second call should get it from cache. - const second = await fetch('/testCompression', { - method: 'get', - headers: { - 'accept-encoding': 'br', - }, - }) + // Response should be identical (contains a random number). + expect(responseFirst).toEqual(responseSecond) - const responseFirst = await first.text() - const responseSecond = await second.text() + const secondEncoding = second.headers.get('content-encoding') - // Response should be identical (contains a random number). - expect(responseFirst).toEqual(responseSecond) + // The encoding should be the same, because the compression feature was + // able to compress the cached response again. + expect(secondEncoding).toEqual(firstEncoding) +} - const secondEncoding = second.headers.get('content-encoding') +describe.only('The route cache with compression enabled', () => { + test('caches a page', async () => { + await purgeAll() + await testResponse('/testCompression') + }) - // The encoding should be the same, because the compression feature was - // able to compress the cached response again. - expect(secondEncoding).toEqual(firstEncoding) + test('caches the result of an API handler', async () => { + await purgeAll() + await testResponse('/api/testApiCompression') }) })