Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
React server side rendering - can't pass data from server handler file to react component

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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda