BVB Source Codes

prerender Show s3HtmlCache.js Source code

Return Download prerender: download s3HtmlCache.js Source code - Download prerender Source code - Type:.js
  1. var cacheManager = require('cache-manager');
  2. var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}});
  3.  
  4. module.exports = {
  5.     init: function() {
  6.         this.cache = cacheManager.caching({
  7.             store: s3_cache
  8.         });
  9.     },
  10.  
  11.     beforePhantomRequest: function(req, res, next) {
  12.         if(req.method !== 'GET') {
  13.             return next();
  14.         }
  15.  
  16.         this.cache.get(req.prerender.url, function (err, result) {
  17.  
  18.             if (!err && result) {
  19.                 console.log('cache hit');
  20.                 return res.send(200, result.Body);
  21.             }
  22.            
  23.             next();
  24.         });
  25.     },
  26.  
  27.     afterPhantomRequest: function(req, res, next) {
  28.         if(req.prerender.statusCode !== 200) {
  29.             return next();
  30.         }
  31.  
  32.         this.cache.set(req.prerender.url, req.prerender.documentHTML, function(err, result) {
  33.             if (err) console.error(err);
  34.             next();
  35.         });
  36.        
  37.     }
  38. };
  39.  
  40.  
  41. var s3_cache = {
  42.     get: function(key, callback) {
  43.         if (process.env.S3_PREFIX_KEY) {
  44.             key = process.env.S3_PREFIX_KEY + '/' + key;
  45.         }
  46.  
  47.         s3.getObject({
  48.             Key: key
  49.         }, callback);
  50.     },
  51.     set: function(key, value, callback) {
  52.         if (process.env.S3_PREFIX_KEY) {
  53.             key = process.env.S3_PREFIX_KEY + '/' + key;
  54.         }
  55.  
  56.         var request = s3.putObject({
  57.             Key: key,
  58.             ContentType: 'text/html;charset=UTF-8',
  59.             StorageClass: 'REDUCED_REDUNDANCY',
  60.             Body: value
  61.         }, callback);
  62.  
  63.         if (!callback) {
  64.             request.send();
  65.         }
  66.     }
  67. };
  68.  
downloads3HtmlCache.js Source code - Download prerender Source code
Related Source Codes/Software:
libsodium - A modern and easy-to-use crypto library. 2017-01-09
Side-Menu.Android - Side menu with some categories to choose. ... 2017-01-09
docker-gitlab - Dockerized GitLab http://www.da... 2017-01-09
sinon - Test spies, stubs and mocks for JavaScript. ... 2017-01-09
in-view - Get notified when a DOM element enters or exits th... 2017-01-09
falcon - Falcon is a low-level, high-performance Python fra... 2017-01-09
ImageOptim - GUI image optimizer for Mac htt... 2017-01-09
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top