Skip to content cloudfront example middleware
Branch: master
Clone or download
Latest commit d107ea2 Apr 9, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE add cache buster to avoid cache on requests Feb 22, 2018
index.html simplify our SPA html code Apr 4, 2018
prerender-cloudfront.yaml task: update prerender bots list Apr 8, 2019
testimage.png Avoid sending requests for binary objects to Apr 10, 2018 cloudfront example middleware

This is an example of integrating and a SPA in S3 served over Cloudfront.


  1. Upload prerender-cloudfront.yaml to Cloudformation as a new stack, that'll setup the example for you. Enter your token when asked.
  2. Upload code.js and index.html to the S3 bucket created by Cloudformation in step 1.
  3. Change the read permissions for code.js and index.html to be publicly readable.


To see the page as rendered by run:

curl -H 'User-Agent: Facebot' https://${CLOUDFRONT_DOMAIN}/over/here

The same page without pre-rendering:

curl https://${CLOUDFRONT_DOMAIN}/over/here


Two Lambda@edge functions are used. The first detects bot requests on requests entering the system, it sets a header which Cloudfront uses to partition the cache. The second function, run after the cache, detects the presence of the header and, if present, routes the request to


By default, static resources from the bucket are cached for a long time period. This improves performance but means that the deploy process of any real app will need a Cloudfront purge step.

As does NOT want any Cloudfront caching (see we disable that by including a X-Prerender-Cachebuster header which effectively disables cloudfront caching.

NOTE: using X-Prerender-Cachebuster is probably not optimal, if you find a better way, please let me know.

You can’t perform that action at this time.