Skip to content

Commit

Permalink
Refactor server entry file to improve promise handling and adjust str…
Browse files Browse the repository at this point in the history
…eam timeout settings
  • Loading branch information
ferdinandsalis committed Jan 23, 2025
1 parent dcd0f28 commit d161dfe
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { PassThrough } from 'stream'

import {
type ActionFunctionArgs,
type LoaderFunctionArgs,
type HandleDocumentRequestFunction,
} from 'react-router'

import { createReadableStreamFromReadable } from '@react-router/node'
import { ServerRouter } from 'react-router'
import * as Sentry from '@sentry/remix'
import chalk from 'chalk'
import { isbot } from 'isbot'
import { renderToPipeableStream } from 'react-dom/server'
import {
ServerRouter,
type ActionFunctionArgs,
type LoaderFunctionArgs,
type HandleDocumentRequestFunction,
} from 'react-router'
import { getEnv, init } from './utils/env.server.ts'
import { makeTimings } from './utils/timing.server.ts'

const ABORT_DELAY = 5000

init()
global.ENV = getEnv()

Expand All @@ -30,6 +27,9 @@ if (ENV.MODE === 'production' && ENV.SENTRY_DSN) {

type DocRequestArgs = Parameters<HandleDocumentRequestFunction>

// Reject all pending promises from handler functions after 10 seconds
export const streamTimeout = 10000

export default async function handleRequest(...args: DocRequestArgs) {
const [request, responseStatusCode, responseHeaders, reactRouterContext] =
args
Expand All @@ -42,11 +42,7 @@ export default async function handleRequest(...args: DocRequestArgs) {
const timings = makeTimings('render', 'renderToPipeableStream')

const { pipe, abort } = renderToPipeableStream(
<ServerRouter
abortDelay={ABORT_DELAY}
context={reactRouterContext}
url={request.url}
/>,
<ServerRouter context={reactRouterContext} url={request.url} />,
{
[callbackName]: () => {
const body = new PassThrough()
Expand All @@ -69,7 +65,9 @@ export default async function handleRequest(...args: DocRequestArgs) {
},
)

setTimeout(abort, 10000)
// Abort the streaming render pass after 11 seconds to allow the rejected
// boundaries to be flushed
setTimeout(abort, streamTimeout + 1000)
})
}

Expand Down

0 comments on commit d161dfe

Please sign in to comment.