Book Image

Offline First Web Development

By : Daniel Sauble
Book Image

Offline First Web Development

By: Daniel Sauble

Overview of this book

When building mobile apps, it’s easy to forget about the moments when your users lack a good Internet connection. Put your phone in airplane mode, open a few popular apps, and you’ll quickly see how they handle being offline. From Twitter to Pinterest to Apple Maps, some apps might handle being offline better—but very few do it well. A poor offline experience will result in frustrated users who will abandon your app, or worse, turn to your competitor’s apps Expert or novice, this book will teach you everything you need to know about designing and building a rigorous offline app experience. By putting the offline experience first, you’ll have a solid foundation to build upon, avoiding the unnecessary stress and frustration of trying to retrofit offline capabilities into your finished app. This basic principle, designing for the worst-case scenario, could save you countless hours of wasted effort.
Table of Contents (17 chapters)
Offline First Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing conflict detection


PouchDB is aware of conflicts under the hood. By default, when a conflict occurs, it chooses an arbitrary winner and returns it on request. This isn't quite the behavior that we want. Instead, when a conflict occurs, we want PouchDB to give us a list of all of the conflicts so that the user can resolve them. The first step is altering our stores to detect and expose the conflicts.

Getting PouchDB to return conflicts

The first step is to get PouchDB to return a list of the conflict revisions. Edit the doWithDocs method in the Item store and add the following option to the allDocs method immediately after the endKey option:

conflicts: true

Now, each document with conflicts will have a _conflicts attribute with an array of all the losing conflicts. Once we have this, we can let the user select the revision that they actually want to win and then remove all of the other conflicts.

Attaching conflicts to the item model

Now that PouchDB is returning conflicts, we need...