/
renderer.ts
170 lines (143 loc) · 5.34 KB
/
renderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as puppeteer from 'puppeteer';
import * as url from 'url';
type SerializedResponse = {
status: number; content: string;
}
type ViewportDimensions = {
width: number; height: number;
};
const MOBILE_USERAGENT =
'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Mobile Safari/537.36';
/**
* Wraps Puppeteer's interface to Headless Chrome to expose high level rendering
* APIs that are able to handle web components and PWAs.
*/
export class Renderer {
private browser: puppeteer.Browser;
constructor(browser: puppeteer.Browser) {
this.browser = browser;
}
async serialize(requestUrl: string, isMobile: boolean):
Promise<SerializedResponse> {
/**
* Executed on the page after the page has loaded. Strips script and
* import tags to prevent further loading of resources.
*/
function stripPage() {
const elements = document.querySelectorAll('script, link[rel=import]');
for (const e of Array.from(elements)) {
e.remove();
}
}
/**
* Injects a <base> tag which allows other resources to load. This
* has no effect on serialised output, but allows it to verify render
* quality.
*/
function injectBaseHref(origin: string) {
const base = document.createElement('base');
base.setAttribute('href', origin);
const bases = document.head.querySelectorAll('base');
if (bases.length) {
// Patch existing <base> if it is relative.
const existingBase = bases[0].getAttribute('href') || '';
if (existingBase.startsWith('/')) {
bases[0].setAttribute('href', origin + existingBase);
}
} else {
// Only inject <base> if it doesn't already exist.
document.head.insertAdjacentElement('afterbegin', base);
}
}
const page = await this.browser.newPage();
await page.setCacheEnabled(false);
page.setViewport({width: 1000, height: 1000, isMobile});
if (isMobile) {
page.setUserAgent(MOBILE_USERAGENT);
}
page.evaluateOnNewDocument('customElements.forcePolyfill = true');
page.evaluateOnNewDocument('ShadyDOM = {force: true}');
page.evaluateOnNewDocument('ShadyCSS = {shimcssproperties: true}');
let response: puppeteer.Response|null = null;
// Capture main frame response. This is used in the case that rendering
// times out, which results in puppeteer throwing an error. This allows us
// to return a partial response for what was able to be rendered in that
// time frame.
page.addListener('response', (r: puppeteer.Response) => {
if (!response) {
response = r;
}
});
try {
// Navigate to page. Wait until there are no oustanding network requests.
response = await page.goto(
requestUrl);
} catch (e) {
console.error(e);
}
while (true) {
const rendered = await page.evaluate(() => {return (window as any).renderComplete;});
if (rendered) break;
await sleep(500);
}
if (!response) {
console.error('response does not exist');
// This should only occur when the page is about:blank. See
// https://github.com/GoogleChrome/puppeteer/blob/v1.5.0/docs/api.md#pagegotourl-options.
return {status: 400, content: ''};
}
// Set status to the initial server's response code. Check for a <meta
// name="render:status_code" content="4xx" /> tag which overrides the status
// code.
let statusCode = response.status();
const newStatusCode =
await page
.$eval(
'meta[name="render:status_code"]',
(element) => parseInt(element.getAttribute('content') || ''))
.catch(() => undefined);
// On a repeat visit to the same origin, browser cache is enabled, so we may
// encounter a 304 Not Modified. Instead we'll treat this as a 200 OK.
if (statusCode === 304) {
statusCode = 200;
}
// Original status codes which aren't 200 always return with that status
// code, regardless of meta tags.
if (statusCode === 200 && newStatusCode) {
statusCode = newStatusCode;
}
// Remove script & import tags.
await page.evaluate(stripPage);
// Inject <base> tag with the origin of the request (ie. no path).
const parsedUrl = url.parse(requestUrl);
await page.evaluate(
injectBaseHref, `${parsedUrl.protocol}//${parsedUrl.host}`);
// Serialize page.
const result = await page.evaluate('document.firstElementChild.outerHTML');
await page.close();
return {status: statusCode, content: result};
}
async screenshot(
url: string,
isMobile: boolean,
dimensions: ViewportDimensions,
options?: object): Promise<Buffer> {
const page = await this.browser.newPage();
page.setViewport(
{width: dimensions.width, height: dimensions.height, isMobile});
if (isMobile) {
page.setUserAgent(MOBILE_USERAGENT);
}
await page.goto(url, {timeout: 10000, waitUntil: 'networkidle0'});
// Must be jpeg & binary format.
const screenshotOptions =
Object.assign({}, options, {type: 'jpeg', encoding: 'binary'});
const buffer = await page.screenshot(screenshotOptions);
return buffer;
}
}
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}