|
| 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"; |
| 13 | +import i18n, { registerCustomFormats } from "./i18n"; // your i18n configuration file |
| 14 | +import * as path from "node:path"; |
| 15 | + |
| 16 | +const ABORT_DELAY = 5_000; |
| 17 | + |
| 18 | +// Override console.erro to suppress specific warnings |
| 19 | +const originalConsoleError = console.error; |
| 20 | +console.error = (msg, ...args) => { |
| 21 | + if (typeof msg === "string" && msg.includes("useLayoutEffect")) { |
| 22 | + return; |
| 23 | + } |
| 24 | + if (typeof msg === "string" && msg.includes("A props object containing")) { |
| 25 | + return; |
| 26 | + } |
| 27 | + originalConsoleError(msg, ...args); |
| 28 | +}; |
| 29 | + |
| 30 | +export default function handleRequest( |
| 31 | + request: Request, |
| 32 | + responseStatusCode: number, |
| 33 | + responseHeaders: Headers, |
| 34 | + routerContext: EntryContext, |
| 35 | + loadContext: AppLoadContext |
| 36 | +) { |
| 37 | + return new Promise(async (resolve, reject) => { |
| 38 | + let shellRendered = false; |
| 39 | + let userAgent = request.headers.get("user-agent"); |
| 40 | + |
| 41 | + // Ensure requests from bots and SPA Mode renders wait for all content to load before responding |
| 42 | + // https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation |
| 43 | + let readyOption: keyof RenderToPipeableStreamOptions = |
| 44 | + (userAgent && isbot(userAgent)) || routerContext.isSpaMode ? "onAllReady" : "onShellReady"; |
| 45 | + |
| 46 | + let instance = createInstance(); |
| 47 | + let lng = await i18next.getLocale(request); |
| 48 | + // let ns = i18next.getRouteNamespaces(routerContext); |
| 49 | + let ns = ["translation"] as string[]; |
| 50 | + |
| 51 | + await instance |
| 52 | + .use(initReactI18next) // Tell our instance to use react-i18next |
| 53 | + .use(Backend) // Setup our backend |
| 54 | + .init({ |
| 55 | + ...i18n, // spread the configuration |
| 56 | + lng, // The locale we detected above |
| 57 | + ns, // The namespaces the routes about to render wants to use |
| 58 | + backend: { |
| 59 | + loadPath: path.resolve("./public/locales/{{lng}}.json"), |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + registerCustomFormats(instance); |
| 64 | + |
| 65 | + const { pipe, abort } = renderToPipeableStream( |
| 66 | + <I18nextProvider i18n={instance}> |
| 67 | + <ServerRouter context={routerContext} url={request.url} abortDelay={ABORT_DELAY} />, |
| 68 | + </I18nextProvider>, |
| 69 | + { |
| 70 | + [readyOption]() { |
| 71 | + shellRendered = true; |
| 72 | + const body = new PassThrough(); |
| 73 | + const stream = createReadableStreamFromReadable(body); |
| 74 | + |
| 75 | + responseHeaders.set("Content-Type", "text/html"); |
| 76 | + |
| 77 | + resolve( |
| 78 | + new Response(stream, { |
| 79 | + headers: responseHeaders, |
| 80 | + status: responseStatusCode, |
| 81 | + }) |
| 82 | + ); |
| 83 | + |
| 84 | + pipe(body); |
| 85 | + }, |
| 86 | + onShellError(error: unknown) { |
| 87 | + reject(error); |
| 88 | + }, |
| 89 | + onError(error: unknown) { |
| 90 | + responseStatusCode = 500; |
| 91 | + // Log streaming rendering errors from inside the shell. Don't log |
| 92 | + // errors encountered during initial shell rendering since they'll |
| 93 | + // reject and get logged in handleDocumentRequest. |
| 94 | + if (shellRendered) { |
| 95 | + console.error(error); |
| 96 | + } |
| 97 | + }, |
| 98 | + } |
| 99 | + ); |
| 100 | + |
| 101 | + setTimeout(abort, ABORT_DELAY); |
| 102 | + }); |
| 103 | +} |
0 commit comments