4

I am developing an expo app and I was working with some caching features. To test them I have to test my app in offline mode, but if I turn off my wifi (system or device) my app simply is unable to connect to the metro server. Even turning the network settings to offline in debugger didn't work, the app was again completely not loading. How can I simulate offline mode in my device while still being able to connect to the metro bundler?

RobC
  • 20,007
  • 20
  • 62
  • 73
  • Why not disconnect internet when app is fully loaded ? Get the bundle without internet is not possible. You can make a build to test your app – BloodyMonkey Jul 17 '20 at 13:23
  • Does this answer your question? [Running Expo in development offline](https://stackoverflow.com/questions/50423562/running-expo-in-development-offline) – Safeer Jan 14 '21 at 07:33

1 Answers1

1

I struggled with this for a while before I came across an answer. Connect via USB! I have tested this with iPhone only so far, but should work for Android too.

I should caveat that I'm using EAS Build, but this should also work with basic Expo.

With the basic development method (expo start --dev-client), the client app and server connect via your local router. The server automatically listens on an ip address available in the local LAN (that's what the QR code is). This is not helpful for developing offline-first features, because as soon as you disconnect your phone from WiFi, the connection will be lost and the dev app will close. In order to develop offline you can do the following:

  1. Run the following command: ifconfig -a. Take note of highest number network interface on list like en7 for example
  2. Connect phone with usb cable
  3. Rerun ifconfig, two new network interfaces should show up with highest numbers. Take the second highest. E.g. en8. Note down it’s inet ip address
  4. Run export EXPO_DEVTOOLS_LISTEN_ADDRESS=<ip address here> in the shell where you will run the development server
  5. Run export REACT_NATIVE_PACKAGER_HOSTNAME=<ip address here>
  6. expo start --dev-client as normal
  7. The IP address advertised through the QR code will now be the IP for the usb connection that you put in above, so mobile client app will go through the usb cable to reach the dev server on your computer.
  8. Switch on airplane mode on the phone as much as you want. Metro, hot reloading, debugging, all keep functioning, and in fact are usually much faster than via router. Enjoy!