Skip to content

Commit 033ce1f

Browse files
refactoring
1 parent 434d173 commit 033ce1f

File tree

7 files changed

+24
-26
lines changed

7 files changed

+24
-26
lines changed

lib/react_on_rails/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def should_raise_streaming_prerender_error?(chunk_json_result, render_options)
644644
end
645645

646646
# Returns object with values that are NOT html_safe!
647-
def server_rendered_react_component(render_options) # rubocop:disable Metrics/CyclomaticComplexity
647+
def server_rendered_react_component(render_options)
648648
return { "html" => "", "consoleReplayScript" => "" } unless render_options.prerender
649649

650650
react_component_name = render_options.react_component_name

lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def reset_pool_if_server_bundle_was_modified
5050
# Note, js_code does not have to be based on React.
5151
# js_code MUST RETURN json stringify Object
5252
# Calling code will probably call 'html_safe' on return value before rendering to the view.
53-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
53+
# rubocop:disable Metrics/CyclomaticComplexity
5454
def exec_server_render_js(js_code, render_options, js_evaluator = nil)
5555
js_evaluator ||= self
5656
if render_options.trace
@@ -89,7 +89,7 @@ def exec_server_render_js(js_code, render_options, js_evaluator = nil)
8989
# We need to parse each chunk and replay the console messages.
9090
result.transform { |chunk| parse_result_and_replay_console_messages(chunk, render_options) }
9191
end
92-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
92+
# rubocop:enable Metrics/CyclomaticComplexity
9393

9494
def trace_js_code_used(msg, js_code, file_name = "tmp/server-generated.js", force: false)
9595
return unless ReactOnRails.configuration.trace || force

node_package/src/loadReactClientManifest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ export default function loadReactClientManifest(reactClientManifestFileName: str
99
// Thus, the __dirname of this code is where we can find the manifest file.
1010
const manifestPath = path.resolve(__dirname, reactClientManifestFileName);
1111
if (!loadedReactClientManifests.has(manifestPath)) {
12-
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
13-
loadedReactClientManifests.set(manifestPath, manifest);
12+
// TODO: convert to async
13+
try {
14+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
15+
loadedReactClientManifests.set(manifestPath, manifest);
16+
} catch (error) {
17+
throw new Error(`Failed to load React client manifest from ${manifestPath}: ${error}`);
18+
}
1419
}
1520

1621
return loadedReactClientManifests.get(manifestPath)!;

node_package/src/registerServerComponent.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import RSCClientRoot from './RSCClientRoot';
44
import { RegisterServerComponentOptions } from './types';
55

66
const registerServerComponent = (options: RegisterServerComponentOptions, ...componentNames: string[]) => {
7-
const componentsWrappedInRSCClientRoot = componentNames.reduce(
8-
(acc, name) => ({
9-
...acc,
10-
[name]: () => React.createElement(RSCClientRoot, {
11-
componentName: name,
12-
rscRenderingUrlPath: options.rscRenderingUrlPath
13-
})
14-
}),
15-
{}
16-
);
7+
const componentsWrappedInRSCClientRoot: Record<string, () => React.ReactElement> = {};
8+
for (const name of componentNames) {
9+
componentsWrappedInRSCClientRoot[name] = () => React.createElement(RSCClientRoot, {
10+
componentName: name,
11+
rscRenderingUrlPath: options.rscRenderingUrlPath
12+
});
13+
}
1714
ReactOnRails.register(componentsWrappedInRSCClientRoot);
1815
};
1916

node_package/src/transformRSCStreamAndReplayConsoleLogs.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ export default function transformRSCStreamAndReplayConsoleLogs(stream: ReadableS
1010
while (!done) {
1111
const decodedValue = lastIncompleteChunk + decoder.decode(value);
1212
const chunks = decodedValue.split('\n');
13-
if (!decodedValue.endsWith('\n')) {
14-
lastIncompleteChunk = chunks.pop() ?? '';
15-
} else {
16-
lastIncompleteChunk = '';
17-
}
13+
lastIncompleteChunk = chunks.pop() ?? '';
1814

1915
const jsonChunks = chunks
2016
.filter(line => line.trim() !== '')

spec/dummy/spec/support/script_tag_utils.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
opts = {}
77
match do |actual|
88
Nokogiri::HTML.fragment(actual).css("script").map do |script|
9-
EquivalentXml.equivalent?(Nokogiri::HTML.fragment(script.to_s.delete(" \t\r\n")),
10-
Nokogiri::HTML.fragment(expected.delete(" \t\r\n")),
9+
EquivalentXml.equivalent?(Nokogiri::HTML.fragment(script.to_s),
10+
Nokogiri::HTML.fragment(expected),
1111
opts)
1212
end.include?(true)
1313
end

spec/react_on_rails/utils_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ def mock_dev_server_running
215215

216216
context "with server file in the manifest, used for client", packer_type.to_sym do
217217
it "returns the correct path hashed server path" do
218-
Packer = ReactOnRails::PackerUtils.packer # rubocop:disable Lint/ConstantDefinitionInBlock, RSpec/LeakyConstantDeclaration
218+
packer = ReactOnRails::PackerUtils.packer
219219
mock_bundle_configs(server_bundle_name: "webpack-bundle.js")
220220
allow(ReactOnRails).to receive_message_chain("configuration.same_bundle_for_client_and_server")
221221
.and_return(true)
222222
mock_bundle_in_manifest("webpack-bundle.js", "webpack/development/webpack-bundle-123456.js")
223-
allow(Packer).to receive_message_chain("dev_server.running?")
223+
allow(packer).to receive_message_chain("dev_server.running?")
224224
.and_return(false)
225225

226226
path = described_class.server_bundle_js_file_path
@@ -278,12 +278,12 @@ def mock_dev_server_running
278278

279279
context "with server file in the manifest, used for client", packer_type.to_sym do
280280
it "returns the correct path hashed server path" do
281-
Packer = ReactOnRails::PackerUtils.packer # rubocop:disable Lint/ConstantDefinitionInBlock, RSpec/LeakyConstantDeclaration
281+
packer = ReactOnRails::PackerUtils.packer
282282
mock_bundle_configs(rsc_bundle_name: "webpack-bundle.js")
283283
allow(ReactOnRails).to receive_message_chain("configuration.same_bundle_for_client_and_server")
284284
.and_return(true)
285285
mock_bundle_in_manifest("webpack-bundle.js", "webpack/development/webpack-bundle-123456.js")
286-
allow(Packer).to receive_message_chain("dev_server.running?")
286+
allow(packer).to receive_message_chain("dev_server.running?")
287287
.and_return(false)
288288

289289
path = described_class.rsc_bundle_js_file_path

0 commit comments

Comments
 (0)