You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
$persist initial value gets overwritten with undefined when localStorage is unavailable #4866
When localStorage is disabled or unavailable (for example, in Safari Private Browsing mode, restricted iframe contexts, or SSR), Alpine's $persist falls back to an in-memory Map storage.
In this fallback state, any property initialized with $persist(initialValue) loses its initial value and gets set to undefined.
Reproduction Steps
Disable localStorage (or open Safari in Private Browsing mode where accessing localStorage throws a SecurityError).
Initialize an Alpine component with a persisted property:
count becomes undefined on component initialization.
Root Cause
According to the Web Storage specification, localStorage.getItem() returns null when a key does not exist. However, the in-memory fallback uses dummy.get.bind(dummy) (from a JS Map), which returns undefined for missing keys.
Because storageHas() checks storage.getItem(key) !== null, the condition undefined !== null evaluates to true. Alpine interprets this as "the item already exists in storage", calls storageGet(), receives undefined, and overwrites the initial value 10 with undefined.
Activity