- I have single page app built with Backbone.js.
- I host app (app consists of static files only) on Amazon S3.
- I use CloudFront as a Bucket CDN.
- App is accessed by
https://myapp.com -> https://abcdefgh34545.cloudfront.com -> https://myBucket.s3-eu-west-1.amazonaws.com/index.html
How I can use Prerender.io
service with this stack? I have to somehow detect that WebSpider/WebRobot is accessing the page and redirect it to prerender.io...
It's hard to use Prerender.io with a static Amazon S3 site.
You could stand up an nginx/apache server in front of s3:
https://myapp.com
->https://mynginx-server.com
->https://myBucket.s3-eu-west-1.amazonaws.com/index.html
This solution is less ideal because you lose the closest-location benefit of cloudfront.
This is a good article about a custom solution: http://www.dave.cx/post/23/prerendering-angular-s3/
David was able to generate the static HTML and save them in S3, then use CloudFlare to detect _escaped_fragment_ in the URL and redirect it to the static HTML on S3.
You can use Lambda@Edge to configure CloudFront to send crawler HTTP requests directly to prerender.io.
The basic idea is to have a viewer-request handler which sets a custom HTTP header for requests which should be sent to prerender.io. For example this Lambda@Edge code:
The cloudfront distribution must be configured to pass through the X-Prerender-Host and X-Prerender-Token headers.
Finally a origin-request handler changes the origin server if X-Prerender-Token is present:
There's a fully worked example at: https://github.com/jinty/prerender-cloudfront
As mentioned, it seems the easiest way to do this is to configure CloudFront/Lambda@Edge to proxy requests to a prerender service. I've found a repo that seems to take care of quite a bit of the aforementioned work for you: https://github.com/sanfrancesco/prerendercloud-lambda-edge
This uses Lambda@Edge to prerender your app via a
make deploy
command. Unfortunately, this uses prerender.cloud, NOT prerender.io. Hopefully this isn't a blocker.Have a look at the full solution over here, creating snapshots of your website with grunt and serving them to search engines with nothing more than amazon S3:
AngularJS SEO for static webpages (S3 CDN)
I managed to do this by not using Prerender at all but creating AWS Lambda function that:
Configure new origin (new lambda function) and behaviour (map /user/* requests to this new origin). Be sure to use "HTTPS only" Origin Protocol Policy for the origin, as API Gateway is only HTTPS, redirect here will cause the hostname to change.
(If you by accident used the redirect, then you will need to Invalidate "/*" as due to some CloudFront bug the configuration change will not help ; I spent multiple hours debugging this last night)