forgeplus-react/server/render.js

29 lines
876 B
JavaScript

import React from "react";
import {renderToString} from "react-dom/server";
import {StaticRouter} from "react-router-dom";
import {renderRoutes} from "react-router-config";
import Routes from "../Routes";
import { Provider } from "react-redux";
import fs from 'fs'
import path from 'path';
export const render = (req,res)=>{
const context = {
css: []
};
const content = renderToString((
<Provider>
<StaticRouter location={req.path} context={context}>{renderRoutes(Routes)}</StaticRouter>
</Provider>
))
let html=fs.readFileSync(path.join(path.resolve('./build'),'index.html'),'utf-8');
const prepHTML=(data,rootString)=>{
data=data.replace('<div id="root" class="page -layout-v -fit widthunit"></div>',`<div id="root" class="page -layout-v -fit widthunit">${rootString}</div>`);
return data;
}
res.send(prepHTML(html, content))
}