Created
November 9, 2017 18:44
-
-
Save MatthiasKunnen/36dc763638dd1622c345a8fcc50201c8 to your computer and use it in GitHub Desktop.
Getting a HTML snapshot via puppeteer
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
import * as cheerio from 'cheerio'; | |
import * as fs from 'fs'; | |
import * as mkdirp from 'mkdirp'; | |
import * as path from 'path'; | |
import * as puppeteer from 'puppeteer'; | |
import { routes } from './routes'; // Array of strings representing routes | |
const host = 'https://forcit.be/'; | |
function writeFile(filePath: string, contents: any, cb: (err) => void) { | |
mkdirp(path.dirname(filePath), err => { | |
if (err) { | |
return cb(err); | |
} | |
fs.writeFile(filePath, contents, cb); | |
}); | |
} | |
(async () => { | |
const browser = await puppeteer.launch(); | |
for (const route of routes) { | |
const fullRoute = host + route; | |
console.log(`Statisfying ${fullRoute}`); | |
let html: any; // Type coercion problem otherwise | |
let success = false; | |
let tryCounter = 0; | |
while (!success && tryCounter <= 3) { | |
try { | |
const page = await browser.newPage(); | |
await page.goto(fullRoute); | |
html = await page.evaluate(() => document.documentElement.outerHTML); | |
await page.close(); | |
success = true; | |
} catch (e) { | |
console.warn(`Could not evaluate ${fullRoute} in try ${tryCounter++}.`); | |
console.warn(`Error: ${e}`); | |
} | |
} | |
if (!success) { | |
console.error(`Could not evaluate ${fullRoute} in ${tryCounter} tries.`); | |
continue; | |
} | |
writeFile(`static/static/${route ? route : 'index'}.html`, html, err => { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
}); | |
} | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment