Skip to content

Commit 22a0e5d

Browse files
committed
refactor(runtime): consolidate React globals and streaming setup
- Remove separate react_globals_setup.js and consolidate React initialization into component_eval_setup.js - Simplify React global object setup by removing redundant Component, Fragment, and Suspense assignments - Add null checks in jsx/jsxs functions to handle missing React context gracefully - Integrate spinner keyframes initialization directly into LoadingSpinner setup - Remove module_reload extension and associated initialization logic - Remove callbacks.rs and streaming module from runtime extensions - Consolidate streaming React setup into init_react.js - Update fetch initialization to use simplified web extension setup - Streamline runtime extension module structure by removing unused modules - Reduce boilerplate in RSC rendering core by eliminating duplicate React setup code
1 parent e66b13e commit 22a0e5d

41 files changed

Lines changed: 456 additions & 1297 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/rari/src/rsc/rendering/core/constants.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub const DEFAULT_MAX_CACHE_SIZE: usize = 1000;
1515

1616
pub const BATCH_ERROR_COLLECTION_SCRIPT: &str = include_str!("js/batch_error_collection.js");
1717
pub const EXTENSION_CHECKS_SCRIPT: &str = include_str!("js/extension_checks.js");
18-
pub const REACT_GLOBALS_SETUP_SCRIPT: &str = include_str!("js/react_globals_setup.js");
1918
pub const V8_CACHE_CLEAR_SCRIPT: &str = include_str!("js/v8_cache_clear.js");
2019
pub const PROMISE_MANAGER_CHECK_SCRIPT: &str = include_str!("js/promise_manager_check.js");
2120
pub const SERVER_FUNCTION_RESOLVER_SCRIPT: &str = include_str!("js/server_function_resolver.js");

crates/rari/src/rsc/rendering/core/js/component_eval_setup.js

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,35 @@
11
/* eslint-disable no-use-before-define, no-var, vars-on-top */
2-
// oxlint-disable block-scoped-var, no-use-before-define, no-var, vars-on-top
32
if (typeof _jsx === 'undefined')
43
var _jsx = globalThis['~react']?.jsxRuntime?.jsx || (() => null)
54
if (typeof _jsxs === 'undefined')
65
var _jsxs = globalThis['~react']?.jsxRuntime?.jsxs || (() => null)
76

8-
class ReactComponent {
9-
constructor(props) {
10-
this.props = props
11-
this.state = {}
12-
}
13-
14-
setState(updater) {
15-
if (typeof updater === 'function')
16-
this.state = { ...this.state, ...updater(this.state, this.props) }
17-
else
18-
this.state = { ...this.state, ...updater }
19-
}
20-
21-
render() {
22-
return null
23-
}
24-
}
25-
26-
if (typeof globalThis.React === 'undefined') {
27-
globalThis.React = {
28-
createElement(type, props, ...children) {
29-
const normalizedProps = props ? { ...props } : {}
30-
if (children.length === 1) {
31-
normalizedProps.children = children[0]
32-
}
33-
else if (children.length > 1) {
34-
normalizedProps.children = children
35-
}
36-
37-
return { $$typeof: Symbol.for('react.transitional.element'), type, props: normalizedProps }
38-
},
39-
Fragment(props) { return props?.children ?? null },
40-
Suspense(props) { return props?.children ?? props?.fallback ?? null },
41-
Component: ReactComponent,
42-
}
43-
}
44-
45-
if (!globalThis.React.Suspense)
46-
globalThis.React.Suspense = function (props) { return props?.children ?? props?.fallback ?? null }
47-
48-
if (!globalThis.React.Component)
49-
globalThis.React.Component = ReactComponent
50-
51-
if (typeof globalThis.Suspense === 'undefined')
52-
globalThis.Suspense = globalThis.React.Suspense
53-
54-
if (typeof globalThis.Fragment === 'undefined')
55-
globalThis.Fragment = globalThis.React.Fragment
56-
57-
if (typeof globalThis.Component === 'undefined')
58-
globalThis.Component = globalThis.React.Component
59-
607
if (typeof globalThis.jsx === 'undefined') {
618
globalThis.jsx = function (type, props, key) {
9+
if (!globalThis.React)
10+
return null
11+
6212
return globalThis.React.createElement(type, { ...props, key })
6313
}
6414
}
6515

6616
if (typeof globalThis.jsxs === 'undefined') {
6717
globalThis.jsxs = function (type, props, key) {
18+
if (!globalThis.React)
19+
return null
20+
6821
return globalThis.React.createElement(type, { ...props, key })
6922
}
7023
}
7124

7225
if (typeof globalThis.LoadingSpinner === 'undefined') {
26+
if (typeof document !== 'undefined' && !document.getElementById('spinner-keyframes')) {
27+
const style = document.createElement('style')
28+
style.id = 'spinner-keyframes'
29+
style.textContent = '@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }'
30+
document.head.appendChild(style)
31+
}
32+
7333
globalThis.LoadingSpinner = function () {
7434
return globalThis.React.createElement('div', {
7535
style: {

crates/rari/src/rsc/rendering/core/js/react_globals_setup.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

crates/rari/src/rsc/rendering/core/renderer.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,6 @@ globalThis['~errors'].batch.push({{
225225
.execute_script("extension-checks".to_string(), EXTENSION_CHECKS_SCRIPT.to_string())
226226
.await?;
227227

228-
self.runtime
229-
.execute_script(
230-
"init_react_globals".to_string(),
231-
REACT_GLOBALS_SETUP_SCRIPT.to_string(),
232-
)
233-
.await?;
234-
235228
let html_render_script = include_str!("../layout/js/html_render.js");
236229
self.runtime
237230
.execute_script("html_render".to_string(), html_render_script.to_string())
@@ -707,7 +700,7 @@ globalThis['~errors'].batch.push({{
707700
let render_start = Instant::now();
708701

709702
if !self.initialized {
710-
return Err(RariError::internal("ReactDOMServer not initialized"));
703+
return Err(RariError::internal("RSC renderer not initialized"));
711704
}
712705

713706
if self.is_client_reference(component_id).await {
@@ -840,7 +833,7 @@ globalThis['~errors'].batch.push({{
840833
self.resource_tracker.total_renders.fetch_add(1, Ordering::Relaxed);
841834

842835
if !self.initialized {
843-
return Err(RariError::internal("ReactDOMServer not initialized"));
836+
return Err(RariError::internal("RSC renderer not initialized"));
844837
}
845838

846839
if self.is_client_reference(component_id).await {

crates/rari/src/rsc/rendering/core/utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub fn transform_imports_for_hmr(source: &str) -> String {
3838
let named_imports = named_imports_match.as_str();
3939
let imports: Vec<&str> = named_imports.split(',').map(|s| s.trim()).collect();
4040

41-
result.push_str("if (typeof React === 'undefined') { var React = globalThis.React || { createElement: function() { return null; }, Fragment: function() { return null; } }; }\n");
41+
result.push_str(
42+
"if (typeof React === 'undefined') { var React = globalThis.React; }\n",
43+
);
4244

4345
for import in imports {
4446
let import_name = import.trim();
@@ -65,7 +67,9 @@ pub fn transform_imports_for_hmr(source: &str) -> String {
6567
}
6668

6769
if react_default_import_regex.is_match(trimmed) {
68-
result.push_str("if (typeof React === 'undefined') { var React = globalThis.React || { createElement: function() { return null; }, Fragment: function() { return null; } }; }\n");
70+
result.push_str(
71+
"if (typeof React === 'undefined') { var React = globalThis.React; }\n",
72+
);
6973
continue;
7074
}
7175
} else if trimmed.starts_with("import") && !trimmed.contains("from 'react'") {

crates/rari/src/rsc/rendering/layout/route_composer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ impl RouteComposer {
3535
const startTotal = performance.now();
3636
3737
const React = globalThis.React;
38-
const ReactDOMServer = globalThis.ReactDOMServer;
3938
4039
if (!globalThis['~suspense']) globalThis['~suspense'] = {{}};
4140
globalThis['~suspense'].discoveredBoundaries = [];

crates/rari/src/rsc/rendering/streaming/js/streaming_init.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-undef */
21
if (!globalThis.renderToRsc) {
32
globalThis.renderToRsc = async function (element, clientComponents = {}) {
43
if (!element)
@@ -130,19 +129,6 @@ if (!globalThis['~suspense']) {
130129
return { type: 'div', props: { children: null }, key: null }
131130
}
132131
}
133-
134-
if (!globalThis['~react'])
135-
globalThis['~react'] = {}
136-
if (!globalThis['~react'].patched && typeof React !== 'undefined' && React.createElement) {
137-
globalThis['~react'].originalCreateElement = React.createElement
138-
139-
const createElementOverride = function (type, props, ...children) {
140-
return globalThis['~react'].originalCreateElement(type, props, ...children)
141-
}
142-
143-
React.createElement = createElementOverride
144-
globalThis['~react'].patched = true
145-
}
146132
}
147133
else {
148134
globalThis['~suspense'].discoveredBoundaries = []

crates/rari/src/rsc/rendering/streaming/js/streaming_react_setup.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@
1010
globalThis.React = reactModule
1111
}
1212

13-
if (typeof React === 'undefined' && typeof require !== 'undefined')
14-
globalThis.React = require('react')
15-
16-
if (typeof React !== 'undefined' && React.createElement && !globalThis['~react']?.patched) {
17-
if (!globalThis['~react'])
18-
globalThis['~react'] = {}
19-
globalThis['~react'].originalCreateElement = React.createElement
20-
21-
const createElementOverride = function (type, props, ...children) {
22-
return globalThis['~react'].originalCreateElement(type, props, ...children)
23-
}
24-
25-
Object.defineProperty(React, 'createElement', {
26-
value: createElementOverride,
27-
writable: false,
28-
enumerable: true,
29-
configurable: false,
30-
})
31-
32-
globalThis['~react'].patched = true
33-
}
34-
3513
if (typeof React !== 'undefined' && React.Suspense) {
3614
React['~originalSuspense'] = React.Suspense
3715

@@ -74,22 +52,6 @@
7452
}
7553
}
7654
}
77-
78-
if (typeof React === 'undefined') {
79-
globalThis.React = {
80-
createElement(type, props, ...children) {
81-
return {
82-
type,
83-
props: props ? { ...props, children: children.length > 0 ? children : props.children } : { children },
84-
key: props?.key || null,
85-
}
86-
},
87-
Fragment: Symbol.for('react.fragment'),
88-
Suspense(props) {
89-
return props.children
90-
},
91-
}
92-
}
9355
}
9456
catch (e) {
9557
console.error('Failed to load React in streaming context:', e)

crates/rari/src/runtime/ext/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ mod fs;
2727
mod http;
2828
mod io;
2929
mod kv;
30-
mod module_reload;
3130
mod napi;
3231
mod node;
3332
mod node_crypto;
@@ -39,7 +38,6 @@ mod rsc_modules;
3938
mod rsc_renderer;
4039
mod runtime;
4140
mod server_functions;
42-
mod streaming;
4341
mod web;
4442
mod webgpu;
4543
mod webidl;
@@ -80,7 +78,6 @@ pub(crate) fn extensions(options: &ExtensionOptions, is_snapshot: bool) -> Vec<E
8078

8179
extensions.extend(rari::extensions(is_snapshot));
8280
extensions.extend(promise_manager::extensions(is_snapshot));
83-
extensions.extend(module_reload::extensions(is_snapshot));
8481
extensions.extend(rsc_modules::extensions(is_snapshot));
8582
extensions.extend(server_functions::extensions(is_snapshot));
8683
extensions.extend(react::extensions(is_snapshot));
@@ -95,7 +92,6 @@ pub(crate) fn extensions(options: &ExtensionOptions, is_snapshot: bool) -> Vec<E
9592
.extend(webstorage::extensions(options.webstorage_origin_storage_dir.clone(), is_snapshot));
9693
extensions.extend(websocket::extensions(options.web.clone(), is_snapshot));
9794
extensions.extend(http::extensions((), is_snapshot));
98-
extensions.extend(streaming::extensions(is_snapshot));
9995
extensions.extend(fetch::extensions(is_snapshot));
10096
extensions.extend(ffi::extensions(is_snapshot));
10197
extensions.extend(kv::extensions(options.kv_store.clone(), is_snapshot));

0 commit comments

Comments
 (0)