- Notifications
You must be signed in to change notification settings - Fork1.3k
Cross-browser storage for all use cases, used across the web.
License
marcuswestin/store.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Cross-browser storage for all use cases, used across the web.
Store.js has been around since 2010 (first commit,v1 release). It is used in production on tens of thousands of websites, such as cnn.com, dailymotion.com, & many more.
Store.js provides basic key/value storage functionality (get/set/remove/each
) as well as a rich set of plug-instorages and extrafunctionality.
- Basic Usage
- All you need to get started
- API
- Installation
- Supported Browsers
- All of them, pretty much :)
- List of supported browsers
- Plugins
- Additional common functionality
- List of all Plugins
- Using Plugins
- Write your own Plugin
- Builds
- Choose which build is right for you
- List of default Builds
- Make your own Build
- Storages
- Storages provide underlying persistence
- List of all Storages
- Storages limits
- Write your own Storage
All you need to know to get started:
store.js exposes a simple API for cross-browser local storage:
// Store current userstore.set('user',{name:'Marcus'})// Get current userstore.get('user')// Remove current userstore.remove('user')// Clear all keysstore.clearAll()// Loop over all stored valuesstore.each(function(value,key){console.log(key,'==',value)})
Using npm:
npm i store
// Example store.js usage with npmvarstore=require('store')store.set('user',{name:'Marcus'})store.get('user').name=='Marcus'
Using script tag (first download one of thebuilds):
<!-- Example store.js usage with script tag --><scriptsrc="path/to/my/store.legacy.min.js"></script><script>store.set('user',{name:'Marcus'})store.get('user').name=='Marcus'</script>
All of them, pretty much :)
To support all browsers (including IE 6, IE 7, Firefox 4, etc.), userequire('store')
(alias forrequire('store/dist/store.legacy')
) orstore.legacy.min.js.
To save some kilobytes but still support all modern browsers, userequire('store/dist/store.modern')
orstore.modern.min.js instead.
- Tested on IE6+
- Tested on iOS 8+
- Tested on Android 4+
- Tested on Firefox 4+
- Tested on Chrome 27+
- Tested on Safari 5+
- Tested on Opera 11+
- Tested on Node (withhttps://github.com/coolaj86/node-localStorage)
Plugins provide additional common functionality that some users might need:
- all.js: All the plugins in one handy place.
- defaults.js: Declare default values.Example usage
- dump.js: Dump all stored values.Example usage
- events.js: Get notified when stored values change.Example usage
- expire.js: Expire stored values at a given time.Example usage
- observe.js: Observe stored values and their changes.Example usage
- operations.js: Useful operations like push, shift & assign.Example usage
- update.js: Update a stored object, or create it if null.Example usage
- v1-backcompat.js: Full backwards compatibility with store.js v1.Example usage
With npm:
// Example plugin usage:varexpirePlugin=require('store/plugins/expire')store.addPlugin(expirePlugin)
If you're using script tags, you can either usestore.everything.min.js (whichhas all plugins built-in), or clone this repo to add or modify a build and runmake build
.
A store.js plugin is a function that returns an object that gets added to the store.If any of the plugin functions overrides existing functions, the plugin function can still callthe original function using the first argument (super_fn).
// Example plugin that stores a version history of every valuevarversionHistoryPlugin=function(){varhistoryStore=this.namespace('history')return{set:function(super_fn,key,value){varhistory=historyStore.get(key)||[]history.push(value)historyStore.set(key,history)returnsuper_fn()},getHistory:function(key){returnhistoryStore.get(key)}}}store.addPlugin(versionHistoryPlugin)store.set('foo','bar 1')store.set('foo','bar 2')store.getHistory('foo')==['bar 1','bar 2']
Let me know if you need more info on writing plugins. For the moment I recommendtaking a look at thecurrent plugins. Good example plugins areplugins/defaults,plugins/expire andplugins/events.
Choose which build is right for you!
- store.everything.min.js: All the plugins, all the storages.Source
- store.legacy.min.js: Full support for all tested browsers. Add plugins separately.Source
- store.modern.min.js: Full support for all modern browsers. Add plugins separately.Source
- store.v1-backcompat.min.js: Full backwards compatibility withstore.js v1.Source
If you're using npm you can create your own build:
// Example custom build usage:varengine=require('store/src/store-engine')varstorages=[require('store/storages/localStorage'),require('store/storages/cookieStorage')]varplugins=[require('store/plugins/defaults'),require('store/plugins/expire')]varstore=engine.createStore(storages,plugins)store.set('foo','bar',newDate().getTime()+3000)// Using expire plugin to expire in 3 seconds
Store.js will pick the best available storage, and automatically falls back to the first available storage that works:
- all.js All the storages in one handy place.
- localStorage.js Store values in localStorage. Great for all modern browsers.
- sessionStorage.js Store values in sessionStorage.
- cookieStorage.js Store values in cookies. Useful for Safari Private mode.
- memoryStorage.js Store values in memory. Great fallback to ensure store functionality at all times.
- oldFF-globalStorage.js Store values in globalStorage. Only useful for legacy Firefox 3+.
- oldIE-userDataStorage.js Store values in userData. Only useful for legacy IE 6+.
Each storage has different limits, restrictions and overflow behavior on different browser. For example, Android has has a 4.57M localStorage limit in 4.0, a 2.49M limit in 4.1, and a 4.98M limit in 4.2... Yeah.
To simplify things we provide these recommendations to ensure cross browser behavior:
Storage | Targets | Recommendations | More info |
---|---|---|---|
all | All browsers | Store < 1 million characters | (Except Safari Private mode) |
all | All & Private mode | Store < 32 thousand characters | (Including Safari Private mode) |
localStorage | Modern browsers | Max 2mb (~1M chars) | limits,android |
sessionStorage | Modern browsers | Max 5mb (~2M chars) | limits |
cookieStorage | Safari Private mode | Max 4kb (~2K chars) | limits |
userDataStorage | IE5, IE6 & IE7 | Max 64kb (~32K chars) | limits |
globalStorage | Firefox 2-5 | Max 5mb (~2M chars) | limits |
memoryStorage | All browsers, fallback | Does not persist across pages! |
Chances are you won't ever need another storage. But if you do...
Seestorages/ for examples. Two good examples arememoryStorage andlocalStorage.
Basically, you just need an object that looks like this:
// Example custom storagevarstorage={name:'myStorage',read:function(key){ ...},write:function(key,value){ ...},each:function(fn){ ...},remove:function(key){ ...},clearAll:function(){ ...}}varstore=require('store').createStore(storage)
About
Cross-browser storage for all use cases, used across the web.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.