32
HTML5 Storage & Cache @yorzi [email protected]

HTML5 Storage/Cache

Embed Size (px)

DESCRIPTION

About HTML5 Storage Solutions and Application Cache.

Citation preview

Page 2: HTML5 Storage/Cache

General Introduction

Page 3: HTML5 Storage/Cache

Background

World Wide Web Consortium (W3C) +

Web Hypertext Application Technology Working Group (WHATWG)

Page 4: HTML5 Storage/Cache

History

● 1991 HTML● 1994 HTML 2● 1996 CSS 1 + JavaScript● 1997 HTML 4● 1998 CSS 2● 2000 XHTML 1● 2002 Tableless Web Design● 2005 AJAX● 2009 HTML5 ~= HTML + CSS + JS

Page 5: HTML5 Storage/Cache

Design Rules

● Based on HTML, CSS, DOM, and JS● Reduce the need for external plugins (

Flash)● Better error handling● More markup to replace scripting● HTML5 should be device independent● The development process should be

visible to the public

Page 6: HTML5 Storage/Cache

Minimum Sample<!DOCTYPE html><html>

<head><meta charset="UTF-8"><title>Title of the document</title>

</head>

<body>Content of the document......

</body>

</html>

Page 7: HTML5 Storage/Cache

Browser Compatiblity

Page 8: HTML5 Storage/Cache

Features

Page 9: HTML5 Storage/Cache

Storage & Cache

Page 10: HTML5 Storage/Cache

Before HTML5

● Cookies● Flash Storage● Internet Explorer UserData● Google Gears● window.name ( hack )

Page 11: HTML5 Storage/Cache

Storage In HTML5

● Web Storage APIs: localStorage / sessionStorage● Web SQL Database● IndexedDB● Application Cache● ...Cookies

Page 12: HTML5 Storage/Cache

window.localStorage

● Key/value hash table● Persistent on page reloads● Avoids cookies’ limitations● Scoped to an origin

http://example.com:80/ \ \ \_ port \ \_ domain \_ scheme

Page 13: HTML5 Storage/Cache

localStorage APIlocalStorage.setItem('someKey', someValue);localStorage.getItem('someKey'); // value

// Can also access as a property, but not recommended.localStorage.someKey = someValue;

// Orther sampleslocalStorage.setItem('dateOfBirth', '1984-07-22');localStorage.getItem('dateOfBirth'); // '1984-07-22'localStorage.length // num of items storedlocalStorage.key(0); // 'dateOfBirth'localStorage.removeItem('dateOfBirth');localStorage.clear();

Page 14: HTML5 Storage/Cache

window.sessionStorage

Same as localStorage but...● Lasts as long as browser is open● Opening page in new window or tab

starts new session

Page 15: HTML5 Storage/Cache

Web Storage APIs Limitation

Web Storage APIs only store strings!

Solution:

localStorage.setItem('user', JSON.stringify({user: 'john', id: 10}));

var user = JSON.parse(localStorage.getItem('user'));

Page 16: HTML5 Storage/Cache

Web SQL Database

● Client-side SQL database● Asynchronous & Synchronous* API● Basically wrapper around SQLite

Page 17: HTML5 Storage/Cache

window.openDatabase();var db = window.openDatabase( 'MyDB', // dbName '1.0', // version 'test database', // description 2 * 1024 * 1024, // estimatedSize in bytes function(db) {} // optional creationCallback);

Page 18: HTML5 Storage/Cache

window.executeSql();

Page 19: HTML5 Storage/Cache

Transaction

Page 20: HTML5 Storage/Cache

Deprecated

Page 21: HTML5 Storage/Cache

Indexed Database

● Object based data store● In-order retrieval by index or locate by

key● Asynchronous & Synchronous API

Page 22: HTML5 Storage/Cache

Object Storevar db = window.indexedDB.open('IntrideaDB', 1);

if (db.version != '1') { // User's first visit, initialize database. db.createObjectStore('Friends', // name of new object store 'id', // key path true); // auto increment? db.setVersion('1');} else { // DB already initialized.}

Page 23: HTML5 Storage/Cache

Put into Storevar store = db.openObjectStore('Friends');

var user = store.put({name: 'Eric', gender: 'male', likes: 'html5'});

console.log(user.id); // Expect 1

Page 24: HTML5 Storage/Cache

Retrieving by key ( indexes )db.createIndex(indexName, keyPath, unique?);

// db.createObjectStore("Friend", "id", true);db.createIndex("FriendNames", "name", false);var index = db.openIndex('FriendNames');var id = index.get('Eric');

Page 25: HTML5 Storage/Cache

Querying ( cursors )db.createIndex(indexName, keyPath, unique?);

// db.createObjectStore("Friend", "id", true);db.createIndex("FriendNames", "name", false);var index = db.openIndex('FriendNames');var id = index.get('Eric');

Page 26: HTML5 Storage/Cache

Web SQL DB VS. Indexed DB

Page 27: HTML5 Storage/Cache

Application Cache ( Why? )

* HTML, CSS, and JS stay fairly consistent* Native browser caching is unreliable* Caching resources creates speedier apps!* Decent mobile support

Page 28: HTML5 Storage/Cache

Menifest File

Page 29: HTML5 Storage/Cache

JS API

Page 30: HTML5 Storage/Cache

Detecting Support

if (Modernizr.localstorage) { ... }if (Modernizr.sessionstorage) { ... }if (Modernizr.websqldatabase) { ... }if (Modernizr.indexeddb) { ... }if (Modernizr.applicationcache) { ... }

Page 32: HTML5 Storage/Cache

Thanks!