-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
feat: add default web-streams based server entry #14759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edmundhung
wants to merge
5
commits into
remix-run:dev
Choose a base branch
from
edmundhung:default-server-entry-web
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−21
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d743c1
feat: add default server entry for non-Node runtimes
edmundhung a599bdd
remove node runtime check from default server entry
edmundhung fb934d2
update contributors.yml
edmundhung 188d4a3
improve runtime detection in default server entries
edmundhung 4af667d
improve the name of the added test
edmundhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,7 @@ | |
| - dokeet | ||
| - doytch | ||
| - Drishtantr | ||
| - edmundhung | ||
| - edwin177 | ||
| - eiffelwong1 | ||
| - ek9 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 27 additions & 5 deletions
32
...-cloudflare-template/app/entry.server.tsx → ...-dev/config/defaults/entry.server.web.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,46 @@ | ||
| import type { AppLoadContext, EntryContext } from "react-router"; | ||
| import { ServerRouter } from "react-router"; | ||
| import { isbot } from "isbot"; | ||
| import { renderToReadableStream } from "react-dom/server"; | ||
| import * as ReactDOMServer from "react-dom/server"; | ||
|
|
||
| // ReactDOMServer.renderToReadableStream is not available in Node.js until React 19.2.0+ | ||
| if (typeof ReactDOMServer.renderToReadableStream !== "function") { | ||
| throw new Error( | ||
| `ReactDOMServer.renderToReadableStream() is not available. ` + | ||
| `React Router uses this API when @react-router/node is not installed. ` + | ||
| `Please install @react-router/node, or provide a custom entry.server.tsx/jsx file in your app directory.`, | ||
| ); | ||
| } | ||
|
Comment on lines
+6
to
+13
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check is still needed because Without this check, users on React 18.x or React 19.0.0-19.1.x running on Node.js would get a cryptic error because |
||
|
|
||
| export const streamTimeout = 5_000; | ||
|
|
||
| export default async function handleRequest( | ||
| request: Request, | ||
| responseStatusCode: number, | ||
| responseHeaders: Headers, | ||
| routerContext: EntryContext, | ||
| _loadContext: AppLoadContext, | ||
| loadContext: AppLoadContext, | ||
| // If you have middleware enabled: | ||
| // loadContext: RouterContextProvider | ||
| ) { | ||
| // https://httpwg.org/specs/rfc9110.html#HEAD | ||
| if (request.method.toUpperCase() === "HEAD") { | ||
| return new Response(null, { | ||
| status: responseStatusCode, | ||
| headers: responseHeaders, | ||
| }); | ||
| } | ||
|
|
||
| let shellRendered = false; | ||
| const userAgent = request.headers.get("user-agent"); | ||
| let userAgent = request.headers.get("user-agent"); | ||
|
|
||
| const body = await renderToReadableStream( | ||
| const body = await ReactDOMServer.renderToReadableStream( | ||
| <ServerRouter context={routerContext} url={request.url} />, | ||
| { | ||
| signal: AbortSignal.timeout(streamTimeout + 1000), | ||
| onError(error: unknown) { | ||
| responseStatusCode = 500; | ||
| // Log streaming rendering errors from inside the shell. Don't log | ||
| // Log streaming rendering errors from inside the shell. Don't log | ||
| // errors encountered during initial shell rendering since they'll | ||
| // reject and get logged in handleDocumentRequest. | ||
| if (shellRendered) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helps users migrating from Node.js to non-Node runtimes. If they forget to remove
@react-router/node, React Router selects this entry butrenderToPipeableStreamwon't be available.The check provides a clear error message instead of a cryptic failure.