From 4a06c32ba47df790c8a3ab32b7bdf27d9882edb8 Mon Sep 17 00:00:00 2001 From: briandennis Date: Mon, 12 Mar 2018 12:28:20 -0700 Subject: [PATCH] wait for page to detect language in /image handler --- handlers/image.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/handlers/image.js b/handlers/image.js index 14d914e..90c903c 100644 --- a/handlers/image.js +++ b/handlers/image.js @@ -1,4 +1,5 @@ -const port = parseInt(process.env.PORT, 10) || 3000 +const PORT = parseInt(process.env.PORT, 10) || 3000 +const ARBITRARY_WAIT_TIME = 500 module.exports = browser => async (req, res) => { let page = await browser.newPage() @@ -7,9 +8,12 @@ module.exports = browser => async (req, res) => { if (!state) res.status(400).send() try { - await page.goto(`http://localhost:${port}?state=${state}`) + await page.goto(`http://localhost:${PORT}?state=${state}`) await page.addScriptTag({ path: `./lib/customDomToImage.js` }) + // wait for page to detect language + await delay(ARBITRARY_WAIT_TIME) + const targetElement = await page.$('#export-container') let dataUrl = await page.evaluate(target => { @@ -34,3 +38,8 @@ module.exports = browser => async (req, res) => { await page.close() } } + +// private +function delay(ms) { + return new Promise(r => setTimeout(r, ms)) +}