refrakt/live_reload
Live reload for refrakt dev.
refrakt dev runs a small Server-Sent Events server next to your app
(port 35729 by default) and tells the app about that port through the
REFRAKT_LIVE_RELOAD_PORT environment variable. After each successful
rebuild and restart, and after a change under priv/, it sends a
reload event, and the script below reloads the page.
The script is rendered only when APP_ENV=dev and
REFRAKT_LIVE_RELOAD_PORT holds a port number. With APP_ENV=prod or
APP_ENV=test, or when the app is started without refrakt dev,
script() renders nothing. The app itself never serves a reload
endpoint: the event stream lives in the refrakt dev process.
Usage
Put it at the end of the <body> in your root layout:
import refrakt/live_reload
body([], [main([class("container")], [inner]), live_reload.script()])
Projects generated by refrakt new already do this in
web/layouts/root_layout.gleam.
Values
pub fn script() -> element.Element(message)
The live reload script, or nothing outside refrakt dev. Reads
APP_ENV and REFRAKT_LIVE_RELOAD_PORT each time it renders.
pub fn script_for(
app_env app_env: Result(String, Nil),
port port: Result(String, Nil),
) -> element.Element(message)
script with the two environment variables passed in, for tests.
Renders the script only for app_env: Ok("dev") and a valid port.