Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cross-browser storage for all use cases, used across the web.

License

NotificationsYou must be signed in to change notification settings

marcuswestin/store.js

Repository files navigation

Cross-browser storage for all use cases, used across the web.

Circle CInpm versionnpm

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.

  1. Basic Usage
  2. Supported Browsers
  3. Plugins
  4. Builds
  5. Storages

Basic Usage

All you need to know to get started:

API

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)})

Installation

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>

Supported Browsers

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.

List of supported browsers

Plugins

Plugins provide additional common functionality that some users might need:

List of all Plugins

Using Plugins

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.

Write your own plugin

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.

Builds

Choose which build is right for you!

List of default builds

Make your own Build

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

Storages

Store.js will pick the best available storage, and automatically falls back to the first available storage that works:

List of all Storages

Storages limits

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:

StorageTargetsRecommendationsMore info
allAll browsersStore < 1 million characters(Except Safari Private mode)
allAll & Private modeStore < 32 thousand characters(Including Safari Private mode)
localStorageModern browsersMax 2mb (~1M chars)limits,android
sessionStorageModern browsersMax 5mb (~2M chars)limits
cookieStorageSafari Private modeMax 4kb (~2K chars)limits
userDataStorageIE5, IE6 & IE7Max 64kb (~32K chars)limits
globalStorageFirefox 2-5Max 5mb (~2M chars)limits
memoryStorageAll browsers, fallbackDoes not persist across pages!

Write your own Storage

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)

[8]ページ先頭

©2009-2025 Movatter.jp