Load assets from same folder as JS bundle

Summary: Fixes #3679, see https://github.com/facebook/react-native/issues/3679

The idea is to always load images from the same folder that we loaded JS bundle.

This doesn't change the current behavior, since `imageNamed:` inferred the
absolute path to the image from app's bundle, but now we make it explicit.

The benefit for OTA updates implementations is that they can simply ask RN
to load js from some `~/Documents/<build-id>/main.jsbundle` folder and RN will
look for images in `~/Documents/<build-id>/assets/`.

Note that for Android we will have to come out with a different plan, since
in prod we load images from built-in resources.

public

Reviewed By: vjeux

Differential Revision: D2616995

fb-gh-sync-id: 2906c62380280ecb987525edf9a0e3e727a1008b
This commit is contained in:
Alex Kotliarskyi 2015-11-05 12:46:38 -08:00 committed by facebook-github-bot-5
parent 0c83407dd2
commit 10b599c343
2 changed files with 18 additions and 4 deletions

View File

@ -107,7 +107,7 @@ describe('resolveAssetSource', () => {
describe('bundle was loaded from file on iOS', () => {
beforeEach(() => {
NativeModules.SourceCode.scriptURL =
'file:///Path/To/Simulator/main.bundle';
'file:///Path/To/Sample.app/main.bundle';
Platform.OS = 'ios';
});
@ -126,7 +126,7 @@ describe('resolveAssetSource', () => {
__packager_asset: true,
width: 100,
height: 200,
uri: 'assets/module/a/logo.png',
uri: '/Path/To/Sample.app/assets/module/a/logo.png',
scale: 1,
});
});

View File

@ -26,7 +26,7 @@ var PixelRatio = require('PixelRatio');
var Platform = require('Platform');
var SourceCode = require('NativeModules').SourceCode;
var _serverURL;
var _serverURL, _offlinePath;
function getDevServerURL() {
if (_serverURL === undefined) {
@ -44,6 +44,20 @@ function getDevServerURL() {
return _serverURL;
}
function getOfflinePath() {
if (_offlinePath === undefined) {
var scriptURL = SourceCode.scriptURL;
var match = scriptURL && scriptURL.match(/^file:\/\/(\/.*\/)/);
if (match) {
_offlinePath = match[1];
} else {
_offlinePath = '';
}
}
return _offlinePath;
}
/**
* Returns the path at which the asset can be found in the archive
*/
@ -59,7 +73,7 @@ function getPathInArchive(asset) {
.replace(/^assets_/, ''); // Remove "assets_" prefix
} else {
// E.g. 'assets/AwesomeModule/icon@2x.png'
return getScaledAssetPath(asset);
return getOfflinePath() + getScaledAssetPath(asset);
}
}