|
| 1 | +import { PassThrough } from 'node:stream'; |
| 2 | + |
| 3 | +import type { AppLoadContext, EntryContext } from 'react-router'; |
| 4 | +import { createReadableStreamFromReadable } from '@react-router/node'; |
| 5 | +import { ServerRouter } from 'react-router'; |
| 6 | +import { isbot } from 'isbot'; |
| 7 | +import type { RenderToPipeableStreamOptions } from 'react-dom/server'; |
| 8 | +import { renderToPipeableStream } from 'react-dom/server'; |
| 9 | +import { createInstance } from 'i18next'; |
| 10 | +import i18next from './i18next.server'; |
| 11 | +import { I18nextProvider, initReactI18next } from 'react-i18next'; |
| 12 | +import Backend from 'i18next-fs-backend/cjs'; |
| 13 | +import i18n from './i18n'; // your i18n configuration file |
| 14 | +import { resolve } from 'node:path'; |
| 15 | + |
| 16 | +export const streamTimeout = 5_000; |
| 17 | + |
| 18 | +export default async function handleRequest( |
| 19 | + request: Request, |
| 20 | + responseStatusCode: number, |
| 21 | + responseHeaders: Headers, |
| 22 | + routerContext: EntryContext, |
| 23 | + loadContext: AppLoadContext, |
| 24 | + // If you have middleware enabled: |
| 25 | + // loadContext: unstable_RouterContextProvider |
| 26 | +) { |
| 27 | + // Ensure requests from bots and SPA Mode renders wait for all content to load before responding |
| 28 | + // https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation |
| 29 | + const userAgent = request.headers.get('user-agent'); |
| 30 | + const readyOption: keyof RenderToPipeableStreamOptions = |
| 31 | + (userAgent && isbot(userAgent)) || routerContext.isSpaMode |
| 32 | + ? 'onAllReady' |
| 33 | + : 'onShellReady'; |
| 34 | + |
| 35 | + const instance = createInstance(); |
| 36 | + const lng = await i18next.getLocale(request); |
| 37 | + const ns = i18next.getRouteNamespaces(routerContext); |
| 38 | + |
| 39 | + await instance |
| 40 | + .use(initReactI18next) // Tell our instance to use react-i18next |
| 41 | + .use(Backend) // Setup our backend |
| 42 | + .init({ |
| 43 | + ...i18n, // spread the configuration |
| 44 | + lng, // The locale we detected above |
| 45 | + ns, // The namespaces the routes about to render wants to use |
| 46 | + backend: { loadPath: resolve('./locales/{{lng}}/{{ns}}.json') }, |
| 47 | + }); |
| 48 | + |
| 49 | + return new Promise((resolve, reject) => { |
| 50 | + let shellRendered = false; |
| 51 | + |
| 52 | + const { pipe, abort } = renderToPipeableStream( |
| 53 | + <I18nextProvider i18n={instance}> |
| 54 | + <ServerRouter context={routerContext} url={request.url} /> |
| 55 | + </I18nextProvider>, |
| 56 | + { |
| 57 | + [readyOption]() { |
| 58 | + shellRendered = true; |
| 59 | + const body = new PassThrough(); |
| 60 | + const stream = createReadableStreamFromReadable(body); |
| 61 | + |
| 62 | + responseHeaders.set('Content-Type', 'text/html'); |
| 63 | + |
| 64 | + resolve( |
| 65 | + new Response(stream, { |
| 66 | + headers: responseHeaders, |
| 67 | + status: responseStatusCode, |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + pipe(body); |
| 72 | + }, |
| 73 | + onShellError(error: unknown) { |
| 74 | + reject(error); |
| 75 | + }, |
| 76 | + onError(error: unknown) { |
| 77 | + responseStatusCode = 500; |
| 78 | + // Log streaming rendering errors from inside the shell. Don't log |
| 79 | + // errors encountered during initial shell rendering since they'll |
| 80 | + // reject and get logged in handleDocumentRequest. |
| 81 | + if (shellRendered) { |
| 82 | + // eslint-disable-next-line no-console |
| 83 | + console.error(error); |
| 84 | + } |
| 85 | + }, |
| 86 | + }, |
| 87 | + ); |
| 88 | + |
| 89 | + // Abort the rendering stream after the `streamTimeout` so it has time to |
| 90 | + // flush down the rejected boundaries |
| 91 | + setTimeout(abort, streamTimeout + 1000); |
| 92 | + }); |
| 93 | +} |
0 commit comments