forked from wp-digital/aws-lambda-prerender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
48 lines (41 loc) · 1.05 KB
/
handler.js
File metadata and controls
48 lines (41 loc) · 1.05 KB
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
const chromium = require('chrome-aws-lambda')
const fetch = require('node-fetch')
const FormData = require('form-data')
const puppeteer = require('puppeteer-core')
module.exports.render = async ({
type,
id,
url,
return_url,
secret,
element
}, context, callback) => {
let response = null
try {
const browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
})
const page = await browser.newPage()
await page.goto(url, {waitUntil: 'networkidle0'})
const html = await page.evaluate(
(el) => document.querySelector(el).innerHTML,
element
)
await browser.close()
const body = new FormData()
body.append('type', type)
body.append('id', id)
body.append('html', html)
body.append('secret', secret)
response = fetch(return_url, {
method: 'POST',
body
})
} catch (error) {
return callback(error)
}
return callback(null, response)
};