简体   繁体   中英

React Native Download JS Code from remote server

I am currently doing an R&D in React Native.

I have a requirement where I need to add some modules(Basically code) in the app.

It is impossible in native iOS app to do so.

In React native apps using tools like AppHub and CodePush , we can push builds to production app. There is a library React Native Auto Updater for downloading latest js bundle but it is deprecated now.

Using the above tools, I need to push the build.

I have a scenario where I want the app to fetch and download the bundle kept on a remote server.

So, How can I download JS Code on making a rest API call from App and refresh the app as per the new JS code?

My concern is regarding download of JS Code. I am clear on the Auto Update of apps part.

Thanks in advance.

Eureka!

I accomplished this by doing the following:

  1. Create an offline jsbundle Refer this link

    react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

  2. Create a Rest API for downloading js bundle. Refer this link

    I have used SpringBoot. Make sure you add this to your API

  3. Download the jsbundle in your app, store and get the path of the bundle.

  4. Reset the path of bundle for React native in AppDelegate.m [for iOS] and in ReactApplication class [for Android]

Try the react-native-dynamic-bundle package: https://github.com/mauritsd/react-native-dynamic-bundle

Also, you can download the bundle using react-native-fs :

import RNFS from 'react-native-fs'

export const download = async (fromUrl, bundleName) => {
  return RNFS.downloadFile({
    fromUrl,
    toFile: RNFS.DocumentDirectoryPath + `/${bundleName}.bundle`,
  }).promise
}

Regards, Juan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM