Skip to content

Commit 4214a6e

Browse files
Enhance RSCPayloadContainer documentation on escape sequences and modify chunk index validation in RSCServerRoot for improved error handling.
1 parent 7d2b0d9 commit 4214a6e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

node_package/src/RSCPayloadContainer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ type RSCPayloadContainerInnerProps = {
1414
getChunkPromise: (chunkIndex: number) => Promise<StreamChunk>;
1515
};
1616

17+
// In JavaScript, when an escape sequence with a backslash (\) is followed by a character
18+
// that isn't a recognized escape character, the backslash is ignored, and the character
19+
// is treated as-is.
20+
// This behavior allows us to use the backslash to escape characters that might be
21+
// interpreted as HTML tags, preventing them from being processed by the HTML parser.
22+
// For example, we can escape the comment tag <!-- as <\!-- and the script tag </script>
23+
// as <\/script>.
24+
// This ensures that these tags are not prematurely closed or misinterpreted by the browser.
1725
function escapeScript(script: string) {
1826
return script.replace(/<!--/g, '<\\!--').replace(/<\/(script)/gi, '</\\$1');
1927
}
@@ -104,7 +112,7 @@ export default function RSCPayloadContainerWrapper({ RSCPayloadStream }: RSCPayl
104112

105113
const getChunkPromise = React.useCallback(
106114
(chunkIndex: number) => {
107-
if (chunkIndex > chunkPromises.length) {
115+
if (chunkIndex >= chunkPromises.length) {
108116
throw new Error('React on Rails Error: RSC Chunk index out of bounds');
109117
}
110118

node_package/src/RSCServerRoot.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,15 @@ const RSCServerRoot: RenderFunction = async (
104104
rscPayloadStream.pipe(rscPayloadStream2);
105105
const serverComponentElement = createFromReactOnRailsNodeStream(rscPayloadStream1, ssrManifest);
106106

107-
return () => (
108-
<>
109-
<React.Fragment key="serverComponentElement">{use(serverComponentElement)}</React.Fragment>
110-
<RSCPayloadContainer RSCPayloadStream={rscPayloadStream2} key="rscPayloadContainer" />
111-
</>
112-
);
107+
return () => {
108+
const resolvedServerComponent = use(serverComponentElement);
109+
return (
110+
<>
111+
<React.Fragment key="serverComponentElement">{resolvedServerComponent}</React.Fragment>
112+
<RSCPayloadContainer RSCPayloadStream={rscPayloadStream2} key="rscPayloadContainer" />
113+
</>
114+
);
115+
};
113116
};
114117

115118
export default RSCServerRoot;

0 commit comments

Comments
 (0)