|
1 | | -import { PassThrough } from "stream"; |
2 | | -import type { EntryContext } from "@remix-run/node"; |
3 | | -import { Response } from "@remix-run/web-fetch"; |
4 | | -import { RemixServer } from "@remix-run/react"; |
5 | | -import { isbot } from "isbot"; |
6 | | -import { renderToPipeableStream } from "react-dom/server"; |
7 | | -import { createInstance } from "i18next"; |
8 | | -import i18next from "./localization/i18n.server"; |
9 | | -import { I18nextProvider, initReactI18next } from "react-i18next"; |
10 | | -import Backend from "i18next-fs-backend"; |
11 | | -import i18n from "./localization/i18n"; // your i18n configuration file |
12 | | -import { resolve } from "node:path"; |
13 | | -import { resources, returnLanguageIfSupported } from "./localization/resource"; |
| 1 | +import { resolve } from "node:path" |
| 2 | +import { PassThrough } from "node:stream" |
| 3 | +import type { EntryContext } from "@remix-run/node" |
| 4 | +import { RemixServer } from "@remix-run/react" |
| 5 | +import { Response } from "@remix-run/web-fetch" |
| 6 | +import { createInstance } from "i18next" |
| 7 | +import Backend from "i18next-fs-backend" |
| 8 | +import { isbot } from "isbot" |
| 9 | +import { renderToPipeableStream } from "react-dom/server" |
| 10 | +import { I18nextProvider, initReactI18next } from "react-i18next" |
| 11 | +import i18n from "./localization/i18n" // your i18n configuration file |
| 12 | +import i18next from "./localization/i18n.server" |
| 13 | +import { resources, returnLanguageIfSupported } from "./localization/resource" |
14 | 14 |
|
15 | | -const ABORT_DELAY = 5000; |
| 15 | +const ABORT_DELAY = 5000 |
16 | 16 |
|
17 | 17 | export default async function handleRequest( |
18 | | - request: Request, |
19 | | - responseStatusCode: number, |
20 | | - responseHeaders: Headers, |
21 | | - remixContext: EntryContext |
| 18 | + request: Request, |
| 19 | + responseStatusCode: number, |
| 20 | + responseHeaders: Headers, |
| 21 | + remixContext: EntryContext |
22 | 22 | ) { |
23 | | - const url = new URL(request.url); |
24 | | - const { pathname } = url; |
| 23 | + const url = new URL(request.url) |
| 24 | + const { pathname } = url |
25 | 25 |
|
26 | | - const lang = pathname.split("/")[1]; |
27 | | - const callbackName = isbot(request.headers.get("user-agent")) |
28 | | - ? "onAllReady" |
29 | | - : "onShellReady"; |
| 26 | + const lang = pathname.split("/")[1] |
| 27 | + const callbackName = isbot(request.headers.get("user-agent")) ? "onAllReady" : "onShellReady" |
30 | 28 |
|
31 | | - const instance = createInstance(); |
32 | | - const lng = |
33 | | - returnLanguageIfSupported(lang) ?? (await i18next.getLocale(request)); |
34 | | - const ns = i18next.getRouteNamespaces(remixContext); |
| 29 | + const instance = createInstance() |
| 30 | + const lng = returnLanguageIfSupported(lang) ?? (await i18next.getLocale(request)) |
| 31 | + const ns = i18next.getRouteNamespaces(remixContext) |
35 | 32 |
|
36 | | - await instance |
37 | | - .use(initReactI18next) // Tell our instance to use react-i18next |
38 | | - .use(Backend) // Setup our backend |
39 | | - .init({ |
40 | | - ...i18n, // spread the configuration |
41 | | - lng, // The locale we detected above |
42 | | - ns, // The namespaces the routes about to render wants to use |
43 | | - backend: { loadPath: resolve("./public/locales/{{lng}}/{{ns}}.json") }, |
44 | | - resources, |
45 | | - }); |
| 33 | + await instance |
| 34 | + .use(initReactI18next) // Tell our instance to use react-i18next |
| 35 | + .use(Backend) // Setup our backend |
| 36 | + .init({ |
| 37 | + ...i18n, // spread the configuration |
| 38 | + lng, // The locale we detected above |
| 39 | + ns, // The namespaces the routes about to render wants to use |
| 40 | + backend: { loadPath: resolve("./public/locales/{{lng}}/{{ns}}.json") }, |
| 41 | + resources, |
| 42 | + }) |
46 | 43 |
|
47 | | - return new Promise((resolve, reject) => { |
48 | | - let didError = false; |
| 44 | + return new Promise((resolve, reject) => { |
| 45 | + let didError = false |
49 | 46 |
|
50 | | - const { pipe, abort } = renderToPipeableStream( |
51 | | - <I18nextProvider i18n={instance}> |
52 | | - <RemixServer context={remixContext} url={request.url} /> |
53 | | - </I18nextProvider>, |
54 | | - { |
55 | | - [callbackName]: () => { |
56 | | - const body = new PassThrough(); |
| 47 | + const { pipe, abort } = renderToPipeableStream( |
| 48 | + <I18nextProvider i18n={instance}> |
| 49 | + <RemixServer context={remixContext} url={request.url} /> |
| 50 | + </I18nextProvider>, |
| 51 | + { |
| 52 | + [callbackName]: () => { |
| 53 | + const body = new PassThrough() |
57 | 54 |
|
58 | | - responseHeaders.set("Content-Type", "text/html"); |
| 55 | + responseHeaders.set("Content-Type", "text/html") |
59 | 56 |
|
60 | | - resolve( |
61 | | - new Response(body, { |
62 | | - headers: responseHeaders, |
63 | | - status: didError ? 500 : responseStatusCode, |
64 | | - }) |
65 | | - ); |
| 57 | + resolve( |
| 58 | + new Response(body, { |
| 59 | + headers: responseHeaders, |
| 60 | + status: didError ? 500 : responseStatusCode, |
| 61 | + }) |
| 62 | + ) |
66 | 63 |
|
67 | | - pipe(body); |
68 | | - }, |
69 | | - onShellError(error: unknown) { |
70 | | - reject(error); |
71 | | - }, |
72 | | - onError(error: unknown) { |
73 | | - didError = true; |
| 64 | + pipe(body) |
| 65 | + }, |
| 66 | + onShellError(error: unknown) { |
| 67 | + reject(error) |
| 68 | + }, |
| 69 | + onError(error: unknown) { |
| 70 | + didError = true |
74 | 71 |
|
75 | | - console.error(error); |
76 | | - }, |
77 | | - } |
78 | | - ); |
| 72 | + console.error(error) |
| 73 | + }, |
| 74 | + } |
| 75 | + ) |
79 | 76 |
|
80 | | - setTimeout(abort, ABORT_DELAY); |
81 | | - }); |
| 77 | + setTimeout(abort, ABORT_DELAY) |
| 78 | + }) |
82 | 79 | } |
0 commit comments