Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback if localStorage is not available. #41

Closed
gyurielf opened this issue May 4, 2023 · 5 comments · Fixed by #42
Closed

Fallback if localStorage is not available. #41

gyurielf opened this issue May 4, 2023 · 5 comments · Fixed by #42
Labels
bug Something isn't working

Comments

@gyurielf
Copy link

gyurielf commented May 4, 2023

Hello there!

I met a quite edge case issue while using the package.

In chrome, if you have an embed iframe on a site (which uses this package), and you open it in incognito mode it will be throws an error.
Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
I walked around the issue, so it's a chrome specific issue which is exists on both mobile and desktop (tested on win, ubuntu, android),

I tried to handle on the the lacking of localStorage in a +page.ts load and throwing the user to a meaningful error page, but files are loaded by the browser when the document load starts and it's try to "connect" to the localStorage instantly and unfortunately it's kill the app before it can be handled by a svelte-kit load function.

I thought about a fallback option or something like that. I mean if it can't read the localStorage, drop a meaningful error but make a regular store regardless to prevent app breaks.

The title probably not too precise, so modify freely.

Best regards.

@gyurielf
Copy link
Author

gyurielf commented May 4, 2023

I look into the code and as I see you did something like that.

export function createLocalStorage<T>(listenExternalChanges = false): StorageInterface<T> {
if (typeof window !== "undefined" && window?.localStorage) {
return getBrowserStorage(window.localStorage, listenExternalChanges)
}
warnStorageNotFound("window.localStorage")
return createNoopStorage()
}

Maybe it's a svelte-kit issue then ?

@MacFJA MacFJA added the bug Something isn't working label May 4, 2023
@MacFJA
Copy link
Owner

MacFJA commented May 4, 2023

I never encountered this case before.
I will try to handle it like when the localStorage doesn't exist


Maybe it's a svelte-kit issue then ?

It's not an issue with SvelteKit, the issue came from the iframe being sandboxed by default.
You can use localStorage in an iframe, but you have to allow it before (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#sandbox)

@gyurielf
Copy link
Author

gyurielf commented May 4, 2023

Well, shame on me, but i forgot to mention that this issue occurs only on incognito mode, otherwise working perfectly.

@MacFJA MacFJA linked a pull request May 4, 2023 that will close this issue
@MacFJA MacFJA closed this as completed in #42 May 4, 2023
@MacFJA
Copy link
Owner

MacFJA commented May 4, 2023

I just push the version 2.3.0 that should fix this kind of errors

@gyurielf
Copy link
Author

gyurielf commented May 4, 2023

I've tested it, working like a charm. Thank you for the super fast reply and fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants