You could return a Cmd from your update function to backup the state to some persistent storage on every update. That storage could be either in the browser (via localStorage or indexedDB) or on your server via HTTP request.
If itās one of the browser storage APIs then youāll need to use a port and some JavaScript code. It could look something like this:
port sendToJs : Model -> Cmd Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
let
newModel =
case msg of
Msg1 ->
-- your usual update stuff
in
(newModel, sendToJs newModel)
This assumes that your Model can be automatically serialized by the Elm runtime. If you have union types or something in there, you may need to Json.Encode it yourself instead.
EDIT: I forgot to mention how to actually recover! If youāre using ports to store data in the browser, you can retrieve it again on page reload with some JavaScript code before you start Elm. Then pass the initial state into the Elm.Main.fullscreen function. Inside your Elm program, use Html.programWithFlags and then handle the initial data in your init function. More details in the Elm guide.
Alternatively, in the case where you backup to the server rather than localStorage, you can return a Cmd from your init function that does an HTTP request to retrieve the data.