react-on-rails 16.2.0-test.7 → 16.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,11 +51,48 @@ function renderElement(el, railsContext) {
51
51
  try {
52
52
  const domNode = document.getElementById(domNodeId);
53
53
  if (domNode) {
54
+ // Check if this component was already rendered by a previous call
55
+ // This prevents hydration errors when reactOnRailsPageLoaded() is called multiple times
56
+ // (e.g., for asynchronously loaded content)
57
+ const existing = renderedRoots.get(domNodeId);
58
+ if (existing) {
59
+ // Only skip if it's the exact same DOM node and it's still connected to the document.
60
+ // If the node was replaced (e.g., via innerHTML or Turbo), we need to unmount the old
61
+ // root and re-render to the new node to prevent memory leaks and ensure rendering works.
62
+ const sameNode = existing.domNode === domNode && existing.domNode.isConnected;
63
+ if (sameNode) {
64
+ if (trace) {
65
+ console.log(`Skipping already rendered component: ${name} (dom id: ${domNodeId})`);
66
+ }
67
+ return;
68
+ }
69
+ // DOM node was replaced (e.g., via async HTML injection) - clean up the old root
70
+ try {
71
+ if (supportsRootApi &&
72
+ existing.root &&
73
+ typeof existing.root === 'object' &&
74
+ 'unmount' in existing.root) {
75
+ existing.root.unmount();
76
+ }
77
+ else {
78
+ unmountComponentAtNode(existing.domNode);
79
+ }
80
+ }
81
+ catch (unmountError) {
82
+ // Ignore unmount errors for replaced nodes
83
+ if (trace) {
84
+ console.log(`Error unmounting replaced component: ${name}`, unmountError);
85
+ }
86
+ }
87
+ renderedRoots.delete(domNodeId);
88
+ }
54
89
  const componentObj = ComponentRegistry.get(name);
55
90
  if (delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) {
56
91
  return;
57
92
  }
58
- // Hydrate if available and was server rendered
93
+ // Hydrate if the DOM node has content (server-rendered HTML)
94
+ // Since we skip already-rendered components above, this check now correctly
95
+ // identifies only server-rendered content, not previously client-rendered content
59
96
  const shouldHydrate = !!domNode.innerHTML;
60
97
  const reactElementOrRouterResult = createReactOutput({
61
98
  componentObj,
@@ -50,7 +50,16 @@ function initializePageEventListeners() {
50
50
  return;
51
51
  }
52
52
  isPageLifecycleInitialized = true;
53
- if (document.readyState !== 'loading') {
53
+ // Important: replacing this condition with `document.readyState !== 'loading'` is not valid
54
+ // As the core ReactOnRails needs to ensure that all component bundles are loaded and executed before hydrating them
55
+ // If the `document.readyState === 'interactive'`, it doesn't guarantee that deferred scripts are executed
56
+ // the `readyState` can be `'interactive'` while the deferred scripts are still being executed
57
+ // Which will lead to the error `"Could not find component registered with name <component name>"`
58
+ // It will happen if this line is reached before the component chunk is executed on browser and reached the line
59
+ // ReactOnRails.register({ Component });
60
+ // ReactOnRailsPro is resellient against that type of race conditions, but it won't wait for that state anyway
61
+ // As it immediately hydrates the components at the page as soon as its html and bundle is loaded on the browser
62
+ if (document.readyState === 'complete') {
54
63
  setupPageNavigationListeners();
55
64
  }
56
65
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-on-rails",
3
- "version": "16.2.0-test.7",
3
+ "version": "16.2.0",
4
4
  "description": "react-on-rails JavaScript for react_on_rails Ruby gem",
5
5
  "main": "lib/ReactOnRails.full.js",
6
6
  "type": "module",