From 49763b96cf8ee1b2ddaf01c7b739fa131aa053a3 Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Sun, 29 Oct 2017 14:35:36 +0100 Subject: [PATCH] add pre-render.js --- package.json | 5 ++++- pre-render.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 pre-render.js diff --git a/package.json b/package.json index 4738eda..c7a8b5a 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,9 @@ "less-loader": "^2.2.2", "phantomjs-prebuilt": "^2.1.6", "prettier": "^1.7.4", + "puppeteer": "^0.12.0", "react-transform-hmr": "^1.0.1", + "serve-static": "^1.13.1", "style-loader": "^0.13.0", "url-loader": "^0.5.7", "webpack": "^1.8.4", @@ -64,7 +66,8 @@ }, "scripts": { "start": "node server.js", - "build": "BABEL_ENV=production ./node_modules/.bin/webpack --config webpack.config.production.js", + "build": + "BABEL_ENV=production ./node_modules/.bin/webpack --config webpack.config.production.js && node prerender.js", "lint": "./node_modules/.bin/eslint .", "test": "node_modules/karma/bin/karma start --single-run --browsers PhantomJS", "pretty": "node_modules/.bin/prettier --write --config .prettierrc \"app/scripts/**/*.js\"" diff --git a/pre-render.js b/pre-render.js new file mode 100644 index 0000000..a3e296b --- /dev/null +++ b/pre-render.js @@ -0,0 +1,27 @@ +const fs = require("fs"); +const connect = require("connect"), + serveStatic = require("serve-static"); +const puppeteer = require("puppeteer"); + +const port = 3000; +console.log("Listening on port", 3000); + +const app = connect(); + +app.use(serveStatic("./dist/Piano-Trainer")); +app.listen(port); + +async function run() { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto("http://localhost:" + port); + const html = await page.content(); + + fs.writeFile("./dist/Piano-Trainer/index.html", html, async () => { + console.log("Successfully wrote pre-rendered html into index.html"); + await browser.close(); + process.exit(0); + }); +} + +run();