Chrome offline mode

Last week, I received a request from a client to make an EPOS app. The main requirement is the app must work even when internet connection is lost. So when it can’t connect to internet, the app will still store new customer and new order information into browser cache and when it can connect to internet again, it will call api to sync data changes to server. This is an interesting project so I decided to give it a try. After 15 minutes on Google, I found this library: redux-offline. From their introduction, it looks like exactly what I want for my app. So I made simple todo app with redux-offline to test out this library.

You can clone and test the example at here: https://github.com/davidtran/react-todo-offline. This repo comes with a small ExpressJS to store todos and a example React todo app.

Step to test:

  1. Clone and run the example.
  2. Disconnect your computer from internet.
  3. Add some todos. Your todos will be stored in localStorage and is waiting to save to server.
  4. Turn on internet again. redux-offline will call API to store our todos to server.

Let’s see the implementation.

In order to get started, I create a simple backend for the app. It has 3 APIs to get todo list, post new todo and remove a todo.

Now let’s create our React app. Because other parts of our app are not important, I only show the code of main component at here:

Because of testing purpose, I put everything in one component. This is what this component does:

  • Connect to redux store to get todo list
  • Display the todo list
  • Call method to post new todo
  • Call method to remove existing todo

Now let’s see how Redux is setup with Redux-Offline. This is the action creator for creating new todo:

Since each todo item must have a unique key, so I create a temp Id in the addTodo method – the tempId will be replace by real ID from API later. I pass tempId to addTodoWithId. addTodoWithId is the main action which will create new todo. The redux-offline setting stay in the meta.offline property of the action object:

  • effect contains the configuration object for API call. This object contains API url, API method and body content of API. By default, redux-offline uses fetch to call API, but you can configure it to use Axios or any other HTTP clients. Read more about it at here: https://github.com/jevakallio/redux-offline#configuration
  • commit contains the action which will be call after API success.
  • rollback contains the action that will be call when API fails ( API returns 500 status code).

This is the reducer:

When ADD_TODO action is dispatched, a new todo is added into Redux store. At this moment, that todo is already stored in browser cache but it is not stored in backend yet and API have not been called. So even when your app is offline, the new todo is still displayed in the app and redux-offline will wait until our app is online again to call API to store our todo. isTemp is true so we can display a spinner if we need.

When redux-offline successful call API to store todo in backend. It will dispatch ADD_TODO_COMMIT. The response from API api is stored in action.payload. At this moment, we replace the temp ID with the real ID from API. We also set isTemp to false so we can hide our spinner.

ADD_TODO_ROLLBACK is dispatched when API is failed. It means our API returns status code 500 or server doesn’t work. When network is too slow or not stable, redux-offline will try to call API few times before dispatch ADD_TODO_ROLLBACK.