diff --git a/README.md b/README.md index 16639cf..76595a5 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ import { useCompression } from 'h3-compression' export default defineNitroPlugin((nitro) => { nitro.hooks.hook('render:response', async (response, { event }) => { // Skip internal nuxt routes (e.g. error page) - if (getRequestURL(event).pathname.startsWith('/__nuxt')) + if (getRequestURL(event).pathname.startsWith('/_nuxt')) return if (!response.headers?.['content-type']?.startsWith('text/html')) @@ -87,6 +87,32 @@ export default defineNitroPlugin((nitro) => { > [!NOTE] > `useCompressionStream` doesn't work right now in nitro. So you just can use `useCompression` +## Nuxt 4 + +If you want to use it in nuxt 4 you can define a nitro plugin with an other hook + +`server/plugins/compression.ts` +````ts +import { useCompression } from 'h3-compression' + +export default defineNitroPlugin((nitro) => { + nitro.hooks.hook('beforeResponse', async (event, res) => { + // Skip internal nuxt routes (e.g. error page) + if (getRequestURL(event).pathname.startsWith('/_nuxt')) return + + const header = getResponseHeader(event, 'content-type') + const values = Array.isArray(header) ? header : [header] + const compress = values.find(t => typeof t === 'string' && t.includes('text/html')) + + if (!compress) return + + await useCompression(event, res) + }) +}) +```` +> [!NOTE] +> `useCompressionStream` doesn't work right now in nitro. So you just can use `useCompression` + ## Utilities H3-compression has a concept of composable utilities that accept `event` (from `eventHandler((event) => {})`) as their first argument and `response` as their second.