Updating prerender script to use async/await
This commit is contained in:
+20
-9
@@ -1,20 +1,31 @@
|
||||
import React from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import fs from 'fs';
|
||||
import util from 'util';
|
||||
import cheerio from 'cheerio';
|
||||
|
||||
import './i18n';
|
||||
|
||||
const pages = fs.readdirSync('./src/pages');
|
||||
const readdir = util.promisify(fs.readdir);
|
||||
const readFile = util.promisify(fs.readFile);
|
||||
const writeFile = util.promisify(fs.writeFile);
|
||||
|
||||
pages.forEach(page => {
|
||||
import(`./pages/${ page }/Component`).then(component => {
|
||||
const Component = component.default;
|
||||
const pagePath = `./build/${ page }.html`;
|
||||
readdir('./src/pages')
|
||||
.then(pages => (
|
||||
Promise.all(pages.map(async page => {
|
||||
const Component = (await import(`./pages/${ page }/Component`)).default;
|
||||
const pagePath = `./build/${ page }.html`;
|
||||
|
||||
const markup = cheerio.load(fs.readFileSync(pagePath));
|
||||
const markup = cheerio.load(await readFile(pagePath));
|
||||
|
||||
markup('#root').html(renderToString(<Component/>));
|
||||
fs.writeFileSync(pagePath, markup.html());
|
||||
markup('#root').html(renderToString(<Component/>));
|
||||
await writeFile(pagePath, markup.html());
|
||||
|
||||
console.log(`${ page } prerendered`); // eslint-disable-line no-console
|
||||
}))
|
||||
))
|
||||
.then(() => console.log('Done prerendering')) // eslint-disable-line no-console
|
||||
.catch(e => {
|
||||
console.log(e.toString()); // eslint-disable-line no-console
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user