Skip to content

Commit cb6ef79

Browse files
refactor: optimize RSC payload handling and improve hook management for better performance and clarity
1 parent 8520d4e commit cb6ef79

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

lib/react_on_rails/utils.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ def self.react_on_rails_pro_version
216216
def self.rsc_support_enabled?
217217
return false unless react_on_rails_pro?
218218

219+
return @rsc_support_enabled if defined?(@rsc_support_enabled)
220+
219221
rorp_config = ReactOnRailsPro.configuration
220-
rorp_config.respond_to?(:enable_rsc_support) && rorp_config.enable_rsc_support
222+
@rsc_support_enabled = rorp_config.respond_to?(:enable_rsc_support) && rorp_config.enable_rsc_support
221223
end
222224

223225
def self.full_text_errors_enabled?

node_package/src/RSCPayloadGenerator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare global {
1313
): Promise<NodeJS.ReadableStream>;
1414
}
1515

16-
const mapRailsContextToRSCPayloadStreams = new Map<string, RSCPayloadStreamInfo[]>();
16+
const rscPayloadStreams = new Map<string, RSCPayloadStreamInfo[]>();
1717

1818
const rscPayloadCallbacks = new Map<string, RSCPayloadCallback[]>();
1919

@@ -44,7 +44,7 @@ export const onRSCPayloadGenerated = (
4444
}
4545

4646
// Call callback for any existing streams for this context
47-
const existingStreams = mapRailsContextToRSCPayloadStreams.get(renderRequestId);
47+
const existingStreams = rscPayloadStreams.get(renderRequestId);
4848
if (existingStreams) {
4949
existingStreams.forEach((streamInfo) => callback(streamInfo));
5050
}
@@ -95,11 +95,11 @@ export const getRSCPayloadStream = async (
9595
props,
9696
stream: stream2,
9797
};
98-
const streams = mapRailsContextToRSCPayloadStreams.get(renderRequestId);
98+
const streams = rscPayloadStreams.get(renderRequestId);
9999
if (streams) {
100100
streams.push(streamInfo);
101101
} else {
102-
mapRailsContextToRSCPayloadStreams.set(renderRequestId, [streamInfo]);
102+
rscPayloadStreams.set(renderRequestId, [streamInfo]);
103103
}
104104

105105
// Notify callbacks about the new stream in a sync manner to maintain proper hydration timing
@@ -120,11 +120,11 @@ export const getRSCPayloadStreams = (
120120
stream: NodeJS.ReadableStream;
121121
}[] => {
122122
const { renderRequestId } = railsContext.componentSpecificMetadata;
123-
return mapRailsContextToRSCPayloadStreams.get(renderRequestId) ?? [];
123+
return rscPayloadStreams.get(renderRequestId) ?? [];
124124
};
125125

126126
export const clearRSCPayloadStreams = (railsContext: RailsContextWithServerComponentCapabilities) => {
127127
const { renderRequestId } = railsContext.componentSpecificMetadata;
128-
mapRailsContextToRSCPayloadStreams.delete(renderRequestId);
128+
rscPayloadStreams.delete(renderRequestId);
129129
rscPayloadCallbacks.delete(renderRequestId);
130130
};

node_package/src/injectRSCPayload.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,12 @@ export default function injectRSCPayload(
7878
// The client-side RSCProvider actively monitors the array for new chunks, processing them as they arrive and forwarding them to the RSC payload stream, regardless of whether the array is initially empty.
7979
initializeCacheKeyJSArray(cacheKey, resultStream);
8080
rscPromises.push(
81-
new Promise((resolve, reject) => {
82-
(async () => {
83-
for await (const chunk of stream ?? []) {
84-
const decodedChunk = typeof chunk === 'string' ? chunk : decoder.decode(chunk);
85-
writeChunk(JSON.stringify(decodedChunk), resultStream, cacheKey);
86-
}
87-
resolve();
88-
})().catch(reject);
89-
}),
81+
(async () => {
82+
for await (const chunk of stream ?? []) {
83+
const decodedChunk = typeof chunk === 'string' ? chunk : decoder.decode(chunk);
84+
writeChunk(JSON.stringify(decodedChunk), resultStream, cacheKey);
85+
}
86+
})(),
9087
);
9188
});
9289

node_package/src/postSSRHooks.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ export const addPostSSRHook = (
77
railsContext: RailsContextWithServerComponentCapabilities,
88
hook: PostSSRHook,
99
) => {
10-
const hooks = postSSRHooks.get(railsContext.componentSpecificMetadata.renderRequestId) || [];
11-
hooks.push(hook);
12-
postSSRHooks.set(railsContext.componentSpecificMetadata.renderRequestId, hooks);
10+
const hooks = postSSRHooks.get(railsContext.componentSpecificMetadata.renderRequestId);
11+
if (hooks) {
12+
hooks.push(hook);
13+
} else {
14+
postSSRHooks.set(railsContext.componentSpecificMetadata.renderRequestId, [hook]);
15+
}
1316
};
1417

1518
export const notifySSREnd = (railsContext: RailsContextWithServerComponentCapabilities) => {
1619
const hooks = postSSRHooks.get(railsContext.componentSpecificMetadata.renderRequestId);
1720
if (hooks) {
1821
hooks.forEach((hook) => hook());
1922
}
23+
postSSRHooks.delete(railsContext.componentSpecificMetadata.renderRequestId);
2024
};

0 commit comments

Comments
 (0)