In this video we look at how you might use Local Storage with SvelteKit to keep track of which theme a user prefers on your site. It is becoming more common to have light and dark themes on sites. This makes it a great time to look at how you can use Local Storage with Svelte stores to implement the functionality. We start by comparing Local Storage to Session Storage and cookies. We then look at a naïve implementation and finally a reusable, scalable implementation leveraging Svelte stores. On top we see how you can debug Local Storage code using in-browser developer tools.
I hope there’s at least one thing you find interesting in there. If there is then why don't roll the video!
- Using vanilla-extract and themes in SvelteKit,
- Using Session Storage with SvelteKit,
- Using HTTP cookies for user session data storage with SvelteKit,
- Full code for the demo site shown in the video ,
- Svelte stores tutorial ,
- MDN Local Storage docs ,
- MDN Web Storage docs .
- Svelte stores are a convenient way of storing app state and sharing it between remote components. As any example instead of passing your user’s chosen colour theme from a layout component down through several layers of components, you just place it in the store. The store acts as a single source of truth and any component which needs access can easily get access and even modify the centrally stored data. Svelte stores need minimal code to set up but offer advanced features in case you need. Examples are adding custom modifiers and deriving the value of one store from another. You can also create a readable store for cases where a preference might be read automatically and never need updating.
- You can combine the Local Storage API with a writable Svelte store to track a user’s colour theme preference. Whenever they hit the toggle button, the value gets written to the store which in turn adds the choice to Local Storage. That way when the user next visits your site, the choice is pulled from Local Storage into the fresh instance of the Svelte store and the user has the colour theme they prefer. Only a few lines of code are needed. First set up the theme store. Then read a value as $theme. Finally you can update the value using theme.set('dark') as an example.
If you have found this video useful, see links below for further related content on this site. I do hope you learned one new thing from the video. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on Twitter, giving me a mention so I can see what you did. Finally be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as Search Engine Optimisation among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.