-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-enable nextjs compression or allow enabling it optionally #475
Comments
We probably don't want to re enable this. The way to do this properly is probably with a custom wrapper https://open-next.js.org/config/custom_overrides#custom-wrapper Something like that should do the trick // customWrapper.ts
import streamingWrapper from 'open-next/wrappers/aws-lambda.js'
import {WrapperHandler} from 'open-next/types/open-next'
const handler : WrapperHandler = async (handler, converter) => {
const defaultHandler = await streamingWrapper.wrapper(handler, converter)
return async (event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> => {
const acceptHeaders = event.headers["accept-encoding"];
const result = await defaultHandler(event)
const compressedBody = // Here you compress with node zlib result.body
return {
...result,
headers: {
...result.headers,
'content-encoding': 'br' // Or whatever
},
body: compressedBody
}
}
}
export default {
wrapper: handler,
name: "custom-aws-lambda",
supportStreaming: false,
}; |
Thanks a lot for the reply, that seems like the way to go. I'm using open-next with SST Ion currently, which uses open-next internally. |
You need a custom |
Thanks, just figured it out.
In my case, thats the only place where I'd need it and where it would have a notable effect. Or do you mean middleware responses or external rewrites would break in case that would be enabled? |
It probably wouldn't break anything in most cases (it might still break things for earlier version of next). |
I was able to get gzip compression working and it did solve my lambda response issues, thanks for the hints! What’s left now is that there seems to be some issue with cookies being swallowed, at least that’s what I think is hapoening since signins to my app fail although there’s a successful response and no cookies are set. Anz chance there are some known issues with this when a custom wrapper is used? From what I see, my wrapper leaves all existing headers as they are and only adds Content-Encoding and Content-Length |
This should have no effect at all, custom wrapper uses the exact same logic as integrated wrapper. |
I just tried on a different app which uses Payload CMS aswell where Login / Cookies worked on the deployment to add the same wrapper for compression. Sorry, this is going a bit beyond the scope of the original issue, but I believe it could also help other people running into response size limits. This is what I'm using currently. I think I'm passing everything from the orignal response properly.
|
Another detail I just found when comparing the API route for login from a previous regular deployment that did not have the custom wrapper with the one that has it and is now missing the While the wrapped one now has a gzip encoding header, the regular doesn't have any. This might be related, maybe it's also out of the scope of open-next: sst/sst#4404 |
I don't see how just using a custom wrapper by itself could cause an issue. The code for importing the wrapper is this https://github.com/sst/open-next/blob/a43fa46e9aeaf18b3c68647d1ef2beca3b05f290/packages/open-next/src/core/resolve.ts#L30-L42 |
It indeed was! Again, thanks a lot! |
I'll keep this open since I believe adding compression could be more straightforward. Maybe some implementation that would align with the next.config But of course feel free to close if you think otherwise. Since the same app that failed due to the respose size worked on Vercel, I assume they also compress at the lambda level, or have some other limits with AWS than regular accounts. |
Although Cloudfront does brotli compression on its own, there are good reasons to have the nextjs lambda compress on its own.to avoid hitting the 6 MB Limit.
In my case when using Payload CMS, a 10 MB file shrinks to ~400 kb using the nextjs gzip compression.
This comment mentions that compression which appareantly caused issues in earlier version actually seems fine now:
#35 (comment)
The text was updated successfully, but these errors were encountered: