square/svelte-store

persisted store results in ReferenceError: window is not defined

ak4zh opened this issue · 8 comments

ak4zh commented
persisted store results in ReferenceError: window is not defined

Any sample code? Any environment information? This is hard to debug otherwise, as I don't see this problem.

Please also include which version of the package you are using from your lock file.

/node_modules/@square/svelte-store/lib/persisted/storage-utils.js:7
    return window.localStorage.getItem(key);
    ^

ReferenceError: window is not defined
    at getLocalStorageItem (/node_modules/@square/svelte-store/lib/persisted/storage-utils.js:7:5)
    at eval (/node_modules/@square/svelte-store/lib/persisted/index.js:75:29)
    at Generator.next (<anonymous>)
    at fulfilled (/node_modules/@square/svelte-store/lib/persisted/index.js:6:58)

Node.js v19.8.1

This is only fixed when I turn ssr off at the root .ts file

Anything rendered server side is not going to have access to the window object and thus persisted stores won't work.

https://learn.svelte.dev/tutorial/ssr

Closing for now. Feel free to reopen with more details if this is not an issue of SSR not having access to the window object.

I think this should be reopened, as the persisted store example doesnt run in sveltekit out of the box. Any client side storage of course wont run in a project enabled for SSR but what i would expect is for persisted to do the checking and waiting internally

so what is the recommended way of implementing client-side persisted store on a ssr-capable app?