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

35 lines
1.0 KiB
TypeScript
Raw Normal View History

import ReactDOMServer from 'react-dom/server'
import { SSRProvider } from '@react-aria/ssr'
import { StaticRouter } from 'react-router-dom'
import i18next from './locales/i18n'
2022-04-03 15:40:00 +00:00
import { AppFr } from './entry-fr'
import { AppEn } from './entry-en'
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
import { FilledContext, HelmetProvider } from 'react-helmet-async'
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 html = ReactDOMServer.renderToString(
<HelmetProvider context={helmetContext}>
<SSRProvider>
<StyleSheetManager sheet={sheet.instance}>
<StaticRouter location={url}>
<App />
</StaticRouter>
</StyleSheetManager>
</SSRProvider>
</HelmetProvider>
)
const styleTags = sheet.getStyleTags()
return { html, styleTags, helmet: helmetContext.helmet }
}