Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the asset download mechanics of Expo and React-Native during development and production mode?

I am confused about the mechanics of Expo and React-Native, how they download the assets.

The info that I know is, once you build your code, expo prepares a bundle which contains both the javascript (compiled code) and the assets altogether.

And during a debug session, that bundle - as a whole - is first downloaded by the expo client and then the app is started. Which means all the assets that are "import"ed in the code should be in place once the application is started.

On the other hand this is completely opposite when I run the following atomic test code. It takes time to load those assets as if they are "lazy loaded".

So my question is; is that behaviour development mode related or will it still take time to download images in the production mode?

  • If production mode will behave the same then where will it download the assets from?
  • If production mode is different than the development mode, why development mode is arranged to behave differently?
import * as React from 'react';
import { Text, View, StyleSheet, Image, Button } from 'react-native';
import Test0Img from './assets/test0.gif';
import Test1Img from './assets/test1.gif';

export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      imageIndex: 0,
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <Text></Text>
        <Text></Text>
        <Button
          onPress={() => {
            let l_newImageIndex = (this.state.imageIndex + 1) % 2;
            this.setState({ imageIndex: l_newImageIndex });
          }}
          title="Click to Change Image"
        />
        <Image
          source={this.state.imageIndex === 1 ? Test0Img : Test1Img}
          style={{
            width: '100%',
            height: '100%',
            resizeMode: 'contain',
          }}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

Code can be seen in this snack: https://snack.expo.io/@mehmetkaplan/assetdownloadtest

If you run this code in your mobile, most probably you'll observe that the animated gif is not changed easily. But if you run it through web it changes quicker.


Expo documentation states here:

For images that saved to the local filesytem, use Asset.fromModule(image).downloadAsync() to download and cache the image. There is also a loadAsync() helper method to cache a batch of assets.

This is also related with above question, why should we need to cache an image if it is in the local filesystem?


Same question added also to Expo forums, as may be seen here. Linking both so that any possible answer can be found by future visitors.

like image 457
Mehmet Kaplan Avatar asked Sep 02 '19 18:09

Mehmet Kaplan


People also ask

Is React Native Expo good for production?

If you have a project requiring rapid development and have chosen React Native to build cross-platform applications, then Expo is for you. Through Expo, you can quickly develop and deploy React Native applications for iOS and Android.

What does production mode do in Expo?

Production Mode Testing your app's performance, as Development slows your app down considerably. Catching bugs that only show up in production.

How does Expo for React Native work?

The sdkVersion of an Expo app indicates what version of the compiled ObjC/Java/C layer of Expo to use. Each sdkVersion roughly corresponds to a release of React Native plus the Expo libraries in the SDK section of these docs. Expo Go supports many versions of the Expo SDK, but a project can only use one at a time.

What is Expo CLI in React Native?

Expo CLI ( expo-cli ): a developer tool for creating projects, viewing logs, opening on your device, publishing, etc. Expo client: an app on your phone that lets you open your projects while you're working on them, without needing to go through XCode or Android Studio, and also lets other people view them too!


1 Answers

For posterity, here is the answer posted in Expo forum, by an Expo engineer :

During development in the Expo client, the images will be downloaded from your local environment. This will take longer due to the all the extra processes that run during Development Mode such as validation checks, remote debugging, hot reloading if enabled, etc. You can read more about this here: https://docs.expo.io/versions/v34.0.0/workflow/development-mode/

When running a published project within the Expo Client, it will fetch your assets from the CDN (CloudFront) in which case you’ll want to make use of the AppLoading module to pre-fetch the assets and only hide the splash screen after all assets have been loaded into memory.

When building a standalone application, you have the option to bundle your assets into the binary (which most should use unless they have an abnormal amount of assets or assets with heavy file sizes) that will result in the assets being loaded into memory much faster since they will be fetched from the local disk rather than making a network request to the CDN.

like image 103
colinux Avatar answered Sep 16 '22 17:09

colinux