Why doesn't React on Rails hydrate at the DOMContentLoaded
event?
#1665
-
In React on Rails, hydration and rendering sometimes occur at the react_on_rails/node_package/src/clientStartup.ts Lines 310 to 337 in 4339d30
This behavior causes hydration to wait until all sub-resources, such as third-party JavaScript or Google Fonts, are fully loaded, potentially delaying the user’s interaction readiness. Would it be possible to allow hydration at the function onPageReady(callback: () => void) {
- if (document.readyState === "complete") {
+ if (document.readyState !== "loading") {
callback();
} else {
document.addEventListener("readystatechange", function onReadyStateChange() {
onPageReady(callback);
document.removeEventListener("readystatechange", onReadyStateChange);
});
}
} This adjustment ensures hydration begins as soon as the Are there any reasons or side effects that might prevent this change from being implemented? If this approach is acceptable, I’d be happy to submit a PR for it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
As it happens, I was looking into this as well. It used to be on And that mentions 7b301f9 which changed it the other way around earlier... @Judahmeek @justin808 Do you remember what exactly "the race condition revealed by #1539" was? And was the intention actually "after
I don't think that's correct; when the state changes to |
Beta Was this translation helpful? Give feedback.
@kotarella1110 We already implemented an approach similar to your approach here #1656. The PR will be merged very soon. But for backward compatibility, you need to pass
force_load: true
on the server side for any component you want to hydrate immediately.