Skip to main content

leptos_use/storage/
use_storage.rs

1use crate::{core::MaybeRwSignal, storage::StorageType, utils::FilterOptions};
2use codee::{CodecError, Decoder, Encoder};
3use default_struct_builder::DefaultBuilder;
4use leptos::prelude::*;
5use leptos::reactive::wrappers::read::Signal;
6use std::sync::Arc;
7use thiserror::Error;
8use wasm_bindgen::JsValue;
9
10const INTERNAL_STORAGE_EVENT: &str = "leptos-use-storage";
11
12/// Reactive [Storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage).
13///
14/// The function returns a triplet `(read_signal, write_signal, delete_from_storage_fn)`.
15///
16/// ## Demo
17///
18/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_storage)
19///
20/// ## Usage
21///
22/// Pass a [`StorageType`] to determine the kind of key-value browser storage to use.
23/// The specified key is where data is stored. All values are stored as UTF-16 strings which
24/// is then encoded and decoded via the given `*Codec`. This value is synced with other calls using
25/// the same key on the same page and across tabs for local storage.
26/// See [`UseStorageOptions`] to see how behavior can be further customised.
27///
28/// Values are (en)decoded via the given codec. You can use any of the string codecs or a
29/// binary codec wrapped in `Base64`.
30///
31/// > Please check [the codec chapter](https://leptos-use.rs/codecs.html) to see what codecs are
32/// > available and what feature flags they require.
33///
34/// ## Example
35///
36/// ```
37/// # use leptos::prelude::*;
38/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage};
39/// # use serde::{Deserialize, Serialize};
40/// # use codee::string::{FromToStringCodec, JsonSerdeCodec, Base64};
41/// # use codee::binary::ProstCodec;
42/// #
43/// # #[component]
44/// # pub fn Demo() -> impl IntoView {
45/// // Binds a struct:
46/// let (state, set_state, _) = use_local_storage::<MyState, JsonSerdeCodec>("my-state");
47///
48/// // Binds a bool, stored as a string:
49/// let (flag, set_flag, remove_flag) = use_session_storage::<bool, FromToStringCodec>("my-flag");
50///
51/// // Binds a number, stored as a string:
52/// let (count, set_count, _) = use_session_storage::<i32, FromToStringCodec>("my-count");
53/// // Binds a number, stored in JSON:
54/// let (count, set_count, _) = use_session_storage::<i32, JsonSerdeCodec>("my-count-kept-in-js");
55///
56/// // Bind string with SessionStorage stored in ProtoBuf format:
57/// let (id, set_id, _) = use_storage::<String, Base64<ProstCodec>>(
58///     StorageType::Session,
59///     "my-id",
60/// );
61/// #    view! { }
62/// # }
63///
64/// // Data stored in JSON must implement Serialize, Deserialize.
65/// // And you have to add the feature "serde" to your project's Cargo.toml
66/// #[derive(Serialize, Deserialize, Clone, PartialEq)]
67/// pub struct MyState {
68///     pub hello: String,
69///     pub greeting: String,
70/// }
71///
72/// // Default can be used to implement initial or deleted values.
73/// // You can also use a signal via UseStorageOptions::default_value`
74/// impl Default for MyState {
75///     fn default() -> Self {
76///         Self {
77///             hello: "hi".to_string(),
78///             greeting: "Hello".to_string()
79///         }
80///     }
81/// }
82/// ```
83///
84/// ## Server-Side Rendering
85///
86/// > Make sure you follow the [instructions in Server-Side Rendering](https://leptos-use.rs/server_side_rendering.html).
87///
88/// On the server the returned signals will just read/manipulate the `initial_value` without persistence.
89///
90/// ### Hydration bugs and `use_cookie`
91///
92/// If you use a value from storage to control conditional rendering you might run into issues with
93/// hydration.
94///
95/// ```
96/// # use leptos::prelude::*;
97/// # use leptos_use::storage::use_session_storage;
98/// # use codee::string::FromToStringCodec;
99/// #
100/// # #[component]
101/// # pub fn Example() -> impl IntoView {
102/// let (flag, set_flag, _) = use_session_storage::<bool, FromToStringCodec>("my-flag");
103///
104/// view! {
105///     <Show when=move || flag.get()>
106///         <div>Some conditional content</div>
107///     </Show>
108/// }
109/// # }
110/// ```
111///
112/// You can see hydration warnings in the browser console and the conditional parts of
113/// the app might never show up when rendered on the server and then hydrated in the browser. The
114/// reason for this is that the server has no access to storage and therefore will always use
115/// `initial_value` as described above. So on the server your app is always rendered as if
116/// the value from storage was `initial_value`. Then in the browser the actual stored value is used
117/// which might be different, hence during hydration the DOM looks different from the one rendered
118/// on the server which produces the hydration warnings.
119///
120/// The recommended way to avoid this is to use `use_cookie` instead because values stored in cookies
121/// are available on the server as well as in the browser.
122///
123/// If you still want to use storage instead of cookies you can use the `delay_during_hydration`
124/// option that will use the `initial_value` during hydration just as on the server and delay loading
125/// the value from storage by an animation frame. This gets rid of the hydration warnings and makes
126/// the app correctly render things. Some flickering might be unavoidable though.
127///
128/// ```
129/// # use leptos::prelude::*;
130/// # use leptos_use::storage::{use_local_storage_with_options, UseStorageOptions};
131/// # use codee::string::FromToStringCodec;
132/// #
133/// # #[component]
134/// # pub fn Example() -> impl IntoView {
135/// let (flag, set_flag, _) = use_local_storage_with_options::<bool, FromToStringCodec>(
136///     "my-flag",
137///     UseStorageOptions::default().delay_during_hydration(true),
138/// );
139///
140/// view! {
141///     <Show when=move || flag.get()>
142///         <div>Some conditional content</div>
143///     </Show>
144/// }
145/// # }
146/// ```
147#[inline(always)]
148pub fn use_storage<T, C>(
149    storage_type: StorageType,
150    key: impl Into<Signal<String>>,
151) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
152where
153    T: Default + Clone + PartialEq + Send + Sync + 'static,
154    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
155{
156    use_storage_with_options::<T, C>(storage_type, key, UseStorageOptions::default())
157}
158
159/// Version of [`use_storage`] that accepts [`UseStorageOptions`].
160pub fn use_storage_with_options<T, C>(
161    storage_type: StorageType,
162    key: impl Into<Signal<String>>,
163    options: UseStorageOptions<T, <C as Encoder<T>>::Error, <C as Decoder<T>>::Error>,
164) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
165where
166    T: Clone + PartialEq + Send + Sync,
167    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
168{
169    let UseStorageOptions {
170        on_error,
171        listen_to_storage_changes,
172        initial_value,
173        filter,
174        delay_during_hydration,
175    } = options;
176
177    let (data, set_data) = initial_value.into_signal();
178    let default = data.get_untracked();
179
180    #[cfg(feature = "ssr")]
181    {
182        let _ = on_error;
183        let _ = listen_to_storage_changes;
184        let _ = filter;
185        let _ = delay_during_hydration;
186        let _ = storage_type;
187        let _ = key;
188        let _ = INTERNAL_STORAGE_EVENT;
189
190        let remove = move || {
191            set_data.set(default.clone());
192        };
193
194        (data, set_data, remove)
195    }
196
197    #[cfg(not(feature = "ssr"))]
198    {
199        use crate::{
200            WatchOptions, sendwrap_fn, use_event_listener, use_window, watch_with_options,
201        };
202        use send_wrapper::SendWrapper;
203
204        let delaying = StoredValue::new(
205            delay_during_hydration
206                && Owner::current_shared_context()
207                    .map(|sc| sc.during_hydration())
208                    .unwrap_or_default(),
209        );
210
211        let key = key.into();
212
213        // Get storage API
214        let storage = storage_type
215            .into_storage()
216            .map_err(UseStorageError::StorageNotAvailable)
217            .and_then(|s| s.ok_or(UseStorageError::StorageReturnedNone));
218        let storage = handle_error(&on_error, storage);
219
220        // Schedules a storage event microtask. Uses a queue to avoid re-entering the runtime
221        let dispatch_storage_event = {
222            let on_error = on_error.to_owned();
223
224            move || {
225                let on_error = on_error.to_owned();
226
227                queue_microtask(move || {
228                    // TODO : better to use a BroadcastChannel (use_broadcast_channel)?
229                    // Note: we cannot construct a full StorageEvent so we _must_ rely on a custom event
230                    let custom = web_sys::CustomEventInit::new();
231                    custom.set_detail(&JsValue::from_str(&key.get_untracked()));
232                    let result = window()
233                        .dispatch_event(
234                            &web_sys::CustomEvent::new_with_event_init_dict(
235                                INTERNAL_STORAGE_EVENT,
236                                &custom,
237                            )
238                            .expect("failed to create custom storage event"),
239                        )
240                        .map_err(UseStorageError::NotifyItemChangedFailed);
241                    let _ = handle_error(&on_error, result);
242                })
243            }
244        };
245
246        let read_from_storage = {
247            let storage = storage.to_owned();
248            let on_error = on_error.to_owned();
249
250            move || {
251                storage
252                    .to_owned()
253                    .and_then(|storage| {
254                        // Get directly from storage
255                        let result = storage
256                            .get_item(&key.get_untracked())
257                            .map_err(UseStorageError::GetItemFailed);
258                        handle_error(&on_error, result)
259                    })
260                    .unwrap_or_default() // Drop handled Err(())
261                    .as_ref()
262                    .map(|encoded| {
263                        // Decode item
264                        let result = C::decode(encoded)
265                            .map_err(|e| UseStorageError::ItemCodecError(CodecError::Decode(e)));
266                        handle_error(&on_error, result)
267                    })
268                    .transpose()
269                    .unwrap_or_default() // Drop handled Err(())
270            }
271        };
272
273        // Fetches direct from browser storage and fills set_data if changed (memo)
274        let fetch_from_storage = {
275            let default = default.clone();
276            let read_from_storage = read_from_storage.clone();
277
278            SendWrapper::new(move || {
279                let fetched = read_from_storage();
280
281                match fetched {
282                    Some(value) => {
283                        // Replace data if changed
284                        if value != data.get_untracked() {
285                            set_data.set(value)
286                        }
287                    }
288
289                    // Revert to default
290                    None => set_data.set(default.clone()),
291                };
292            })
293        };
294
295        // Fires when storage needs to be fetched
296        let notify = ArcTrigger::new();
297
298        // Refetch from storage. Keeps track of how many times we've been notified. Does not increment for calls to set_data
299        let notify_id = Memo::<usize>::new({
300            let notify = notify.clone();
301            let fetch_from_storage = fetch_from_storage.clone();
302
303            move |prev| {
304                notify.track();
305                match prev {
306                    None => 1, // Avoid async fetch of initial value
307                    Some(prev) => {
308                        fetch_from_storage();
309                        prev + 1
310                    }
311                }
312            }
313        });
314
315        // Set item on internal (non-event) page changes to the data signal
316        {
317            let storage = storage.to_owned();
318            let on_error = on_error.to_owned();
319            let dispatch_storage_event = dispatch_storage_event.to_owned();
320
321            let _ = watch_with_options(
322                move || (notify_id.get(), data.get()),
323                move |(id, value), prev, _| {
324                    // Skip setting storage on changes from external events. The ID will change on external events.
325                    let change_from_external_event =
326                        prev.map(|(prev_id, _)| *prev_id != *id).unwrap_or_default();
327
328                    if change_from_external_event {
329                        return;
330                    }
331
332                    // Don't write default value if store is empty or still hydrating
333                    if value == &default && (read_from_storage().is_none() || delaying.get_value())
334                    {
335                        return;
336                    }
337
338                    if let Ok(storage) = &storage {
339                        // Encode value
340                        let result = C::encode(value)
341                            .map_err(|e| UseStorageError::ItemCodecError(CodecError::Encode(e)))
342                            .and_then(|enc_value| {
343                                // Set storage -- sends a global event
344                                storage
345                                    .set_item(&key.get_untracked(), &enc_value)
346                                    .map_err(UseStorageError::SetItemFailed)
347                            });
348                        let result = handle_error(&on_error, result);
349                        // Send internal storage event
350                        if result.is_ok() {
351                            dispatch_storage_event();
352                        }
353                    }
354                },
355                WatchOptions::default().filter(filter).immediate(true),
356            );
357        }
358
359        if delaying.get_value() {
360            request_animation_frame({
361                let fetch_from_storage = fetch_from_storage.clone();
362                move || {
363                    delaying.set_value(false);
364                    fetch_from_storage()
365                }
366            });
367        } else {
368            fetch_from_storage();
369        }
370
371        if listen_to_storage_changes {
372            // Listen to global storage events
373            let _ = use_event_listener(use_window(), leptos::ev::storage, {
374                let notify = notify.clone();
375
376                move |ev| {
377                    let ev_key = ev.key();
378                    // Key matches or all keys deleted (None)
379                    if ev_key == Some(key.get_untracked()) || ev_key.is_none() {
380                        notify.notify()
381                    }
382                }
383            });
384            // Listen to internal storage events
385            let _ = use_event_listener(
386                use_window(),
387                leptos::ev::Custom::new(INTERNAL_STORAGE_EVENT),
388                {
389                    let notify = notify.clone();
390
391                    move |ev: web_sys::CustomEvent| {
392                        if Some(key.get_untracked()) == ev.detail().as_string() {
393                            notify.notify()
394                        }
395                    }
396                },
397            );
398        };
399
400        Effect::watch(
401            move || key.get(),
402            {
403                let notify = notify.clone();
404                move |_, _, _| notify.notify()
405            },
406            false,
407        );
408
409        // Remove from storage fn
410        let remove = {
411            sendwrap_fn!(move || {
412                let _ = storage.as_ref().map(|storage| {
413                    // Delete directly from storage
414                    let result = storage
415                        .remove_item(&key.get_untracked())
416                        .map_err(UseStorageError::RemoveItemFailed);
417                    let _ = handle_error(&on_error, result);
418                    notify.notify();
419                    dispatch_storage_event();
420                });
421            })
422        };
423
424        (data, set_data, remove)
425    }
426}
427
428/// Session handling errors returned by [`use_storage_with_options`].
429#[derive(Error, Debug)]
430pub enum UseStorageError<E, D> {
431    #[error("storage not available")]
432    StorageNotAvailable(JsValue),
433    #[error("storage not returned from window")]
434    StorageReturnedNone,
435    #[error("failed to get item")]
436    GetItemFailed(JsValue),
437    #[error("failed to set item")]
438    SetItemFailed(JsValue),
439    #[error("failed to delete item")]
440    RemoveItemFailed(JsValue),
441    #[error("failed to notify item changed")]
442    NotifyItemChangedFailed(JsValue),
443    #[error("failed to encode / decode item value")]
444    ItemCodecError(CodecError<E, D>),
445}
446
447/// Options for use with [`fn@crate::storage::use_local_storage_with_options`], [`fn@crate::storage::use_session_storage_with_options`] and [`use_storage_with_options`].
448#[derive(DefaultBuilder)]
449pub struct UseStorageOptions<T, E, D>
450where
451    T: Send + Sync + 'static,
452{
453    // Callback for when an error occurs
454    #[builder(skip)]
455    on_error: Arc<dyn Fn(UseStorageError<E, D>) + Send + Sync>,
456    // Whether to continuously listen to changes from browser storage
457    listen_to_storage_changes: bool,
458    // Initial value to use when the storage key is not set
459    #[builder(skip)]
460    initial_value: MaybeRwSignal<T>,
461    // Debounce or throttle the writing to storage whenever the value changes
462    #[builder(into)]
463    filter: FilterOptions,
464    /// Delays the reading of the value from storage by one animation frame during hydration.
465    /// This ensures that during hydration the value is the initial value just like it is on the server
466    /// which helps prevent hydration errors. Defaults to `false`.
467    delay_during_hydration: bool,
468}
469
470/// Calls the on_error callback with the given error. Removes the error from the Result to avoid double error handling.
471#[cfg(not(feature = "ssr"))]
472fn handle_error<T, E, D>(
473    on_error: &Arc<dyn Fn(UseStorageError<E, D>) + Send + Sync>,
474    result: Result<T, UseStorageError<E, D>>,
475) -> Result<T, ()> {
476    result.map_err(|err| (on_error)(err))
477}
478
479impl<T: Default, E, D> Default for UseStorageOptions<T, E, D>
480where
481    T: Send + Sync + 'static,
482{
483    fn default() -> Self {
484        Self {
485            on_error: Arc::new(|_err| ()),
486            listen_to_storage_changes: true,
487            initial_value: MaybeRwSignal::default(),
488            filter: FilterOptions::default(),
489            delay_during_hydration: false,
490        }
491    }
492}
493
494impl<T: Default, E, D> UseStorageOptions<T, E, D>
495where
496    T: Send + Sync + 'static,
497{
498    /// Optional callback whenever an error occurs.
499    pub fn on_error(
500        self,
501        on_error: impl Fn(UseStorageError<E, D>) + Send + Sync + 'static,
502    ) -> Self {
503        Self {
504            on_error: Arc::new(on_error),
505            ..self
506        }
507    }
508
509    /// Initial value to use when the storage key is not set. Note that this value is read once on creation of the storage hook and not updated again. Accepts a signal and defaults to `T::default()`.
510    pub fn initial_value(self, initial: impl Into<MaybeRwSignal<T>>) -> Self {
511        Self {
512            initial_value: initial.into(),
513            ..self
514        }
515    }
516}