How can I fix "sessionStorage is not defined" in Svelte?
Asked Answered
G

3

11

I'm new in Svelte and I'm trying to set some info into sessionStorage but it is throwing "sessionStorage is not defined". I realised that I received this error because it's running on the server side.

I created a component at /src/components/nav.svelte that uses /src/domain/auth/service.js and the error occurs in the last one.

Searching on the web I found that in this case I must use sessionStorage inside onMount function. Is that the right way?

How can I avoid that my code get a little mess?

Gentes answered 11/6, 2019 at 22:17 Comment(3)
what data you are trying to store? are you going to use the data in both server side and client side?Charcoal
You indeed found the correct answer. You would use sessionStorage inside the onMount function as this will only run when your attaching the component on the client. The cost inside onMount will specifically NOT run on the server so that you are able to lazily load data on the client side.Ryder
I put code which contains sessionStorage into functions and it stopped complains. But now it shows me the following warning: "Using browser-only version of superagent in non-browser environment" Do I need to worry about it? Thank you!Gentes
A
9

You could try to check if you're actually in a browser environment, e.g. something like

if (window && window.sessionStorage) {
    // do your stuff with sessionStorage
}

or

if (typeof window !== 'undefined') {
    // do your stuff with sessionStorage
}
Alaynaalayne answered 13/6, 2019 at 12:49 Comment(2)
For me, the latter works but the former doesn't (SvelteKit): ReferenceError: window is not definedVereen
Note to self: Don't call sessionStorage.setItem() in a reactive statement ($:) since it will reset the stored value to the initial state on every refresh.Vereen
F
12

hey you need to use onMount

import { onMount } from 'svelte';

onMount(() => {
    sessionStorage.setItem('myWork', 'Developer');
});
Feline answered 28/9, 2019 at 9:1 Comment(1)
Or, you may need afterUpdate instead of onMount, to trigger some action on dom update. For me, after await goto(/);, need to use afterUpdate to read sessionStorage to make sure it works, when use onMount I need a refresh by hand sometimes.Diet
A
9

You could try to check if you're actually in a browser environment, e.g. something like

if (window && window.sessionStorage) {
    // do your stuff with sessionStorage
}

or

if (typeof window !== 'undefined') {
    // do your stuff with sessionStorage
}
Alaynaalayne answered 13/6, 2019 at 12:49 Comment(2)
For me, the latter works but the former doesn't (SvelteKit): ReferenceError: window is not definedVereen
Note to self: Don't call sessionStorage.setItem() in a reactive statement ($:) since it will reset the stored value to the initial state on every refresh.Vereen
I
2

While exsides approach is valid, Sveltekit's recommended approach is;

import { browser } from '$app/environment';

then

if (browser) {
    // do your stuff with sessionStorage...
}
Inhibit answered 23/3, 2023 at 9:53 Comment(3)
Nice, thanks for the addition, didn't know about that one! Did that exist at the time I wrote the answer?Alaynaalayne
I don't think it was. 😁Inhibit
@Alaynaalayne I think it was introduced recently, in the last 2 years maybe. it is the one I use more often since it makes sense semantically speaking.Shevlo

© 2022 - 2024 — McMap. All rights reserved.