From 10b599c34357b5d1d8e7c31010c0682073612ff6 Mon Sep 17 00:00:00 2001 From: Alex Kotliarskyi Date: Thu, 5 Nov 2015 12:46:38 -0800 Subject: [PATCH] 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//main.jsbundle` folder and RN will look for images in `~/Documents//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 --- .../Image/__tests__/resolveAssetSource-test.js | 4 ++-- Libraries/Image/resolveAssetSource.js | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 6c1029ed7..81fdb2b08 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -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, }); }); diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index a278a708f..638b72ce0 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -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); } }