Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
serverless screenshot
exports.run = async (browser, url, key) => {
const page = await browser.newPage();
await page.goto(url,
{waitUntil: ['domcontentloaded', 'networkidle0']}
);
await page.setViewport({width: 1464, height: 764});
await page.screenshot({
path: '/tmp/screenshot.png',
clip: {
x: 64,
y: 64,
width: 1400,
height: 700,
},
});
const aws = require('aws-sdk');
const s3 = new aws.S3({apiVersion: '2006-03-01'});
const fs = require('fs');
const screenshot = await new Promise((resolve, reject) => {
fs.readFile('/tmp/screenshot.png', (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
await s3.putObject({
Bucket: 'assets.bounties.network',
Key: key,
Body: screenshot,
ACL: 'public-read',
}).promise();
await page.close();
await axios.post('https://api.prerender.io/recache', {
prerenderToken: process.env.PRERENDER_TOKEN,
url: url,
});
const lastLetterOfUrl = url.slice(-1);
let secondaryUrl;
if (lastLetterOfUrl === '/') {
secondaryUrl = url.slice(0, -1);
} else {
secondaryUrl = url + '/';
}
await axios.post('https://api.prerender.io/recache', {
prerenderToken: process.env.PRERENDER_TOKEN,
url: secondaryUrl,
});
return 'done';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.