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

persisted store results in ReferenceError: window is not defined #53

Closed
ak4zh opened this issue Mar 6, 2023 · 8 comments
Closed

persisted store results in ReferenceError: window is not defined #53

ak4zh opened this issue Mar 6, 2023 · 8 comments

Comments

@ak4zh
Copy link

ak4zh commented Mar 6, 2023

No description provided.

@sureshjoshi
Copy link

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

@Akolyte01
Copy link
Collaborator

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

@ekrata-main
Copy link

/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

@Akolyte01
Copy link
Collaborator

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

@Akolyte01
Copy link
Collaborator

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.

@mattpilott
Copy link

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

@based64-eth
Copy link

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

@squeedee
Copy link

squeedee commented May 4, 2024

This works for me:

// src/lib/stores.ts

const createStarredWorkloads = () => {
    const {subscribe, update} = persisted(
        {},
        'STARRED',
        {storageType: "LOCAL_STORAGE"}
    )
    return {
        subscribe,
        update,
        star: key => update((a) => {
            if (a[key]) {
                a[key] = false
            } else {
                a[key] = true
            }
            return a
        }),
    }
}

export const starredWorkloads = browser ? createStarredWorkloads() : readable();

Seems like as soon as we hit the client, the starredWorkloads store is resubbed? It might be the side effect of me doing other dynamic loading, so not sure if YMMV.

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

No branches or pull requests

7 participants