Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can HTML5 databases and localStorage be shared across subdomains?

I am attempting to share data across subdomains using Safari. I would like to use an HTML5 database (specifically localStorage as my data is nothing but key-value pairs). However, it seems as though data stored to domain.com can not be accessed from sub.domain.com (or vice versa). Is there any way to share a single database in this situation?

like image 362
Sebastian Celis Avatar asked Nov 14 '10 14:11

Sebastian Celis


People also ask

Can local storage be shared between subdomains?

Way to Solution That's because localstorage doesn't support sharing the storage across subdomains or even domain. Thus, if you have something stored at a.example.com it won't be accessible from example.com or b.example.com.

Can local storage be shared between domains?

Solution: Use an iframe to save the data in localStorage, and then the other domains ask the iframe for what we already saved. 4- in whatever other domain we ask for the data from the iframe (using postMessage and listening for a response).

Does iframe share local storage?

According to the W3C: Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data. In other words, if the iframes all reside in the same domain, then they will share the same local storage instance.

What are the localStorage functions in HTML5?

The local storage is a type of HTML5 offline storage that allows user string data to be saved synchronously in their browser. Information is kept in name and value pairs and not available between different browsers on the same device. Local storage can be used as an alternative to cookies.


2 Answers

Update 2016

This library from Zendesk worked for me.

Sample:

Hub

// Config s.t. subdomains can get, but only the root domain can set and del CrossStorageHub.init([   {origin: /\.example.com$/,            allow: ['get']},   {origin: /:\/\/(www\.)?example.com$/, allow: ['get', 'set', 'del']} ]); 

Note the $ for matching the end of the string. The regular expression in the above example will match origins such as valid.example.com, but not invalid.example.com.malicious.com.

Client

var storage = new CrossStorageClient('https://store.example.com/hub.html');  storage.onConnect().then(function() {   return storage.set('newKey', 'foobar'); }).then(function() {   return storage.get('existingKey', 'newKey'); }).then(function(res) {   console.log(res.length); // 2 }).catch(function(err) {   // Handle error }); 

Check https://stackoverflow.com/a/39788742/5064633

like image 57
super1ha1 Avatar answered Sep 28 '22 08:09

super1ha1


There is simple way to use cross-domain anything, just create simple page that will be included as proxy iframe hosted on domain you try to access, send PostMessage to that iframe and inside iframe you do your LocalStorage database manipulation. Here is a link to article that do this with lcoalStorage. And here is demo that send message to different page in subdomain check the source code, it use iframe and PostMessage.

EDIT: New version of sysend.js library (used by above demo) use BroadcastChannel if browser support it, but still it require Iframe. Recent version also simplify using of Cross-Origin messages, you have html of the iframe in repo, that you can use (or you can use simple html file with single script tag with the lib) and in parent you just need to call one function sysend.proxy('https://example.com'); where example.com need to have proxy.html file (you can also use your own filename and different path).

like image 34
jcubic Avatar answered Sep 28 '22 07:09

jcubic