| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Module Federation Server Side Rendering example using React Suspense.
This example demonstrates a basic shell application loading remote components and rendering them server side. With different types of components styling
It combines three different approaches of styling
exposes: {
'./Content': './src/Content',
'./LoaderContext': './src/LoaderContext'
},
exposes: {
'./Content': './src/Content',
},
shared: {
...YOUR_SHARED_DEPS,
"react-jss": {
singleton: true,
},
},
exposes: {
'./Content': './src/Content',
'./LoaderContext': './src/LoaderContext'
},
shared: {
...YOUR_SHARED_DEPS,
"styled-components": {
singleton: true,
},
},
Configs:
Rendering: Basically there is two main parts of this
The main SSR related staff of each shell is a /shell/server/render.js; Overall understanding: before you send the response with your SSR html you need to collect all CSS that is used inside your application. For isomorphic-style-loader and react-jss this could be done through corresponding ContextProviders during the DOM Rendering function call and for styled-components after DOM Rendering;
The main Application Hydration related things could be found in corresponding library official doc:
if you are going to consume components styled with isomorphic-style-loader you will need to import each StyleContext and compose them for your App. There is basic example of React Context Provider composition ComposeProviders.js (of course you can use your own) For this approach the basic scenario in render.js will look something like this
import React from 'react';
import { renderToString } from 'react-dom/server';
import App from '../src/components/App';
// Your Compose Component
import Compose from '../src/ComposeProviders';
// Your Array of `isomorphic-style-loader` StyleContext providers
import providers from '../src/StyleProviders';
export default async function(req, res) {
const css = new Set();
// required function that will collect css
const insertCss = (...styles) => {
styles.forEach(style => css.add(style._getCss()));
};
const combinedProviders = providers.map(p => [p, { value: { insertCss } }]);
const component = renderToString(
<Compose providers={combinedProviders}>
<App />
</Compose>
);
const html = `<!doctype html>
<html>
<head>
<!--Here we add our collected styles-->
<style>${[...css].join('')}</style>
</head>
<body>
<div id="root">${component}</div>
<script async data-chunk="main" src="http://localhost:4001/static/main.js"></script>
</body>
</html>`
res.status(200).send(html);
};
if you are going to consume components styled with react-jss you will need to use JssProvider and compose it for your App. There is basic example of React Context Provider composition ComposeProviders.js (of course you can use your own) For this approach the basic scenario in render.js will look something like this
import React from 'react';
import { renderToString } from 'react-dom/server';
import App from '../src/components/App';
// Your Compose Component
import Compose from '../src/ComposeProviders';
// Jss related
import {JssProvider, SheetsRegistry} from 'react-jss';
export default async function(req, res) {
const sheets = new SheetsRegistry();
const combinedProviders = [[JssProvider, { registry: sheets }]];
const component = renderToString(
<Compose providers={combinedProviders}>
<App />
</Compose>
);
const html = `<!doctype html>
<html>
<head>
<!--Here we add our collected styles-->
<style>${sheets.toString()}</style>
</head>
<body>
<div id="root">${component}</div>
<script async data-chunk="main" src="http://localhost:4001/static/main.js"></script>
</body>
</html>`
res.status(200).send(html);
};
if you are going to consume components styled with styled-components you will need to use ServerStyleSheet and use it for App Rendering. There is basic example of React Context Provider composition ComposeProviders.js (of course you can use your own) For this approach the basic scenario in render.js will look something like this
import React from 'react';
import { renderToString } from 'react-dom/server';
import App from '../src/components/App';
// Your Compose Component
import Compose from '../src/ComposeProviders';
// Styled-components related
import {ServerStyleSheet} from "styled-components";
export default async function(req, res) {
const sheet = new ServerStyleSheet();
const combinedProviders = [];
const component = renderToString(sheet.collectStyles(
<Compose providers={combinedProviders}>
<App />
</Compose>
));
const styleTags = sheet.getStyleTags();
const html = `<!doctype html>
<html>
<head>
<!--Here we add our collected styles-->
${styleTags}
</head>
<body>
<div id="root">${component}</div>
<script async data-chunk="main" src="http://localhost:4001/static/main.js"></script>
</body>
</html>`
res.status(200).send(html);
};
| Exposed Styling | React |
|---|---|
| Css | ✅ |
| Scss | ✅ |
| Less | ✅ |
| Css Module | ✅ |
| react-jss | ✅ |
| styled-components | ✅ |
| tailwind css (as module) | ✅ |
Run yarn to install the dependencies.
Run yarn build to build the packages.
Run yarn serve in the shell and related expose remotes folders to start the servers.
This will build the packages and serve them.
Expose Remotes
Shells
| Back | FazBrowse Home | New Git URL |