-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
fix(v2): fail-safe usage of browser storage (localStorage/sessionStorage) when access is denied #4501
Conversation
[V1] Built with commit 725dc88 |
[V2] Built with commit 725dc88 |
⚡️ Lighthouse report for the changes in this PR:
Lighthouse ran on https://deploy-preview-4501--docusaurus-2.netlify.app/ |
What about handling our own local storage API with a default fail-safe behavior? (ex with RN: https://github.com/slorber/react-native-storage-slot/blob/master/src/StorageSlot.ts) |
Thanks for the quick response! That makes sense to me. Shall we go with the exact same API as used there? i.e create a slot and then use it? |
I think that could be useful so that we have a uniform API to use everywhere yes. We could also have a hook-like "useStorage("key")" that would no-op if some config option is provided? maybe a bit overkill for now |
Puts all uses of localStorage behind an abstraction which doesn't fail when localStorage isn't available.
I wasn't sure where to put the localStorage wrapper, so chose @docusaurus/core as it seems to be the most widely shared dependency, but the failing e2e test shows it's failing to import in some cases:
@slorber Do you have any guidance on how I can fix this? |
Does anyone have any ideas? Perhaps @lex111 ? It looks like its just the yarn-v1 E2E test that's failing with this PR. And to be specific, its docusaurus-theme-common failing to import the new file from docusaurus-core. |
Will take a look at this! FYE simple steps to repro (and validate this is correctly fixed):
It seems your PR works: |
Updated your PR, moved to theme-common and cleaned a few things to align with the direction I'd like to set for storage usage in Docusaurus themes. Seems good to merge, still works in an iframe. |
Motivation
When you have 3rd party cookies blocked in chrome, in some cases (e.g. running in iframe), you can't access window.localStorage.
So I'm wrapping all such calls in a try-catch. It's already done at many callsites, so I'm reusing the same convention, by just
console.error(err)
.Without this, the entire page renders blank when some of these calls are attempted and fail.
Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
Tested loading the docsuaurus website in a cross-domain iframe.
Before: renders blank page.
After: Renders correctly, with new errors printed to console.