mon-entreprise/site/source/entry-server.tsx

41 lines
1.2 KiB
TypeScript
Raw Normal View History

import { SSRProvider } from '@react-aria/ssr'
2022-07-06 12:48:35 +00:00
import ReactDOMServer from 'react-dom/server'
import { FilledContext, HelmetProvider } from 'react-helmet-async'
2022-07-25 09:02:08 +00:00
import { StaticRouter } from 'react-router-dom/server'
2022-07-06 12:48:35 +00:00
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
import { AppEn } from './entry-en'
import { AppFr } from './entry-fr'
import i18next from './locales/i18n'
export function render(url: string, lang: 'fr' | 'en') {
const sheet = new ServerStyleSheet()
const helmetContext = {} as FilledContext
const App = lang === 'fr' ? AppFr : AppEn
2022-03-28 13:34:12 +00:00
i18next.changeLanguage(lang).catch((err) =>
// eslint-disable-next-line no-console
console.error(err)
)
const element = (
<HelmetProvider context={helmetContext}>
<SSRProvider>
<StyleSheetManager sheet={sheet.instance}>
<StaticRouter location={url}>
2022-07-25 09:02:08 +00:00
<App />
</StaticRouter>
</StyleSheetManager>
</SSRProvider>
</HelmetProvider>
)
2022-07-20 18:54:55 +00:00
// Render to initialize redux store (via useSimulationConfig)
ReactDOMServer.renderToString(element)
2022-07-20 18:54:55 +00:00
// Render with redux store configured
const html = ReactDOMServer.renderToString(element)
const styleTags = sheet.getStyleTags()
return { html, styleTags, helmet: helmetContext.helmet }
}