Skip to content

Commit 8b8ba11

Browse files
committed
Added viewPort plugin
1 parent aa31e78 commit 8b8ba11

File tree

4 files changed

+722
-5
lines changed

4 files changed

+722
-5
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Plugins enabled by default:
1212
- removeScriptTags (*prerender*)
1313
- httpHeaders (*prerender*)
1414
- redisCache (*custom - lib/redisCache*)
15+
- viewPort (*custom - lib/viewPort*)
1516

1617
See [Prerender](https://github.com/prerender/prerender) for more documentation.
1718

@@ -24,3 +25,10 @@ See [Prerender](https://github.com/prerender/prerender) for more documentation.
2425
- **PARAMS_TO_IGNORE** - Defaults to *[]*, query parameters separated by
2526
whitespace to be ignored, when ignoring a query parameter, we will cache and
2627
serve a specified URL without using the ignored query parameter.
28+
29+
### ViewPort
30+
31+
Configuration by environment variables:
32+
33+
- **VIEWPORT_WIDTH** - Defaults to 1024.
34+
- **VIEWPORT_HEIGHT** - Defaults to 1024.

lib/viewPort.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var width = 1024; // 1440;
4+
var height = 1024; // 718;
5+
6+
module.exports = {
7+
onPhantomPageCreate: function (phantom, req, res, next) {
8+
req.prerender.page.run(width, height, function (width, height, resolve) {
9+
this.viewportSize = {
10+
width: width,
11+
height: height
12+
};
13+
resolve();
14+
}).then(function () {
15+
next();
16+
}).catch(function (err) {
17+
console.log('custom viewport size error', err);
18+
});
19+
}
20+
};

server.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
var prerender = require('prerender');
55
var redisCache = require('./lib/redisCache');
6+
var viewPort = require('./lib/viewPort');
67

78
var server = prerender({
8-
workers: process.env.PRERENDER_NUM_WORKERS,
9-
iterations: process.env.PRERENDER_NUM_ITERATIONS,
10-
resourceDownloadTimeout: process.env.RESOURCE_DOWNLOAD_TIMEOUT || 40000,
11-
jsTimeout: process.env.JS_TIMEOUT || 40000
9+
workers: process.env.PRERENDER_NUM_WORKERS,
10+
iterations: process.env.PRERENDER_NUM_ITERATIONS,
11+
resourceDownloadTimeout: process.env.RESOURCE_DOWNLOAD_TIMEOUT || 40000,
12+
jsTimeout: process.env.JS_TIMEOUT || 40000
1213
});
1314

1415
server.use(prerender.sendPrerenderHeader());
@@ -21,5 +22,5 @@ server.use(prerender.httpHeaders());
2122
// server.use(prerender.inMemoryHtmlCache());
2223
// server.use(prerender.s3HtmlCache());
2324
server.use(redisCache);
24-
25+
server.use(viewPort);
2526
server.start();

0 commit comments

Comments
 (0)