1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-02 20:52:33 +02:00

Removing unneeded props (issue #12).

Moved code into separate modules.
This commit is contained in:
Chung Leong
2019-01-23 13:59:56 +01:00
parent a0f0c9191d
commit 36a9d0c619
11 changed files with 219 additions and 137 deletions

37
server/page-renderer.js Normal file
View File

@@ -0,0 +1,37 @@
const Bluebird = require('bluebird');
const FS = Bluebird.promisifyAll(require('fs'));
const ReactDOMServer = require('react-dom/server');
const CrossFetch = require('cross-fetch');
const FrontEnd = require('./client/front-end');
const NGINX_HOST = process.env.NGINX_HOST;
const CACHE_SIZE = 500;
const HTML_TEMPLATE = `${__dirname}/client/index.html`;
async function generate(path, target) {
// retrieve cached JSON through Nginx
let host = `http://${NGINX_HOST}`;
// create a fetch() that remembers the URLs used
let sourceURLs = [];
let fetch = (url, options) => {
if (url.startsWith(host)) {
sourceURLs.push(url.substr(host.length));
}
return CrossFetch(url, options);
};
let options = { host, path, target, fetch };
let rootNode = await FrontEnd.render(options);
let appHTML = ReactDOMServer.renderToString(rootNode);
let htmlTemplate = await FS.readFileAsync(HTML_TEMPLATE, 'utf-8');
let html = htmlTemplate.replace(`<!--REACT-->`, appHTML);
if (target === 'hydrate') {
// add <noscript> tag to redirect to SEO version
let meta = `<meta http-equiv=refresh content="0; url=?js=0">`;
html += `<noscript>${meta}</noscript>`;
}
return { path, target, sourceURLs, html };
}
module.exports = {
generate,
};