lily/browser
The browser module covers everything a Lily page does with the browser
around the Runtime, the URL and history, links,
localStorage, deploys and the development reload. It sits beside
client rather than inside it, and it is built on the
same public seams you have, client.observe,
client.model,
client.update_model and
client.dispatch, meaning nothing here is out of
your reach. On Erlang it does nothing, letting shared code compile.
Every function that hooks into a runtime takes it and gives it back,
letting the pieces you want pipe on between
client.start and
client.connect:
import lily/browser
import lily/client
import lily/transport
pub fn main() {
shared.store()
|> client.start
|> browser.url(set: fn(model, uri) {
Model(..model, route: shared.parse_route(uri))
})
|> browser.intercept_links
|> browser.persist(fields: [theme_field()])
|> browser.connection_status(set: fn(model, connected) {
Model(..model, connected:)
})
|> browser.reload_on_new_version
|> client.connect(with: transport.websocket("/ws"))
}
Lily does client-side routing, though it is opt-in and deliberately not a
router. url mirrors the location into your model through a
setter, navigate and replace_url move
around, and intercept_links lets ordinary <a href>
links do so without a page reload. Use load_page when the
server has to handle the path, and load_external for
another site.
State that should outlive a reload is described with
persisted_field and switched on with
persist. Each field is hydrated from localStorage and
mirrored back whenever it changes, under the lily_persisted_ key prefix.
clear_persisted wipes them, and what the offline
queue holds, say on logout.
For deploys, reload_on_new_version reloads the
page when the server announces a new build. For development,
hot_reload reloads on every rebuild and
recover_after_reload carries simple fields of
the model across it.
Three page signals mirror the browser into your model the same way.
visibility tracks whether the tab is showing,
online whether the browser is online and
viewport the window’s size. Separately,
confirm_leaving asks before the page is closed with
unsaved changes.
Types
One persisted piece of the model, built with
persisted_field and handed to
persist.
pub opaque type PersistedField(model)
Values
pub fn clear_persisted() -> Nil
pub fn confirm_leaving(
runtime: client.Runtime(model, message, mode),
when when: fn(model) -> Bool,
) -> client.Runtime(model, message, mode)
Asks the user to confirm before they close or leave the page while when
holds, for unsaved changes. The browser shows its own wording.
runtime |> browser.confirm_leaving(when: fn(model) { model.draft != "" })
pub fn connection_status(
runtime: client.Runtime(model, message, mode),
set set: fn(model, Bool) -> model,
) -> client.Runtime(model, message, mode)
Tracks the connection in the model, for disabling elements while offline.
set is called with True when the connection opens and False when it
drops.
Pipe it before client.connect so the opening is
not missed.
runtime
|> browser.connection_status(set: fn(model, connected) {
Model(..model, connected:)
})
pub fn hot_reload(
runtime: client.Runtime(model, message, mode),
) -> client.Runtime(model, message, mode)
Reloads the page whenever the dev server’s reload socket, on the page’s port
plus one, reports a rebuild. Does nothing unless the page is on
localhost, 127.0.0.1 or [::1].
Pair with recover_after_reload to keep simple
model fields across the reload.
runtime |> browser.hot_reload
pub fn intercept_links(
runtime: client.Runtime(model, message, mode),
) -> client.Runtime(model, message, mode)
Makes a left-click on a same-origin <a href> a navigate
instead of a page load. Pipe once, after url.
Everything that is a real navigation falls through untouched, external
links, target="_blank", download, rel="external", mailto: and
tel:, modified clicks, in-page #fragment anchors and any link carrying
data-lily-native.
runtime |> browser.url(set: set_route) |> browser.intercept_links
pub fn intercept_links_with(
runtime: client.Runtime(model, message, mode),
within within: String,
opt_out_attribute opt_out_attribute: String,
) -> client.Runtime(model, message, mode)
Like intercept_links but limited to links inside a
CSS selector and opted out by an attribute of your choosing.
runtime
|> browser.intercept_links_with(
within: "#app",
opt_out_attribute: "data-external",
)
pub fn load_external(url: String) -> Nil
Full page navigation to another site, such as a payment page. Any origin,
but only an absolute http or https URL, anything else is refused with a
warning.
browser.load_external("https://pay.example.com/checkout")
pub fn load_page(path: String) -> Nil
Full page navigation, leaving the app. Counterpart to
navigate.
Use it when the server has to handle the path, after a logout that clears a cookie or when entering a server-rendered flow. The connection closes.
It only loads a page on this origin, a javascript: URL or another site is
refused with a warning, which keeps a path that came from data from running
script. Use load_external for another site.
browser.load_page("/logout")
pub fn navigate(path: String) -> Nil
Pushes a history entry and tells the url setter, letting the model
follow the new location without a page load.
Only a path on this origin, anything else is refused with a warning.
browser.navigate("/projects/42")
pub fn online(
runtime: client.Runtime(model, message, mode),
set set: fn(model, Bool) -> model,
) -> client.Runtime(model, message, mode)
Tracks whether the browser is online in the model, straight away and on
every change. It reports the browser’s own view, see
connection_status for the server connection.
runtime
|> browser.online(set: fn(model, online) { Model(..model, online:) })
pub fn persist(
runtime: client.Runtime(model, message, mode),
fields fields: List(PersistedField(model)),
) -> client.Runtime(model, message, mode)
Keeps parts of the model in localStorage. Each field is read back into the
model when this is piped on, then written again whenever it changes.
Fields that cannot be read, because they are missing or no longer decode,
keep the model’s own value. Pipe it right after
client.start.
runtime |> browser.persist(fields: [theme_field(), token_field()])
pub fn persisted_field(
key key: String,
encode encode: fn(value) -> json.Json,
decode decoder: decode.Decoder(value),
get get: fn(model) -> value,
set set: fn(model, value) -> model,
) -> PersistedField(model)
Describes one persisted value, stored in localStorage as JSON under
lily_persisted_ followed by the key.
get and set move the value in and out of the model, encode and
decode handle its JSON.
browser.persisted_field(
key: "theme",
encode: theme_to_json,
decode: theme_decoder(),
get: fn(model) { model.theme },
set: fn(model, theme) { Model(..model, theme:) },
)
pub fn recover_after_reload(initial: model) -> model
Carries the model across a hot_reload, called on your
initial model.
Top level primitive fields (Int, Float, String, Bool) are merged
into initial by name. Anything else, a nested custom type, a list or an
option, keeps the fresh value.
Only on localhost, 127.0.0.1 or [::1], elsewhere initial comes back
untouched, the stash is a development aid.
let initial = browser.recover_after_reload(shared.initial_model())
pub fn reload_on_new_version(
runtime: client.Runtime(model, message, mode),
) -> client.Runtime(model, message, mode)
Reloads the page when the server announces a different build than the first one this runtime saw.
That means a new deploy or a restarted server. A
client.ProtocolMismatch reloads too, once
per pair of protocols. At most two such reloads happen in ten seconds and
six in ten minutes, counted in sessionStorage, and without sessionStorage
it doesn’t reload at all. The server and persist bring most
state back after the reload.
runtime |> browser.reload_on_new_version
pub fn replace_url(path: String) -> Nil
pub fn url(
runtime: client.Runtime(model, message, mode),
set set: fn(model, uri.Uri) -> model,
) -> client.Runtime(model, message, mode)
Tracks the browser location in the model. set receives the parsed
Uri to map onto your own
route type. The initial location is applied straight away, then every back
or forward, navigate and replace_url
applies it again.
runtime
|> browser.url(set: fn(model, uri) {
Model(..model, route: shared.parse_route(uri))
})
pub fn viewport(
runtime: client.Runtime(model, message, mode),
set set: fn(model, #(Int, Int)) -> model,
) -> client.Runtime(model, message, mode)
Tracks the window’s width and height in the model, straight away and at most once a frame while it is resized.
runtime |> browser.viewport(set: fn(model, size) { Model(..model, size:) })
pub fn visibility(
runtime: client.Runtime(model, message, mode),
set set: fn(model, Bool) -> model,
) -> client.Runtime(model, message, mode)
Tracks whether the tab is showing in the model, straight away and on every change, for pausing work or marking a user as away.
runtime
|> browser.visibility(set: fn(model, visible) { Model(..model, visible:) })