-
Notifications
You must be signed in to change notification settings - Fork 11
fix(android): actually download remote http(s) jsBundleSource #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
akelmanson
wants to merge
3
commits into
callstackincubator:main
from
akelmanson:fix/android-remote-http-bundle
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,13 +27,20 @@ import com.facebook.react.runtime.ReactHostImpl | |
| import com.facebook.react.runtime.hermes.HermesInstance | ||
| import com.facebook.react.shell.MainReactPackage | ||
| import com.facebook.react.uimanager.ViewManager | ||
| import java.io.File | ||
| import java.net.HttpURLConnection | ||
| import java.net.URL | ||
|
|
||
| class SandboxReactNativeDelegate( | ||
| private val context: Context, | ||
| ) { | ||
| companion object { | ||
| private const val TAG = "SandboxRNDelegate" | ||
|
|
||
| private const val REMOTE_BUNDLE_CONNECT_TIMEOUT_MS = 10_000 | ||
| private const val REMOTE_BUNDLE_READ_TIMEOUT_MS = 15_000 | ||
| private const val REMOTE_BUNDLE_DOWNLOAD_TIMEOUT_MS = 20_000L | ||
|
|
||
| private val sharedHosts = mutableMapOf<String, SharedReactHost>() | ||
| private val registeredSubstitutionPackages = mutableListOf<ReactPackage>() | ||
| private val registeredHostPackages = mutableListOf<ReactPackage>() | ||
|
|
@@ -166,13 +173,20 @@ class SandboxReactNativeDelegate( | |
| val componentFactory = ComponentFactory() | ||
| DefaultComponentsRegistry.register(componentFactory) | ||
|
|
||
| // For a remote (http/https) bundle, disable developer support on this | ||
| // ReactHost. With dev support enabled the runtime ignores jsBundleLoader | ||
| // and fetches the bundle from the Metro dev server using jsMainModulePath, | ||
| // turning the URL into http://localhost:8081/<url>.bundle (a 404). Local | ||
| // sources ("index"/asset names) keep dev support so Fast Refresh works. | ||
| val isRemoteBundle = capturedBundleSource.startsWith("http://") || | ||
| capturedBundleSource.startsWith("https://") | ||
| host = | ||
| ReactHostImpl( | ||
| sandboxContext, | ||
| hostDelegate, | ||
| componentFactory, | ||
| true, | ||
| true, | ||
| !isRemoteBundle, | ||
| !isRemoteBundle, | ||
| ) | ||
|
|
||
| ownsReactHost = true | ||
|
|
@@ -259,7 +273,66 @@ class SandboxReactNativeDelegate( | |
| if (bundleSource.isEmpty()) return null | ||
| return when { | ||
| bundleSource.startsWith("http://") || bundleSource.startsWith("https://") -> { | ||
| JSBundleLoader.createFileLoader(bundleSource) | ||
| // JSBundleLoader.createFileLoader(url) does not download anything: | ||
| // loadScriptFromFile treats the argument as a local path, so a remote | ||
| // URL never resolves. Prefetch the bundle to a cache file (off the main | ||
| // thread to avoid NetworkOnMainThreadException) and hand it to the | ||
| // network loader, which keeps the original sourceURL for stack traces. | ||
| // | ||
| // The cache file is keyed by the URL, and a remote URL is treated as | ||
| // immutable: if it was already downloaded, reuse the cached copy and | ||
| // skip the network entirely. This makes repeated loads (e.g. every cold | ||
| // start) instant and offline-capable. To ship an update, change the URL | ||
| // (a version segment or `?v=` query) — a new URL misses the cache and is | ||
| // fetched once. Serving changing content at a fixed URL won't update. | ||
| val cacheFile = File( | ||
| context.cacheDir, | ||
| "sandbox-remote-${bundleSource.hashCode()}.bundle", | ||
| ) | ||
| if (cacheFile.exists() && cacheFile.length() > 0L) { | ||
| Log.d(TAG, "Reusing cached bundle '$bundleSource' (${cacheFile.length()} bytes)") | ||
| } else { | ||
| var downloadError: Exception? = null | ||
| val worker = Thread { | ||
| try { | ||
| val connection = URL(bundleSource).openConnection() as HttpURLConnection | ||
| connection.connectTimeout = REMOTE_BUNDLE_CONNECT_TIMEOUT_MS | ||
| connection.readTimeout = REMOTE_BUNDLE_READ_TIMEOUT_MS | ||
| try { | ||
| connection.inputStream.use { input -> | ||
| cacheFile.outputStream().use { output -> input.copyTo(output) } | ||
| } | ||
| } finally { | ||
| connection.disconnect() | ||
| } | ||
| } catch (e: Exception) { | ||
| downloadError = e | ||
| } | ||
| } | ||
| worker.start() | ||
| worker.join(REMOTE_BUNDLE_DOWNLOAD_TIMEOUT_MS) | ||
| if (downloadError != null || !cacheFile.exists() || cacheFile.length() == 0L) { | ||
| // Drop a partial file so a later load can retry cleanly. | ||
| cacheFile.delete() | ||
| Log.e(TAG, "Failed to download remote bundle '$bundleSource'", downloadError) | ||
| return null | ||
| } | ||
| Log.d(TAG, "Downloaded remote bundle '$bundleSource' (${cacheFile.length()} bytes)") | ||
| } | ||
| // Load the cached bundle synchronously (loadSynchronously = true). | ||
| // createCachedBundleFromNetworkLoader() loads with loadSynchronously | ||
| // = false, which on RN 0.85 bridgeless executes the script before the | ||
| // TurboModule JSI bindings are installed on the runtime — the bundle's | ||
| // InitializeCore then hits TurboModuleRegistry.getEnforcing('Platform- | ||
| // Constants') with no proxy registered and the VM aborts. A synchronous | ||
| // load mirrors how the built-in asset loader (createAssetLoader(.., true)) | ||
| // runs the main bundle, which sequences correctly. We keep the original | ||
| // URL as sourceURL so stack traces still point at the remote bundle. | ||
|
Comment on lines
+322
to
+330
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same advice as for the previous comment |
||
| JSBundleLoader.createFileLoader( | ||
| cacheFile.absolutePath, | ||
| bundleSource, | ||
| true, | ||
| ) | ||
| } | ||
|
|
||
| else -> { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to simplify it to 2-3 lines, current explanation is overkill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it makes sense to create some utility / private function for donwload file?