Skip to content

Commit

Permalink
prerender updated - headless chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
pballester committed Dec 7, 2017
1 parent 8b8ba11 commit 1eab27c
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 774 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Exclude all...
*
# ...but
!lib
!server.js
!package.json
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode
46 changes: 0 additions & 46 deletions .jshintrc

This file was deleted.

32 changes: 19 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
FROM node:latest
FROM node:carbon

MAINTAINER Pablo Ballester
RUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

RUN echo deb http://ftp.debian.org/debian/ jessie main contrib non-free > /etc/apt/source.list
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apt-get update -y && apt-get install -y \
python2.7 python-pip \
libfreetype6 libfontconfig
COPY package.json /usr/src/app/
RUN npm install

RUN mkdir /data
COPY . /usr/src/app

ADD ./package.json /data/package.json
RUN cd /data && npm install

ADD . /data/

CMD node /data/server.js
CMD [ "npm", "start" ]
6 changes: 3 additions & 3 deletions lib/redisCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var cleanUrl = function(url) {
};

module.exports = {
beforePhantomRequest: function(req, res, next) {
requestReceived: function(req, res, next) {
if (req.method !== 'GET' || redisOnline !== true) {
return next();
}
Expand All @@ -99,14 +99,14 @@ module.exports = {
});
},

afterPhantomRequest: function(req, res, next) {
pageLoaded: function(req, res, next) {
if (redisOnline !== true) {
return next();
}
// Don't cache anything that didn't result in a 200. This is to stop caching of 3xx/4xx/5xx status codes
if (req.prerender.statusCode === 200) {
var prerenderUrl = cleanUrl(req.prerender.url);
client.set(prerenderUrl, req.prerender.documentHTML, function(err, reply) {
client.set(prerenderUrl, req.prerender.content, function(err, reply) {
// If library set to cache set an expiry on the key.
if (!err && reply && ttl && ttl !== 0) {
client.expire(prerenderUrl, ttl, function(err, didSetExpiry) {
Expand Down
16 changes: 4 additions & 12 deletions lib/viewPort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@ var width = 1024; // 1440;
var height = 1024; // 718;

module.exports = {
onPhantomPageCreate: function (phantom, req, res, next) {
req.prerender.page.run(width, height, function (width, height, resolve) {
this.viewportSize = {
width: width,
height: height
};
resolve();
}).then(function () {
next();
}).catch(function (err) {
console.log('custom viewport size error', err);
});
requestReceived: function (req, res, next) {
req.prerender.width = width,
req.prerender.height = height
next();
}
};
Loading

0 comments on commit 1eab27c

Please sign in to comment.