Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offline webapp. How to store data?

Introduction:

I want to develop a webapp to manage sports competitions. It must be able to run completely offline, store data locally and post the results online via AJAX whenever there is an internet connection available - this may be the day after.

Question:

How to store data using Javascript?

Aditional notes:

  • I don't want to use any server-side technology.
  • It must be secure like a database. I've read about cookies and html5 storage but none of them sound convincing.
like image 347
Hertzu Avatar asked Apr 15 '13 03:04

Hertzu


People also ask

How can I save website data offline?

The simplest one is Web Storage - a key-value string storage, allowing Web applications to store data either persistently and cross-window ( localStorage ) or for a single session in a single browser tab ( sessionStorage ).

Can PWA store data?

If the user has installed an icon for the PWA in the operating system, the browser may grant persistent storage.

How do you save data in PWA?

For PWAs, you could cache the static files composing your application shell (JS/CSS/HTML files) in the Cache API and fill in the offline page data from IndexedDB. There are no hard and fast rules around this however.

Can you use a web app offline?

Progressive Web Apps (PWAs) are web applications that use modern technologies to provide a user experience that closely mimics a native app. PWAs have become very popular in recent years as they do not only improve performance and user experience but are also accessible when a user is offline.


1 Answers

If you are supporting modern browsers, you can make use of HTML5 Local Storage.

Persistent local storage is one of the areas where native client applications have held an advantage over web applications. For native applications, the operating system typically provides an abstraction layer for storing and retrieving application-specific data like preferences or runtime state. These values may be stored in the registry, INI files, XML files, or some other place according to platform convention. If your native client application needs local storage beyond key/value pairs, you can embed your own database, invent your own file format, or any number of other solutions.

Example

// Save data to a the current session's store
sessionStorage.setItem("username", "John");

// Access some stored data
alert( "username = " + sessionStorage.getItem("username"));

// Get the text field that we're going to track
var field = document.getElementById("field");

// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if ( sessionStorage.getItem("autosave")) {
   // Restore the contents of the text field
   field.value = sessionStorage.getItem("autosave");
}

// Check the contents of the text field every second
setInterval(function(){
   // And save the results into the session storage object
   sessionStorage.setItem("autosave", field.value);
}, 1000);

Browser Compatibility


Older Browsers

Use Polyfill.

like image 118
Praveen Kumar Purushothaman Avatar answered Oct 17 '22 09:10

Praveen Kumar Purushothaman