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

createJSONStorage: localStorage is not defined #2581

Closed
rtritto opened this issue May 28, 2024 · 14 comments · Fixed by #2585
Closed

createJSONStorage: localStorage is not defined #2581

rtritto opened this issue May 28, 2024 · 14 comments · Fixed by #2585
Labels
bug Something isn't working

Comments

@rtritto
Copy link
Contributor

rtritto commented May 28, 2024

Summary

ReferenceError: localStorage is not defined

  1 | import { atomWithStorage, createJSONStorage } from 'jotai/utils'
  2 |
> 3 | const storage = createJSONStorage(() => localStorage)
    |                                         ^
  4 |
  5 | const darkModeState = atomWithStorage('darkMode', true, storage)

Details

Next.js: 14.2.3
jotai: 2.8.2

It correctly works with jotai <= 2.8.1.

Issue probably generated by #2539

Similar issue #949

@rtritto rtritto changed the title atomWithStorage: localStorage is not defined createJSONStorage: localStorage is not defined May 28, 2024
@fernandojbf
Copy link

Having the same issue for sessionStorage.

I believe 2.8.2 start calling the storage fn on server side.

@dai-shi
Copy link
Member

dai-shi commented May 29, 2024

Ohhhh, that was it. #2539 (comment)
Thanks for reporting.

#2539 broke it.
@mhsattarian Would you like to fix?

@mhsattarian
Copy link
Contributor

@dai-shi on it.

@dai-shi
Copy link
Member

dai-shi commented May 30, 2024

Can any of you confirm if #2585 fixes your case?
https://ci.codesandbox.io/status/pmndrs/jotai/pr/2585
☝️ Find "Local Install Instructions"

@rtritto
Copy link
Contributor Author

rtritto commented May 30, 2024

@dai-shi it works, thanks!

PS: I'm using yarn v4: this version should be added in the second step of "Local Install Instructions" of CodeSandbox CI

@dai-shi
Copy link
Member

dai-shi commented May 30, 2024

Thanks for confirming!

(Yeah, but it's out of my control.)

@omridevk
Copy link

@dai-shi
Checking with version 2.8.4
I am still getting:

15:18:41 [vite] Error when evaluating SSR module /src/components/wizard/wizard.tsx:
|- ReferenceError: sessionStorage is not defined

my code:

const storage = createJSONStorage<WizardAtom>(() => sessionStorage)

@omridevk
Copy link

Also tested with the Local Instructions:
npm i https://pkg.csb.dev/pmndrs/jotai/commit/982fd7c7/jotai
same error

@omridevk
Copy link

it is hard for me create a reproduction as I am using vite + fastify to create an SSR output

@dai-shi
Copy link
Member

dai-shi commented Jun 28, 2024

Does it work with v2.8.1? Otherwise, it's probably a different thing.

@omridevk
Copy link

omridevk commented Jun 28, 2024

Nope, same issue. as in I can reproduce it in v2.8.1

@omridevk
Copy link

15:52:21 [vite] Error when evaluating SSR module /src/atoms/wizard-data-atom.ts:
|- ReferenceError: localStorage is not defined
    at eval (/src/atoms/wizard-data-atom.ts:7:63)
    at Object.getItem (/node_modules/jotai/esm/vanilla/utils.mjs:372:31)
    at Module.atomWithStorage (/node_modules/jotai/esm/vanilla/utils.mjs:419:25)
    at eval (/src/atoms/wizard-data-atom.ts:8:46)
    at async instantiateModule (/vite/dist/node/chunks/dep-C1-ZB6nQ.js:53451:5)

@dai-shi
Copy link
Member

dai-shi commented Jun 28, 2024

It correctly works with jotai <= 2.8.1.

In #2581, we discussed and solved the issue specific with v2.8.2.
If v2.8.1 doesn't solve your case, please open a new bug report 👉 https://github.com/pmndrs/jotai/discussions/new?category=bug-report
Or, a question https://github.com/pmndrs/jotai/discussions/new?category=q-a if you don't have a reproduction.

@omridevk
Copy link

Thanks @dai-shi for the quick response.
#2639
And I will open an issue once I can reproduce it in stackblitz/codesandbox or a repo.
Really love this lib by the. way, appreciate all your hard work sensei

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
5 participants