Local Storage in Web Pages

You,web development

TL;DR

User enters input in a web page, but the internet connection seems to be temporarily down at the time of sending changes to server.

Can we somehow store the entered changes locally, and save to server after the connection is up?

Short Summary:

Limitations:

Getting Hands-on:

  1. Using client side JS, check if we are connected to internet:

     How: ```if(navigator.onLine){/*we are online*/}
           else {/*we are offline*/}```
  2. Design the web app to save content to server (via REST etc) only if we are online if not (i.e. offline), save content locally to ```localStorage``

  3. Save data typed in by user at HTML5 localStorage while offline.

     How: storing ```localStorage.setItem("keyName",objectContainigState;```
    
     retreiving ```objectContainingState = localStorage.getItem("keyName");```
  4. Caching files can be misleading over long term because some files get updated during new releases. The Manifest file thus contains a version nr. More info about how changes are registered via versioning Also information on web server side e.g. Tomcat how to indicate manifest file in .htaccess

  5. Tell browser side to cache certian files

     How:  proiding a text file manifest of relevant files.

Further Reading:

There many articles on this, not all are well written.

  1. This one seems clean and useful. The example used is too much contrivedand not real-life, the explanation is clear and direct. https://developer.ibm.com/tutorials/x-html5mobile3/ (opens in a new tab)

  2. tbc...