Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 809
Rakesh Gujari Avatar asked Feb 19 '18 10:02

Rakesh Gujari


People also ask

How download file from server location react JS?

To download a file with React. js, we can add the download attribute to an anchor element. We just add the download prop to do the download. If we don't want to use an anchor element, we can also use the file-saver package to download our file.

How do I download a react js file?

It is possible by using the fetch() method provided by Java Script. The PDF file will be placed in the public folder of React JS application folder structure.

Does React Native use JavaScriptCore?

In most cases, React Native will use JavaScriptCore, the JavaScript engine that powers Safari. Note that on iOS, JavaScriptCore does not use JIT due to the absence of writable executable memory in iOS apps.


1 Answers

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 produces "text/javascript"

  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]

like image 85
Rakesh Gujari Avatar answered Sep 18 '22 15:09

Rakesh Gujari