/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/material-ui-express-ssr/server.js
70 строк
2 KB
Olivier Tassinari
[code-infra] Create examples eslint rule (#42170)
21 май 2024, 23:35
Не верифицирован
21 май 2024, 23:35
8707eb1
Код
Авторство
О чём код?
import express from 'express'; import * as React from 'react'; import * as ReactDOMServer from 'react-dom/server'; import CssBaseline from '@mui/material/CssBaseline'; import { ThemeProvider } from '@mui/material/styles'; import { CacheProvider } from '@emotion/react'; import createEmotionServer from '@emotion/server/create-instance'; import createEmotionCache from './createEmotionCache'; import App from './App'; import theme from './theme'; function renderFullPage(html, css) { return ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My page</title> <meta name="viewport" content="initial-scale=1, width=device-width" /> <link rel="preconnect" href="https://fonts.googleapis.com" /> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" /> <meta name="emotion-insertion-point" content="" /> ${css} </head> <body> <script async src="build/bundle.js"></script> <div id="root">${html}</div> </body> </html> `; } function handleRender(req, res) { const cache = createEmotionCache(); const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache); // Render the component to a string. const html = ReactDOMServer.renderToString( <CacheProvider value={cache}> <ThemeProvider theme={theme}> {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */} <CssBaseline /> <App /> </ThemeProvider> </CacheProvider>, ); // Grab the CSS from emotion const emotionChunks = extractCriticalToChunks(html); const emotionCss = constructStyleTagsFromChunks(emotionChunks); // Send the rendered page back to the client. res.send(renderFullPage(html, emotionCss)); } const app = express(); app.use('/build', express.static('build')); // This is fired every time the server-side receives a request. app.use(handleRender); const port = 3000; app.listen(port, () => { console.log(`Listening on ${port}`); });