electrode-io / electrode

Web applications with node.js and React
Other
2.1k stars 300 forks source link

[minor][feat] add custom renderer support for SSR. #1865

Closed ashuverma closed 2 years ago

ashuverma commented 2 years ago

This is to support use cases like css in js e.g. Rendering Styled Components on server

e.g. in server entry (server.js)


import {ServerStyleSheet} from "styled-components";
import ReactDOM from "react-dom/server";

export default {
renderer(element, options) {
   const sheet = new ServerStyleSheet();
   try {
      const html = ReactDOMServer.renderToString(.  // This can be renderToNodeStream or Static markup/stream depending on option. 
                              sheet.collectStyles(element)
       );
      const styleTags = sheet.getStyleTags(); 

      return styleTags + html;
    } catch (error) {
        console.log(error);
    }
}
ashuverma commented 2 years ago

@divyakarippath @jchip