Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using session-storage across subdomains

I have two domains domain1.site.com and domain2.site.com, i have set session-storage in domain1.site.com and then not able to get session-storage on other domain domain2.site.com from same tab. Is there any other way which can be used identify a tab when navigated across sub-domains?

like image 318
binfull Avatar asked Jul 31 '14 16:07

binfull


People also ask

Does session storage persist across 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).

Is session storage available across tabs?

Right, sessionStorage is not shared across tabs. The way I solved it is by using localStorage events. When a user opens a new tab, we first ask any other tab that is opened if he already have the sessionStorage for us.


1 Answers

The question is a bit older, but maybe the following helps others.

There is a solution called "Cross Domain Local Storage", it works with a combination of localStorage and postMessage. With that you have a storage you can use with any domain. There are two projects that I know:

  • https://github.com/ofirdagan/cross-domain-local-storage
  • https://github.com/zendesk/cross-storage.

But that's not a session storage. To separate multiple tabs, you have to generate a unique id (maybe tab creation date in milliseconds) and save that into the session storage, but only the first time, when there is no value already in the session storage. That way you have one id per tab. That id you can use to put data into the cross domain local storage.

like image 82
RiZKiT Avatar answered Oct 05 '22 23:10

RiZKiT