Single Page App + Amazon S3 + Amazon CloudFront + Prerender.io - how to set up?

后端 未结 5 1190
清歌不尽
清歌不尽 2021-02-07 09:15
  1. I have single page app built with Backbone.js.
  2. I host app (app consists of static files only) on Amazon S3.
  3. I use CloudFront as a Bucket CDN.
  4. Ap
5条回答
  •  无人共我
    2021-02-07 09:47

    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:

            'use strict';
            /* change the version number below whenever this code is modified */
            exports.handler = (event, context, callback) => {
                const request = event.Records[0].cf.request;
                const headers = request.headers;
                const user_agent = headers['user-agent'];
                const host = headers['host'];
                if (user_agent && host) {
                  if (/baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/.test(user_agent[0].value)) {
                    headers['x-prerender-token'] = [{ key: 'X-Prerender-Token', value: '${PrerenderToken}'}];
                    headers['x-prerender-host'] = [{ key: 'X-Prerender-Host', value: host[0].value}];
                  }
                }
                callback(null, request);
            };
    

    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:

          'use strict';
          /* change the version number below whenever this code is modified */
          exports.handler = (event, context, callback) => {
               const request = event.Records[0].cf.request;
               if (request.headers['x-prerender-token'] && request.headers['x-prerender-host']) {
                 request.origin = {
                     custom: {
                         domainName: 'service.prerender.io',
                         port: 443,
                         protocol: 'https',
                         readTimeout: 20,
                         keepaliveTimeout: 5,
                         customHeaders: {},
                         sslProtocols: ['TLSv1', 'TLSv1.1'],
                         path: '/https%3A%2F%2F' + request.headers['x-prerender-host'][0].value
                     }
                 };
              }
              callback(null, request);
          };
    

    There's a fully worked example at: https://github.com/jinty/prerender-cloudfront

提交回复
热议问题