- AWS Lambda@Edge (enabled Preview)
- One Amazon CloudFront Distribution (origin doesn't matter)
- IAM role (basic execution is enough)
- npm to install Node.js dependencies
The Lambda@Edge will be invoked whenever a new "Viewer Request" event is triggered by CloudFront.
The Lambda Function will behave as follows:
- If the requested resource is NOT available locally (i.e. not an HTML file), the request can proceed to the origin
- If the local template exists, it will be read and rendered using Plates with a few dynamic variables (i.e. "title" and "today)
- The resulting HTML is then minified and eventually compressed, based on the request HTTP headers (response headers are correctly set as well)
- The final HTTP body is directly returned to the client without hitting the CloudFront origin
cd this-gist
npm install
zip -r ../edge-deployment-package.zip ./*
- The deployment package cannot exceed 1MB, and a manual hack was required to include the 'html-minifier' library (i.e. reducing its size from 3MB to 500KB)
Hi @jmmitchell,
That's a good point, and I have to check whether the built-in gzip happens before or after hitting/missing the cache. My Lambda@Edge Function is running on "Viewer Request", which means before trying to hit the cache (or the origin) since I wanted to achieve an "originless" execution.
I'm afraid that CloudFront will add to its cache and gzip only objects that come from the origin or from the "Origin Request/Response" Lambda@Edge Functions, in which case I can't count on the built-in gzip support.
I'll keep you posted :)