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(utils): subscribe method of createJSONStorage should only run in the client #2585

Merged
merged 12 commits into from
Jun 3, 2024

Conversation

mhsattarian
Copy link
Contributor

Related Issues or Discussions

Fixes #2581

Summary

Fixes some issues created by previous PR (#2539) subscribe method of createJSONStorage should only run in the client

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented May 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 3, 2024 1:03pm

Copy link

codesandbox-ci bot commented May 29, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

github-actions bot commented May 29, 2024

LiveCodes Preview in LiveCodes

Latest commit: 982fd7c
Last updated: Jun 3, 2024 1:01pm (UTC)

Playground Link
React demo https://livecodes.io?x=id/FHKC76S5Z

See documentations for usage instructions.

@mhsattarian
Copy link
Contributor Author

@dai-shi Hi again.

this is the simplest fix I could think of. is it enough in your opinion?

should we add a try/catch block to ensure subscribe method won't fail?

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

Unfortunately, it's not what I expect. Let me try and see how it goes.

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

oh, this is tricky. what I expect doesn't work.

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

How does 6ca781f look?

I wanted to keep this: https://github.com/pmndrs/jotai/pull/2539/files#diff-4bb714dba559e7b7df1c7cb13113df5aefbafe023dac97e346bb39e8db159a1aL150-L152

@mhsattarian
Copy link
Contributor Author

Awesome. this looks better.

@mhsattarian
Copy link
Contributor Author

I should remove the test I added to check the subscriber not be called on server.

…rage to not be called in server"

This reverts commit 71d2fa0.
@mhsattarian
Copy link
Contributor Author

but now can we make sure such a case won't happen in the future?

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

but now can we make sure such a case won't happen in the future?

Ideally, there should be a test that throw on window.localStorage access. I'm not sure if we can write such a test.

@mhsattarian
Copy link
Contributor Author

can't we patch window.localStorage with a spy? just like what is done for window.addEventListener?

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

can we? Please give it a try!

@mhsattarian
Copy link
Contributor Author

honestly, I couldn't wrap my head around this problem as much as I wanted.

since we are already calling getStringStorage in the try block, checking localStorage access is not what we want. am I right? So I checked for access to getItem method instead.

the last commit seems eligible. what do you think @dai-shi?

@mhsattarian
Copy link
Contributor Author

I feel like I'm commit-polluting (if such a word exists) but in my defense, I should say these TS errors were not thrown in my local tests. anyways, sorry. :)

@dai-shi
Copy link
Member

dai-shi commented May 30, 2024

How about this?

    Object.defineProperty(window, 'localStorage', {
      get() {
         throw new Error('localStorage in not available.')
      },
    })

It's probably different from Node.js case, but if we removed try-catch, the test would fail.

@mhsattarian
Copy link
Contributor Author

same behavior with less code. I'm in.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments below.

src/vanilla/utils/atomWithStorage.ts Outdated Show resolved Hide resolved
Comment on lines 375 to 379
Object.defineProperty(window, 'localStorage', {
get() {
return localStorage
},
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really restore, but maybe it's fine.

Copy link
Contributor Author

@mhsattarian mhsattarian May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. I removed this part of the code and nothing changed. doesn't it affect using the localStorage in other tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may. the best is to leave it for now.

expect(storage.subscribe).toBeUndefined()
})

it('createJSONStorage with localStorage', async () => {
expect(() => createJSONStorage(() => window.localStorage)).not.toThrow()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we should check is not only createJSONStorage, but also atomWithStorage.
You want to confirm if the test works as expected by removing the try-catch we added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with the try/catch removed and it did throw an error:

CleanShot 2024-05-30 at 15 17 44@2x

should we add a new test or modify this one in you opinion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. that's my oversight. yeah, the current test seems good. no need to add new one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I think what we need to test, if possible, is createJSONStorage(). Do you have any idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean instead of createJSONStorage(() => window.localStorage) we should test createJSONStorage()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if possible, we want to test both. Otherwise, it's fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simply added another expect to check for it, but I have a feeling you mean something else.
there is also a console.warn happening here due to the checking we have in createJSONStorage. is it ok?

here is a screenshot:

CleanShot 2024-06-02 at 13 06 43@2x

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

except(() => createJSONStorage()).not.toThrow() seems good. (It fails if we remove the fix, right?)

As for the console.warn, let's mock it for this test and hide the warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails if we remove the fix, right?

Yes

Also, I added console.warn patching in the last commit, please check it.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks for your contribution!

@mhsattarian
Copy link
Contributor Author

I'm glad I could be of any help. 🙏

@dai-shi dai-shi added this to the v2.8.3 milestone Jun 3, 2024
@dai-shi dai-shi changed the title Fix #2581: subscribe method of createJSONStorage should only run in the client fix(utils): subscribe method of createJSONStorage should only run in the client Jun 3, 2024
@dai-shi dai-shi merged commit 48df663 into pmndrs:main Jun 3, 2024
37 checks passed
dmaskasky pushed a commit to dmaskasky/jotai that referenced this pull request Jun 7, 2024
…the client (pmndrs#2585)

* fix: prevent calling storage subscribe unless in client

* test: add test for getStringStorage method from createJSONStorage to not be called in server

* use try-catch

* Revert "test: add test for getStringStorage method from createJSONStorage to not be called in server"

This reverts commit 71d2fa0.

* test: checking localStorage/sessionStorage access with createJSONStorage in server

* test: improve checking localStorage/sessionStorage access with createJSONStorage in server

* test: use defineProperty for checking localStorage/sessionStorage access with createJSONStorage in server

* fix: ts error in new test

* improve detecting browser storage access in client

* Update src/vanilla/utils/atomWithStorage.ts

* patch console.warn for atomWithStorage (in non-browser environment) test

---------

Co-authored-by: daishi <daishi@axlight.com>
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

createJSONStorage: localStorage is not defined
2 participants