You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use code below to compress response before output using brotlli/gzip/deflate method, it's working good when response 200, but for 404/500 error pages this code doesn't work in async page:
import{Readable,pipeline}from'node:stream';importzlibfrom'node:zlib';exportdefaultdefineNitroPlugin((nitroApp)=>{nitroApp.hooks.hook('beforeResponse',async(event,response)=>{compressResponse(event,response);});});asyncfunctioncompressResponse(event: any,response: {body?: unknown}){if(typeofresponse.body!=='string'){return;}// using deflate as fallback here bcz stackblitz doesn't include it in req headers// You can test locally without setting fallbackconstacceptableEncoding=getRequestHeader(event,'accept-encoding')??'deflate';if(!acceptableEncoding||!/\b(deflate|gzip|br)\b/.test(acceptableEncoding)){return;}constoriginalBody=response.body;conststream=Readable.from(response.body);if(/\bbr\b/.test(acceptableEncoding)){setResponseHeader(event,'Content-Encoding','br');response.body=pipeline(stream,zlib.createBrotliCompress(),event.node.res,()=>(response.body=originalBody));}elseif(/\bgzip\b/.test(acceptableEncoding)){setResponseHeader(event,'Content-Encoding','gzip');response.body=pipeline(stream,zlib.createGzip(),event.node.res,()=>(response.body=originalBody));}else{setResponseHeader(event,'Content-Encoding','deflate');response.body=pipeline(stream,zlib.createDeflate(),event.node.res,()=>(response.body=originalBody));}}
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered:
Environment
Working directory: /home/projects/nuxt-starter-xkjsve 1:20:34 PM
Nuxt project info: 1:20:34 PM
👉 Report an issue: https://github.com/nuxt/nuxt/issues/new 1:20:34 PM
👉 Suggest an improvement: https://github.com/nuxt/nuxt/discussions/new
👉 Read documentation: https://nuxt.com
Reproduction
https://stackblitz.com/edit/nuxt-starter-xkjsve
Describe the bug
I use code below to compress response before output using brotlli/gzip/deflate method, it's working good when response 200, but for 404/500 error pages this code doesn't work in async page:
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: