2024-09-10 20:33:22 +02:00
|
|
|
import { PassThrough } from "stream";
|
|
|
|
import { createReadableStreamFromReadable, type EntryContext } from "@remix-run/node";
|
2024-09-05 22:29:12 +02:00
|
|
|
import { RemixServer } from "@remix-run/react";
|
|
|
|
import { isbot } from "isbot";
|
|
|
|
import { renderToPipeableStream } from "react-dom/server";
|
2024-09-10 20:33:22 +02:00
|
|
|
import { createInstance } from "i18next";
|
|
|
|
import i18next from "./i18next.server";
|
|
|
|
import { I18nextProvider, initReactI18next } from "react-i18next";
|
|
|
|
import Backend from "i18next-fs-backend";
|
|
|
|
import i18n from "./i18n"; // your i18n configuration file
|
|
|
|
import { resolve } from "node:path";
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-10 20:33:22 +02:00
|
|
|
const ABORT_DELAY = 5000;
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-10 20:33:22 +02:00
|
|
|
export default async function handleRequest(
|
2024-09-05 22:29:12 +02:00
|
|
|
request: Request,
|
|
|
|
responseStatusCode: number,
|
|
|
|
responseHeaders: Headers,
|
|
|
|
remixContext: EntryContext,
|
|
|
|
) {
|
2024-09-10 20:33:22 +02:00
|
|
|
const callbackName = isbot(request.headers.get("user-agent")) ? "onAllReady" : "onShellReady";
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-10 20:33:22 +02:00
|
|
|
const instance = createInstance();
|
|
|
|
const lng = await i18next.getLocale(request);
|
|
|
|
const ns = i18next.getRouteNamespaces(remixContext);
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-10 20:33:22 +02:00
|
|
|
await instance
|
|
|
|
.use(initReactI18next) // Tell our instance to use react-i18next
|
|
|
|
.use(Backend) // Set up our backend
|
|
|
|
.init({
|
|
|
|
...i18n, // spread the configuration
|
|
|
|
lng, // The locale we detected above
|
|
|
|
ns, // The namespaces the routes about to render wants to use
|
|
|
|
backend: { loadPath: resolve("./public/locales/{{lng}}.json") },
|
|
|
|
});
|
2024-09-05 22:29:12 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-09-10 20:33:22 +02:00
|
|
|
let didError = false;
|
|
|
|
|
2024-09-05 22:29:12 +02:00
|
|
|
const { pipe, abort } = renderToPipeableStream(
|
2024-09-10 20:33:22 +02:00
|
|
|
<I18nextProvider i18n={instance}>
|
|
|
|
<RemixServer context={remixContext} url={request.url} />
|
|
|
|
</I18nextProvider>,
|
2024-09-05 22:29:12 +02:00
|
|
|
{
|
2024-09-10 20:33:22 +02:00
|
|
|
[callbackName]: () => {
|
2024-09-05 22:29:12 +02:00
|
|
|
const body = new PassThrough();
|
|
|
|
const stream = createReadableStreamFromReadable(body);
|
|
|
|
responseHeaders.set("Content-Type", "text/html");
|
|
|
|
|
|
|
|
resolve(
|
|
|
|
new Response(stream, {
|
|
|
|
headers: responseHeaders,
|
2024-09-10 20:33:22 +02:00
|
|
|
status: didError ? 500 : responseStatusCode,
|
2024-09-05 22:29:12 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
pipe(body);
|
|
|
|
},
|
|
|
|
onShellError(error: unknown) {
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
onError(error: unknown) {
|
2024-09-10 20:33:22 +02:00
|
|
|
didError = true;
|
|
|
|
|
|
|
|
console.error(error);
|
2024-09-05 22:29:12 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
setTimeout(abort, ABORT_DELAY);
|
|
|
|
});
|
|
|
|
}
|