the below code snippet is for server handler function. build/index.html is from build folder generated by "react-scripts build" command
let finalData;
const app = ReactDOMServer.renderToString(
<StaticRouter location = {event.path} context = {{}}>
<SSRApp />
</StaticRouter>);
let html = await fs.promises.readFile('build/index.html', 'utf8');
if(!html)
{
finalData = {
statusCode: 500,
headers: { "Content-Type": "text/html" },
body: "Oops, better luck next time!",
};
}
else{
const ssrData= {"message":"From Server"}
html.replace('</head>', `<script>var ssrData= ${JSON.stringify(ssrData)}</script>`).replace('<div id="root"></div>', `<div id="root">${app}</div>`);
finalData= {
statusCode: 200,
headers: { "Content-Type": "text/html" },
body: html,
};
}
Here i am trying to pass ssrData inside html string. In local node server ,it can able to pass and retrieve in client side using window object i.e window.ssrData. But in lambda , logs shows replaced string of div root element only not for ssrData.
Any suggestion on this?