-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathentry.server.tsx
More file actions
76 lines (69 loc) · 2.45 KB
/
entry.server.tsx
File metadata and controls
76 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { PassThrough } from "node:stream"
import { createReadableStreamFromReadable } from "@react-router/node"
import { createInstance } from "i18next"
import { isbot } from "isbot"
import { renderToPipeableStream } from "react-dom/server"
import { I18nextProvider, initReactI18next } from "react-i18next"
import { type EntryContext, ServerRouter, type unstable_RouterContextProvider } from "react-router"
import i18n from "./localization/i18n" // your i18n configuration file
import i18nextOpts from "./localization/i18n.server"
import { resources } from "./localization/resource"
import { globalAppContext } from "./server/context"
// Reject all pending promises from handler functions after 10 seconds
export const streamTimeout = 10000
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
context: EntryContext,
appContext: unstable_RouterContextProvider
) {
const ctx = appContext.get(globalAppContext)
const callbackName = isbot(request.headers.get("user-agent")) ? "onAllReady" : "onShellReady"
const instance = createInstance()
const lng = ctx.lang
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const ns = i18nextOpts.getRouteNamespaces(context as any)
await instance
.use(initReactI18next) // Tell our instance to use react-i18next
.init({
...i18n, // spread the configuration
lng, // The locale we detected above
ns, // The namespaces the routes about to render wants to use
resources,
})
return new Promise((resolve, reject) => {
let didError = false
const { pipe, abort } = renderToPipeableStream(
<I18nextProvider i18n={instance}>
<ServerRouter context={context} url={request.url} />
</I18nextProvider>,
{
[callbackName]: () => {
const body = new PassThrough()
const stream = createReadableStreamFromReadable(body)
responseHeaders.set("Content-Type", "text/html")
resolve(
// @ts-expect-error
ctx.body(stream, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
)
pipe(body)
},
onShellError(error: unknown) {
reject(error)
},
onError(error: unknown) {
didError = true
// biome-ignore lint/suspicious/noConsole: We console log the error
console.error(error)
},
}
)
// Abort the streaming render pass after 11 seconds so to allow the rejected
// boundaries to be flushed
setTimeout(abort, streamTimeout + 1000)
})
}