Skip to content

Commit 671ea38

Browse files
robhoganfacebook-github-bot
authored andcommitted
Use Content-Location header in bundle response as JS source URL (#37501)
Summary: Pull Request resolved: #37501 This is the iOS side of the fix for #36794. That issue aside for the moment, the high-level idea here is to conceptually separate the bundle *request URL*, which represents a request for the *latest* bundle, from the *source URL* passed to JS engines, which should represent the code actually being executed. In future, we'd like to use this to refer to a point-in-time snapshot of the bundle, so that stack traces more often refer to the code that was actually run, even if it's since been updated on disk (actually implementing this isn't planned at the moment, but it helps describe the distinction). Short term, this separation gives us a way to address the issue with JSC on iOS 16.4 by allowing Metro to provide the client with a [JSC-safe URL](react-native-community/discussions-and-proposals#646) to pass to the JS engine, even where the request URL isn't JSC-safe. We'll deliver that URL to the client on HTTP bundle requests via the [`Content-Location`](https://www.rfc-editor.org/rfc/rfc9110#name-content-location) header, which is a published standard for communicating a location for the content provided in a successful response (typically used to provide a direct URL to an asset after content negotiation, but I think it fits here too). For the long-term goal we should follow up with the same functionality on Android and out-of-tree platforms, but it's non-essential for anything other than iOS 16.4 at the moment. For the issue fix to work end-to-end we'll also need to update Metro, but the two pieces are decoupled and non-breaking so it doesn't matter which lands first. Changelog: [iOS][Changed] Prefer `Content-Location` header in bundle response as JS source URL Reviewed By: huntie Differential Revision: D45950661 fbshipit-source-id: 170fcd63a098f81bdcba55ebde0cf3569dceb88d
1 parent c05d822 commit 671ea38

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/react-native/React/Base/RCTJavaScriptLoader.mm

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,17 @@ static void attemptAsynchronousLoadOfBundleAtURL(
303303
return;
304304
}
305305

306-
RCTSource *source = RCTSourceCreate(scriptURL, data, data.length);
306+
// Prefer `Content-Location` as the canonical source URL, if given, or fall back to scriptURL.
307+
NSURL *sourceURL = scriptURL;
308+
NSString *contentLocationHeader = headers[@"Content-Location"];
309+
if (contentLocationHeader) {
310+
NSURL *contentLocationURL = [NSURL URLWithString:contentLocationHeader relativeToURL:scriptURL];
311+
if (contentLocationURL) {
312+
sourceURL = contentLocationURL;
313+
}
314+
}
315+
316+
RCTSource *source = RCTSourceCreate(sourceURL, data, data.length);
307317
parseHeaders(headers, source);
308318
onComplete(nil, source);
309319
}

packages/react-native/React/CxxBridge/RCTCxxBridge.mm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ - (void)start
475475
// Load the source asynchronously, then store it for later execution.
476476
dispatch_group_enter(prepareBridge);
477477
__block NSData *sourceCode;
478+
__block NSURL *sourceURL = self.bundleURL;
478479

479480
#if RCT_DEV_MENU && __has_include(<React/RCTDevLoadingViewProtocol.h>)
480481
{
@@ -490,6 +491,9 @@ - (void)start
490491
}
491492

492493
sourceCode = source.data;
494+
if (source.url) {
495+
sourceURL = source.url;
496+
}
493497
dispatch_group_leave(prepareBridge);
494498
}
495499
onProgress:^(RCTLoadingProgress *progressData) {
@@ -504,7 +508,7 @@ - (void)start
504508
dispatch_group_notify(prepareBridge, dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
505509
RCTCxxBridge *strongSelf = weakSelf;
506510
if (sourceCode && strongSelf.loading) {
507-
[strongSelf executeSourceCode:sourceCode sync:NO];
511+
[strongSelf executeSourceCode:sourceCode withSourceURL:sourceURL sync:NO];
508512
}
509513
});
510514
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
@@ -1050,7 +1054,7 @@ - (void)registerModuleForFrameUpdates:(id<RCTBridgeModule>)module withModuleData
10501054
[_displayLink registerModuleForFrameUpdates:module withModuleData:moduleData];
10511055
}
10521056

1053-
- (void)executeSourceCode:(NSData *)sourceCode sync:(BOOL)sync
1057+
- (void)executeSourceCode:(NSData *)sourceCode withSourceURL:(NSURL *)url sync:(BOOL)sync
10541058
{
10551059
// This will get called from whatever thread was actually executing JS.
10561060
dispatch_block_t completion = ^{
@@ -1075,12 +1079,13 @@ - (void)executeSourceCode:(NSData *)sourceCode sync:(BOOL)sync
10751079
};
10761080

10771081
if (sync) {
1078-
[self executeApplicationScriptSync:sourceCode url:self.bundleURL];
1082+
[self executeApplicationScriptSync:sourceCode url:url];
10791083
completion();
10801084
} else {
1081-
[self enqueueApplicationScript:sourceCode url:self.bundleURL onComplete:completion];
1085+
[self enqueueApplicationScript:sourceCode url:url onComplete:completion];
10821086
}
10831087

1088+
// Use the original request URL here - HMRClient uses this to derive the /hot URL and entry point.
10841089
[self.devSettings setupHMRClientWithBundleURL:self.bundleURL];
10851090
}
10861091

0 commit comments

Comments
 (0)