最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari localStorage not persisted between sessions - Stack Overflow

programmeradmin7浏览0评论

We have a web app that runs in Facebook (i.e. a running in an iFrame at a different domain). If a Safari user has Cookies and Website Data set to the default, "Allow from websites I visit", the data we store via localStorage.setItem is acting like sessionStorage, i.e. it's not available beyond the user's current session (i.e. after the user closes the tab). If we change the setting to "Always allow", it works fine just like in Chrome, IE , etc.

As a test, we've tried navigating the browser to our app's domain () directly and it works fine there. And also then it should truly be a visited website, but when going back to the game within Facebook, the problem still exists.

Note that the setItem call is succeeding, it's just that getItem doesn't return anything in a subsequent session. (So it's not like when the user is Private Browsing and the setItem call itself fails with a Quota Exceeded error.)

What do we need to do to support Safari so that our app, running within Facebook, can use localStorage as intended where the data will survive between sessions?

We have a web app that runs in Facebook (i.e. a running in an iFrame at a different domain). If a Safari user has Cookies and Website Data set to the default, "Allow from websites I visit", the data we store via localStorage.setItem is acting like sessionStorage, i.e. it's not available beyond the user's current session (i.e. after the user closes the tab). If we change the setting to "Always allow", it works fine just like in Chrome, IE , etc.

As a test, we've tried navigating the browser to our app's domain (https://ourappname.appspot.com) directly and it works fine there. And also then it should truly be a visited website, but when going back to the game within Facebook, the problem still exists.

Note that the setItem call is succeeding, it's just that getItem doesn't return anything in a subsequent session. (So it's not like when the user is Private Browsing and the setItem call itself fails with a Quota Exceeded error.)

What do we need to do to support Safari so that our app, running within Facebook, can use localStorage as intended where the data will survive between sessions?

Share Improve this question edited Jul 27, 2015 at 20:21 leontx asked Jul 10, 2015 at 23:46 leontxleontx 1,1951 gold badge14 silver badges24 bronze badges 2
  • Weird, I just tested in Safari 8.0.7 (10600.7.12) with "Allow from websites I visit". I set a localStorage key and then closed the browser (with ⌘+Q) and when I reopened the browser and used localStorage.getItem it was there. – daviddoran Commented Jul 14, 2015 at 7:08
  • @daviddoran thanks. Were you testing a site running in an iFrame that was a different domain than the top level site? I'll edit my question to make that more clear. – leontx Commented Jul 14, 2015 at 8:07
Add a comment  | 

2 Answers 2

Reset to default 10 +50

It's either a bug in Safari or a security feature.

You are visiting FaceBook and not your website. Your app is in iframe which would violate security model if it let you read any data from the browser. Think if a competitor site read data it did/didn't set. That'd constitute an information leak.

Safari is doing it's job well in that regard.

Ideally, in "Allow from websites I visit" mode, no browser should let iframes to set data to localStorage; even if every domain has their own storage-sandboxes.

What's troubling me is why are they even letting you write to localStorage from iframe at all (in your 'Allow for only sites I visit' mode)? That might actually be a bug - a information spoof attack enabling bug.

I think it's because security-exceptions were dropped from localStorage in case of not-same-party origin of request. So Safari might actually will not throw error but let it silently fail (in some cases). That's probably why your setItem call is succeeding.

At this point, with the given information, I suspect, sir you are out of luck due to Safari programmers following standard to the letter.

I'm still waiting on a reply from Apple, but it's safe to say we're stuck with this behavior. So Anubhav's answer is accurate, but we still needed a solution.

So as a work around, we created new endpoints on our server for persisting/restoring game state. We only utilize this for Safari, for all other browsers we're still persisting our game state in localStorage.

There is a slight performance penalty for the user. And a slight server cost. Not a sexy solution, but now our Facebook canvas app supports Safari.

发布评论

评论列表(0)

  1. 暂无评论