Created
April 1, 2020 11:43
-
-
Save mvaneijgen/edbf10aa21a88af797c02dbe7d799c96 to your computer and use it in GitHub Desktop.
Function to create OG images on a lamda function also should work on Netlify taken from Wes Bos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chrome = require('chrome-aws-lambda'); | |
const puppeteer = require('puppeteer-core'); | |
const wait = require('waait'); | |
const cached = new Map(); | |
const exePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; | |
async function getOptions(isDev) { | |
if (isDev) { | |
return { | |
product: 'chrome', | |
args: [], | |
executablePath: exePath, | |
headless: true, | |
}; | |
} | |
return { | |
product: 'chrome', | |
args: chrome.args, | |
executablePath: await chrome.executablePath, | |
headless: chrome.headless, | |
}; | |
} | |
async function getScreenshot(url, isDev) { | |
// first check if this value has been cached | |
const cachedImage = cached.get(url); | |
if (cachedImage) { | |
console.log('Cached image! Return this'); | |
return cachedImage; | |
} | |
const options = await getOptions(isDev); | |
const browser = await puppeteer.launch(options); | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 1200, height: 630, deviceScaleFactor: 1.5 }); | |
await page.goto(url); | |
await wait(1000); | |
const buffer = await page.screenshot({ type: 'png' }); | |
const base64Image = buffer.toString('base64'); | |
cached.set(url, base64Image); | |
return base64Image; | |
} | |
// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method | |
exports.handler = async (event, context) => { | |
const qs = new URLSearchParams(event.queryStringParameters); | |
const photoBuffer = await getScreenshot( | |
`${process.env.URL || `http://localhost:8888`}/thumbnail?${qs.toString()}`, | |
!process.env.NETLIFY | |
); | |
return { | |
statusCode: 200, | |
body: photoBuffer, | |
isBase64Encoded: true, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment