Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-on-rails-pro-node-renderer/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ export default function run(config: Partial<Config>) {
}
},

onUpdateReceived: (obj: unknown) => {
onUpdateReceived: async (obj: unknown) => {
if (!incrementalSink) {
log.error({ msg: 'Unexpected update chunk received after rendering was aborted', obj });
return;
}

try {
log.info(`Received a new update chunk ${JSON.stringify(obj)}`);
incrementalSink.add(obj);
await incrementalSink.add(obj);
} catch (err) {
// Log error but don't stop processing
log.error({ err, msg: 'Error processing update chunk' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getRequestBundleFilePath } from '../shared/utils';

export type IncrementalRenderSink = {
/** Called for every subsequent NDJSON object after the first one */
add: (chunk: unknown) => void;
add: (chunk: unknown) => Promise<void>;
handleRequestClosed: () => void;
};

Expand Down Expand Up @@ -93,11 +93,11 @@ export async function handleIncrementalRenderRequest(
return {
response,
sink: {
add: (chunk: unknown) => {
add: async (chunk: unknown) => {
try {
assertIsUpdateChunk(chunk);
const bundlePath = getRequestBundleFilePath(chunk.bundleTimestamp);
executionContext.runInVM(chunk.updateChunk, bundlePath).catch((err: unknown) => {
await executionContext.runInVM(chunk.updateChunk, bundlePath).catch((err: unknown) => {
log.error({ msg: 'Error running incremental render chunk', err, chunk });
});
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-on-rails-pro-node-renderer/src/worker/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export async function buildExecutionContext(
const objectResult = await result;
result = JSON.stringify(objectResult);
}
if (log.level === 'debug') {
if (log.level === 'debug' && result) {
log.debug(`result from JS:
${smartTrim(result)}`);
const debugOutputPathResult = path.join(serverBundleCachePath, 'result.json');
Expand Down
16 changes: 13 additions & 3 deletions react_on_rails_pro/lib/react_on_rails_pro/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class Request # rubocop:disable Metrics/ClassLength
class << self
def reset_connection
@standard_connection&.close
@incremental_connection&.close
@standard_connection = nil
@incremental_connection = nil
reset_thread_local_incremental_connections
end

def render_code(path, js_code, send_bundle)
Expand Down Expand Up @@ -135,8 +134,19 @@ def connection
end
# rubocop:enable Naming/MemoizedInstanceVariableName

# Thread-local connection for incremental rendering
# Each thread gets its own persistent connection to avoid connection pool issues
def incremental_connection
@incremental_connection ||= create_incremental_connection
Thread.current[:react_on_rails_incremental_connection] ||= create_incremental_connection
end

def reset_thread_local_incremental_connections
# Close all thread-local incremental connections
Thread.list.each do |thread|
conn = thread[:react_on_rails_incremental_connection]
conn&.close
thread[:react_on_rails_incremental_connection] = nil
end
end

def perform_request(path, **post_options) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
Expand Down
Loading