Skip to content

Preferences load/save can crash app before mount when localStorage is blocked #55

Description

@ctoth

Summary

Preferences load and save touch localStorage outside any guard, so a browser where storage is blocked (sandboxed iframe, some private modes, storage disabled) throws during module init and takes the whole app down before React mounts.

Details

src/stores/preferencesStore.ts:137:

const storedData = localStorage.getItem(STORAGE_KEY);

This runs at module-evaluation time. The try at line 141 wraps only JSON.parse, not the getItem. If getItem throws, it propagates out of loadPreferences() during import.

The persist subscriber has the same exposure on write, src/stores/preferencesStore.ts:183-185:

usePreferences.subscribe((state) => {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
});

Because this throws inside a Zustand listener, a quota-exceeded on write surfaces as the failure of whatever unrelated set() call triggered it.

Contrast src/hooks/useChannelHistory.tsx:147 and :177, which wrap both the read and the write.

Fix direction

Wrap both getItem and setItem in try/catch and degrade to in-memory defaults when storage is unavailable.

Verification

Read src/stores/preferencesStore.ts end to end.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions