Headless Chrome: an answer to server-side rendering JS sites
The techniques in this article show how to use Puppeteer’s APIs to add server-side rendering (SSR) capabilities to an Express web server. The best part is the app itself requires almost no code changes. Headless does all the heavy lifting. In a couple of lines of code you can SSR any page and get its final markup.
A taste of what’s to come:
import puppeteer from 'puppeteer'; async function ssr(url) { const browser = await puppeteer.launch({headless: true}); const page = await browser.newPage(); await page.goto(url, {waitUntil: 'networkidle0'}); const html = await page.content(); // serialized HTML of page DOM. await browser.close(); return html; }
Interesting, but I wonder how it’ll work if the receiving browser doesn’t quite match up with what Headless Chrome generates.