Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/generate_changelog/incoming/1848.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### PR [#1848](https://github.com/danielmiessler/Fabric/pull/1848) by [zeddy303](https://github.com/zeddy303): Fix localStorage SSR error in favorites-store

- Fix localStorage SSR error in favorites-store by using SvelteKit's browser constant instead of typeof localStorage check to properly handle server-side rendering and prevent 'localStorage.getItem is not a function' error when running dev server
11 changes: 6 additions & 5 deletions web/src/lib/store/favorites-store.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { writable } from 'svelte/store';
import { browser } from '$app/environment';

// Load favorites from localStorage if available
const storedFavorites = typeof localStorage !== 'undefined'
const storedFavorites = browser
? JSON.parse(localStorage.getItem('favoritePatterns') || '[]')
: [];

const createFavoritesStore = () => {
const { subscribe, set, update } = writable<string[]>(storedFavorites);

return {
subscribe,
toggleFavorite: (patternName: string) => {
Expand All @@ -17,7 +18,7 @@ const createFavoritesStore = () => {
: [...favorites, patternName];

// Save to localStorage
if (typeof localStorage !== 'undefined') {
if (browser) {
localStorage.setItem('favoritePatterns', JSON.stringify(newFavorites));
}

Expand All @@ -26,11 +27,11 @@ const createFavoritesStore = () => {
},
reset: () => {
set([]);
if (typeof localStorage !== 'undefined') {
if (browser) {
localStorage.removeItem('favoritePatterns');
}
}
};
};

export const favorites = createFavoritesStore();
export const favorites = createFavoritesStore();