Learn HalogenGHC loads in the background

12. Prerendering and hydration

Prerendering puts component HTML in the page before its browser program starts. When the program loads, hydration adopts that HTML and attaches behaviour. The tutorial uses this approach: examples/learn/Prerender.hs generated the page you are reading.

Rendering natively

Halogen.Static.renderToHtml mounts a component in the in-memory DOM using the normal driver. It waits for initialization to finish and returns HTML. For this lesson's starting component, a native program

main = renderToHtml Lesson.component () >>= T.putStrLn

prints

<div><p>Count: <!---->0</p><button>+</button></div>

The tutorial's native prerender executable does this for every route and inserts the output into <div id="app"></div>. The browser entry point, Main.hs, is compiled to WebAssembly.

Adopting the page

Use hydrateUI instead of runUI to adopt existing HTML. It compares the first render with the page in order, retains matching elements, attaches listeners and updates properties or text. Mismatched nodes are replaced.

Repairs are allowed, but cost work and can lose the user's selection. To adopt the page cleanly:

  • Make the initial browser render match the prerendered HTML. Initial state must not depend on browser-only information, and rendering should depend on state alone.
  • Keep separators between adjacent text nodes. The renderer inserts <!----> so the HTML parser does not merge "Count: " and the count.
  • Remember that native prerendering waits for H.onInitialize, while browser initialization happens after adoption. Unconditional state changes during initialization can therefore make their initial HTML differ.

The lesson wrapper inserts the HTML above and gives each element a dashed outline before hydration. Retained nodes keep their outlines. Replace HH.p_ with HH.h2_ to see a newly created heading without one.

Exercise

Disable the button until the browser program is ready. Add State {count, ready} with ready = False. Use HP.disabled (not st.ready) and display "Loading..." while it is false.

Add an H.onInitialize program that sets ready only when inBrowser, importing inBrowser from Halogen.JSBits. Without that guard, native prerendering would already enable the button. With it, the build produces <button disabled>Loading...</button>, and the browser enables it after adoption.

Lesson.hs
{-# LANGUAGE OverloadedStrings #-}

module Lesson (component) where

import Control.Monad.IO.Class (MonadIO)
import Control.Monad.State (modify)
import Data.Text qualified as T
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.HTML.Events qualified as HE

data Action = Increment

-- A build renders this natively, in MemDOM, with renderToHtml component ();
-- the program around this module mounts it with hydrateUI rather than runUI.
component :: forall q o m. (MonadIO m) => H.Component q () o m
component =
  H.component initialState render [H.onAction handleAction]
  where
    initialState _ = pure (0 :: Int)
    render count =
      HH.div_
        [ HH.p_ [HH.text "Count: ", HH.text (T.pack (show count))]
        , HH.button [HE.onClick (\_ -> Increment)] [HH.text "+"]
        ]
    handleAction Increment = modify (+ 1)

The compiler loads once the page has started.

Your component will appear here after compilation.