From 63703fb73ca7480c81ac1b9c9e7838908a09fbad Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 6 May 2026 04:21:51 -0700 Subject: [PATCH] Return actual loaded bundle URL from SourceCodeModule Summary: The C++ `SourceCodeModule` (ReactCxxPlatform) always derived `scriptURL` from `DevServerHelper::getBundleUrl()`, coupling the module to dev infrastructure. This diff replaces that dependency with a simple `std::string` parameter passed at construction time. `ReactHost` and `FoxReactHost` store the source URL in a `shared_ptr` that is updated on each successful dev-server bundle load. When `SourceCodeModule` is instantiated (lazily, after the bundle is loaded), the TurboModule provider dereferences the current value and passes it through as a plain string. For file-based bundle loading the URL is cleared, matching the previous behavior. This decouples `SourceCodeModule` from `DevServerHelper` without introducing lambdas, context container lookups, or shared mutable state in the module itself. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D102669868 --- .../react/devsupport/SourceCodeModule.cpp | 9 +-------- .../react/devsupport/SourceCodeModule.h | 10 +++------- .../react/runtime/ReactCxxTurboModuleProvider.cpp | 9 ++++++--- .../react/runtime/ReactCxxTurboModuleProvider.h | 4 +++- .../ReactCxxPlatform/react/runtime/ReactHost.cpp | 5 ++++- .../ReactCxxPlatform/react/runtime/ReactHost.h | 1 + 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.cpp b/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.cpp index ba2bc8d3df36..d7ccc105142e 100644 --- a/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.cpp +++ b/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.cpp @@ -7,17 +7,10 @@ #include "SourceCodeModule.h" -#include -#include - namespace facebook::react { SourceCodeConstants SourceCodeModule::getConstants(jsi::Runtime& /*rt*/) { - std::string scriptURL; - if (auto devServerHelper = devServerHelper_.lock()) { - scriptURL = devServerHelper->getBundleUrl(); - } - return SourceCodeConstants{.scriptURL = scriptURL}; + return SourceCodeConstants{.scriptURL = sourceURL_}; } } // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.h b/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.h index 9582cba32e0c..7924c5ad1b29 100644 --- a/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.h +++ b/packages/react-native/ReactCxxPlatform/react/devsupport/SourceCodeModule.h @@ -13,8 +13,6 @@ namespace facebook::react { -class DevServerHelper; - using SourceCodeConstants = NativeSourceCodeSourceCodeConstants; template <> @@ -22,17 +20,15 @@ struct Bridging : NativeSourceCodeSourceCodeConstantsBridgi class SourceCodeModule : public NativeSourceCodeCxxSpec { public: - explicit SourceCodeModule( - std::shared_ptr jsInvoker, - std::shared_ptr devServerHelper = nullptr) - : NativeSourceCodeCxxSpec(jsInvoker), devServerHelper_(devServerHelper) + explicit SourceCodeModule(std::shared_ptr jsInvoker, std::string sourceURL = "") + : NativeSourceCodeCxxSpec(jsInvoker), sourceURL_(std::move(sourceURL)) { } SourceCodeConstants getConstants(jsi::Runtime &rt); private: - std::weak_ptr devServerHelper_; + std::string sourceURL_; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.cpp b/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.cpp index b86ddd2c0ce2..ab2af66e29e6 100644 --- a/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.cpp +++ b/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.cpp @@ -38,7 +38,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider( std::shared_ptr logBoxSurfaceDelegate, HttpClientFactory httpClientFactory, WebSocketClientFactory webSocketClientFactory, - std::function liveReloadCallback) + std::function liveReloadCallback, + std::shared_ptr sourceURL) : turboModuleProviders_(std::move(turboModuleProviders)), jsInvoker_(std::move(jsInvoker)), onJsError_(std::move(onJsError)), @@ -48,7 +49,8 @@ ReactCxxTurboModuleProvider::ReactCxxTurboModuleProvider( logBoxSurfaceDelegate_(std::move(logBoxSurfaceDelegate)), httpClientFactory_(std::move(httpClientFactory)), webSocketClientFactory_(std::move(webSocketClientFactory)), - liveReloadCallback_(std::move(liveReloadCallback)) {} + liveReloadCallback_(std::move(liveReloadCallback)), + sourceURL_(std::move(sourceURL)) {} std::shared_ptr ReactCxxTurboModuleProvider::operator()( const std::string& name) const { @@ -82,7 +84,8 @@ std::shared_ptr ReactCxxTurboModuleProvider::operator()( } else if (name == ImageLoaderModule::kModuleName) { return std::make_shared(jsInvoker_); } else if (name == SourceCodeModule::kModuleName) { - return std::make_shared(jsInvoker_, devServerHelper_); + return std::make_shared( + jsInvoker_, sourceURL_ ? *sourceURL_ : ""); } else if (name == WebSocketModule::kModuleName) { return std::make_shared( jsInvoker_, webSocketClientFactory_); diff --git a/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.h b/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.h index c031fbb5b610..cb2c6e7edcd4 100644 --- a/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.h +++ b/packages/react-native/ReactCxxPlatform/react/runtime/ReactCxxTurboModuleProvider.h @@ -33,7 +33,8 @@ class ReactCxxTurboModuleProvider final { std::shared_ptr logBoxSurfaceDelegate = nullptr, HttpClientFactory httpClientFactory = nullptr, WebSocketClientFactory webSocketClientFactory = nullptr, - std::function liveReloadCallback = nullptr); + std::function liveReloadCallback = nullptr, + std::shared_ptr sourceURL = nullptr); std::shared_ptr operator()(const std::string &name) const; @@ -48,6 +49,7 @@ class ReactCxxTurboModuleProvider final { HttpClientFactory httpClientFactory_; WebSocketClientFactory webSocketClientFactory_; std::function liveReloadCallback_; + std::shared_ptr sourceURL_; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp index 60fde1385549..a770ba8196b7 100644 --- a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp +++ b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.cpp @@ -261,7 +261,8 @@ void ReactHost::createReactInstance() { reactInstanceData_->logBoxSurfaceDelegate, httpClientFactory, webSocketClientFactory, - std::move(liveReloadCallback)); + std::move(liveReloadCallback), + sourceURL_); reactInstance_->initializeRuntime( { @@ -392,6 +393,7 @@ bool ReactHost::loadScriptFromDevServer() { }) .get(); auto script = std::make_unique(std::move(response)); + *sourceURL_ = bundleUrl; reactInstance_->loadScript(std::move(script), bundleUrl); devServerHelper_->setupHMRClient(); return true; @@ -408,6 +410,7 @@ bool ReactHost::loadScriptFromBundlePath(const std::string& bundlePath) { try { LOG(INFO) << "Loading JS bundle from bundle path: " << bundlePath; auto script = ResourceLoader::getFileContents(bundlePath); + *sourceURL_ = ""; reactInstance_->loadScript(std::move(script), bundlePath); LOG(INFO) << "Loaded JS bundle from bundle path: " << bundlePath; return true; diff --git a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.h b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.h index 47645e4a1198..9ff4a1d68941 100644 --- a/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.h +++ b/packages/react-native/ReactCxxPlatform/react/runtime/ReactHost.h @@ -110,6 +110,7 @@ class ReactHost { std::unique_ptr surfaceManager_; std::shared_ptr devServerHelper_; + std::shared_ptr sourceURL_ = std::make_shared(); std::shared_ptr inspector_; std::unique_ptr packagerConnection_;