Skip to content
Closed
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
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
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'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

if (!response.headers?.['content-type']?.startsWith('text/html'))
Expand All @@ -87,6 +87,32 @@
> [!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) => {

Check failure on line 99 in README.md

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 2 spaces but found 1 tab

Check failure on line 99 in README.md

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character
// Skip internal nuxt routes (e.g. error page)

Check failure on line 100 in README.md

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 100 in README.md

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character
if (getRequestURL(event).pathname.startsWith('/_nuxt')) return

Check failure on line 101 in README.md

View workflow job for this annotation

GitHub Actions / lint

Expect newline after if

Check failure on line 101 in README.md

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 101 in README.md

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character

const header = getResponseHeader(event, 'content-type')

Check failure on line 103 in README.md

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 103 in README.md

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character
const values = Array.isArray(header) ? header : [header]

Check failure on line 104 in README.md

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character
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.
Expand Down