sladg / nextjs-lambda

Lambda deployments for Nextjs12 & Nextjs13 (standalone). Easy CLI commands to get your standalone Next output to run in AWS Lambda (not @Edge)! Uses CDK in behind and produces code zips importable to Terraform, Serverless, Azure, etc.
MIT License
169 stars 19 forks source link

Bundle image handler with Sharp #45

Closed sladg closed 1 year ago

sladg commented 1 year ago

Existing implementation was error prone, creating unnecessary complexity and layers. There needs to be easier way how to bundle image handler and include Sharp for Lambda as well.

Currently using Squoosh for image handling, which is not recommended for production use with Vercel.

khuezy commented 1 year ago

I'd be interested in how you're bundling sharp into the lambda Function/NodejsFunction. Sharp spawns a child process and executes the sharp binary, it might not be possible to bundle the sharp code into the function.

sladg commented 1 year ago

@khuezy Lambda on it's own does not care about child processes, the problem is typical in binaries that needs to be compiled specifically for Lambda's environment. Also, Next uses rather shitty implementation for figuring out where sharp is, so that might also be pain.

Will send code once done :)

sladg commented 1 year ago

Next's built-in image handler will be replaced by custom solution as it ultimately sucks. Bundling is overly complicated, passing binaries does not work as expected, hacking all around is needed.

Related to #64

Will be optionally secured by providing allowed image sources. Expected in version 4 (in couple of days).

sladg commented 1 year ago

As of v4, image optimizer was moved to separate package. Changes: https://github.com/sladg/nextjs-lambda/compare/release/v3.22.0...v4.0.0 New optimizer: https://github.com/sladg/imaginex-lambda