HTML5 localStorage size limit for subdomains
Asked Answered
S

5

81

HTML5's localStorage databases are usually size-limited — standard sizes are 5 or 10 MB per domain. Can these limits be circumvented by subdomains (e.g. example.com, hack1.example.com and hack2.example.com all have their own 5 MB databases)? And is there anything in the standard that specifies whether parent domains can access their children's databases? I can't find anything, and I can see arguments for doing it either way, but it seems like there has to be some standard model.

Silvey answered 30/4, 2010 at 19:55 Comment(5)
I'm working right now with a program where we are trying to completely store all text in localStorage. It would be awesome if you could add some links to where you found this information about the current 5MB limit. It would help me understand the alternatives better. ThanksEmilia
Webkit-based browsers use UTF-16 for storage which haves it to 2.5MB limit.Hibernicism
Note, the June 2011 RFC says that "User agents should guard against sites storing data under the origins other affiliated sites, e.g. storing up to the limit in a1.example.com, a2.example.com, a3.example.com, etc, circumventing the main example.com storage limit." So don't count on that hack continuing to work in the future. ( dev.w3.org/html5/webstorage )Uzzi
The limit can be artificially "expanded" a great deal by using compression. Fast algorithms can be used safely such as mine: pieroxy.net/blog/pages/lz-string/index.htmlCryogen
There's some research done: computerworld.com/s/article/9237259/… However, the problem is, what with 2nd level top-domains such as com.de or org.pl?Unguarded
O
54

From http://dev.w3.org/html5/webstorage/#disk-space

A mostly arbitrary limit of five megabytes per origin is recommended. Implementation feedback is welcome and will be used to update this suggestion in the future.

It also mentions that :

User agents should guard against sites storing data under the origins other affiliated sites, e.g. storing up to the limit in a1.example.com, a2.example.com, a3.example.com, etc, circumventing the main example.com storage limit.

Openandshut answered 3/5, 2010 at 8:34 Comment(4)
This doesn't really answer the question about subdomains.Bifurcate
@JörnZaefferer, the intent of the spec is to prevent using subdomains.Antipodal
It's interesting to note that despite the warning in the spec, apparently only FireFox implemented the suggested prevention. See this project for a fun way to fill your disk/crash your browser: feross.org/fill-diskElliellicott
None of the answers are actually an answer to the question that said this one's the closest I suppose just have to read between the lines and follow the links etcNsf
D
16

Here's a pretty detailed test result with plenty of desktop and mobile browsers covered: http://dev-test.nemikor.com/web-storage/support-test/

Which confirms this bug report: http://code.google.com/p/chromium/issues/detail?id=58985#c15

You can rely on only 2.5MB, not 5MB, based on the string length that you can store.

Demoralize answered 17/1, 2012 at 13:9 Comment(0)
A
8

I missed this question when I asked "Is 5MB the de facto limit for W3C Web Storage?", but I got basically the same answer. If you want more information, I did link to some browser specific limits in my question.

Arioso answered 27/8, 2010 at 13:39 Comment(0)
A
7

A better solution is to use the [HTML5 IndexedDB for offline storage.]1

It looks like the replacement for the old Web SQL (which seems to be misnamed b/c it's for offline storage) is: Indexed DB, which allows offline storage and is still supportd:

IndexedDB is new in HTML5. Web Databases are hosted and persisted inside a user's browser. By allowing developers to create applications with rich query abilities it is envisioned that a new breed of web applications will emerge that have the ability to work online and off-line.

More info and a test-app at: http://ido-green.appspot.com/WebSQL-IndexedDB-example/jqm_indexedDB.html

Antipodal answered 23/5, 2012 at 13:52 Comment(3)
No IndexedDB support on mobile for now (might be upcoming in iOS 7). So might be better to create a persistence api wrapping WebSQL and IndexedDB until IndexedDB is better supported on mobile caniuse.com/#search=indexeddbSprouse
This should be a comment, since it doesn't answer the question.Unguarded
I actually think it's likely to provide the best possible advice for someone who asks the initial question. Separately, there exists a decent polyfill library that implements indexedDB on top of WebSQL for the older mobile browsers.Pandarus
H
2

To get 50MB of storage space use code below

// 1. paste this line in your code
!function(){function e(t,o){return n?void(n.transaction("s").objectStore("s").get(t).onsuccess=function(e){var t=e.target.result&&e.target.result.v||null;o(t)}):void setTimeout(function(){e(t,o)},100)}var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;if(!t)return void console.error("indexDB not supported");var n,o={k:"",v:""},r=t.open("d2",1);r.onsuccess=function(e){n=this.result},r.onerror=function(e){console.error("indexedDB request error"),console.log(e)},r.onupgradeneeded=function(e){n=null;var t=e.target.result.createObjectStore("s",{keyPath:"k"});t.transaction.oncomplete=function(e){n=e.target.db}},window.ldb={get:e,set:function(e,t){o.k=e,o.v=t,n.transaction("s","readwrite").objectStore("s").put(o)}}}();

// 2. Setting values
ldb.set('nameGoesHere', 'value goes here');

// 3. Getting values - callback is required because the data is being retrieved asynchronously:
ldb.get('nameGoesHere', function (value) {
  console.log('And the value is', value);
});

source https://github.com/DVLP/localStorageDB

Heptangular answered 27/5, 2016 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.