Signals kept across page loads
Page::persist keeps a signal in localStorage, so the next page load starts from the visitor's last value.
Keeping a choice for the next page
A page loads with the values Page::new was given, so a choice the visitor made in the browser, such as a color theme or a list's density, is gone on the next page. Page::persist, fn persist<T>(self, signal: Sig<S, T>) -> Self, keeps one signal in the browser's localStorage. The browser stores the signal's value after each write, and the next page that persists the same path starts from the stored value in place of the one Page::new was given.
use hypersig::html::button;
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
enum Density {
Comfortable,
Compact,
}
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
density: Density,
unread: i32,
}
let page = Page::new(
"Inbox",
Inbox {
density: Density::Comfortable,
unread: 3,
},
)
// The next page load that persists `density` starts from the stored value.
.persist(Inbox::density())
.child(
button()
.on_click(Inbox::density().set(&Density::Compact))
.text("Compact"),
);Before the stored value is drawn
The server renders the first HTML from the initial value, because it never sees the stored one. Where the stored value differs from the initial one, the page stays hidden until the runtime has drawn the stored value, so the visitor does not see the initial value drawn first. If the runtime fails to start, the page shows after a short wait.
Pages that share a stored value
Every page of one application under one base that persists the same path shares one stored value. Two pages that persist the same path with different types share it too, and nothing checks that. Give a persisted field a name that no other page uses for another type.
Signals that cannot be persisted
persist takes a Sig<S, T>, a field reached from the page root. A computed signal would be recomputed on load, a read-only view names a field the page does not write, and a field of a list item has no fixed path, so each of them is a compile error:
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
price: f64,
#[signals(computed)]
total: f64,
}
// error[E0308]: mismatched types: expected `Sig<Cart, _>`, found `Sig<Cart, f64, Computed>`
let page = Page::new("Cart", Cart { price: 2.5, total: 0.0 }).persist(Cart::total());What the types leave unchecked
- The runtime checks only that a stored value is the same kind of value as the initial one: text, a number, a boolean, a list, a struct, or
None. A value of another kind is skipped. An enum variant that an older build wrote and that was since removed is still a string, so it reaches the page as stored. - Every script on the page's origin can read and write
localStorage, and the value stays after the visitor signs out, so persist a preference and leave a token or personal data out. - An action sees a persisted value only when its call passes the signal as an argument, and then gets the value the page holds, which is the stored one once the runtime has started.
Next
Lists renders a Vec field and changes it in the browser. The theme picker in Theming persists the visitor's color scheme.