@@ -4,8 +4,10 @@
44
55module Frontend where
66
7+import Control.Monad
78import qualified Data.Text as T
89import qualified Data.Text.Encoding as T
10+import Language.Javascript.JSaddle (eval, liftJSM)
911
1012import Obelisk.Frontend
1113import Obelisk.Configs
@@ -18,6 +20,9 @@ import Common.Api
1820import Common.Route
1921
2022
23+-- This runs in a monad that can be run on the client or the server.
24+-- To run code in a pure client or pure server context, use one of the
25+-- `prerender` functions.
2126frontend :: Frontend (R FrontendRoute)
2227frontend = Frontend
2328 { _frontend_head = do
@@ -26,6 +31,13 @@ frontend = Frontend
2631 , _frontend_body = do
2732 el "h1" $ text "Welcome to Obelisk!"
2833 el "p" $ text $ T.pack commonStuff
34+
35+ -- `prerender` and `prerender_` let you choose a widget to run on the server
36+ -- during prerendering and a different widget to run on the client with
37+ -- JavaScript. The following will generate a `blank` widget on the server and
38+ -- print "Hello, World!" on the client.
39+ prerender_ blank $ liftJSM $ void $ eval ("console.log('Hello, World!')" :: T.Text)
40+
2941 elAttr "img" ("src" =: static @"obelisk.jpg") blank
3042 el "div" $ do
3143 exampleConfig <- getConfig "common/example"