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

fix(v2): fail-safe usage of browser storage (localStorage/sessionStorage) when access is denied #4501

Merged
merged 2 commits into from
Apr 13, 2021

Conversation

jknoxville
Copy link
Contributor

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.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 23, 2021
@netlify
Copy link

netlify bot commented Mar 23, 2021

[V1]

Built with commit 725dc88

https://deploy-preview-4501--docusaurus-1.netlify.app

@netlify
Copy link

netlify bot commented Mar 23, 2021

[V2]

Built with commit 725dc88

https://deploy-preview-4501--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Mar 23, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 66
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4501--docusaurus-2.netlify.app/

@slorber
Copy link
Collaborator

slorber commented Mar 23, 2021

What about handling our own local storage API with a default fail-safe behavior?
That could make the code simpler?

(ex with RN: https://github.com/slorber/react-native-storage-slot/blob/master/src/StorageSlot.ts)

@jknoxville
Copy link
Contributor Author

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?

@slorber
Copy link
Collaborator

slorber commented Mar 23, 2021

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.
@jknoxville
Copy link
Contributor Author

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:

Module not found: Can't resolve '@docusaurus/core/src/localStorage' in '/home/runner/work/docusaurus/docusaurus/test-website/node_modules/@docusaurus/theme-common/lib/utils/docsPreferredVersion'

@slorber Do you have any guidance on how I can fix this?

@jknoxville
Copy link
Contributor Author

Does anyone have any ideas? Perhaps @lex111 ?
As I understand it, this makes all uses of Docusaurus fail to render inside an iframe for users blocking third-party cookies, which is now the default in some browsers, and soon will be in chrome too, so I'm very eager to get it fixed.

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.

@slorber
Copy link
Collaborator

slorber commented Apr 9, 2021

Will take a look at this!

FYE simple steps to repro (and validate this is correctly fixed):

It seems your PR works:

image

@slorber
Copy link
Collaborator

slorber commented Apr 13, 2021

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.

@slorber slorber changed the title fix: Wrap localStorage calls in try catch fix(v2): Wrap localStorage calls in try catch Apr 13, 2021
@slorber slorber changed the title fix(v2): Wrap localStorage calls in try catch fix(v2): fail-safe usage of browser storage (localStorage/sessionStorage) when access is denied Apr 13, 2021
@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Apr 13, 2021
@slorber slorber merged commit 2c57f44 into facebook:master Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants