Skip to content

Commit

Permalink
add pre-render.js
Browse files Browse the repository at this point in the history
  • Loading branch information
philippotto committed Oct 29, 2017
1 parent fe568ca commit 49763b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@
"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",
"webpack-dev-server": "^1.14.1"
},
"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\""
Expand Down
27 changes: 27 additions & 0 deletions pre-render.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 49763b9

Please sign in to comment.