@@ -12,6 +12,7 @@
1212module Obelisk.Frontend
1313 ( ObeliskWidget
1414 , Frontend (..)
15+ , HeadRender (..)
1516 , runFrontend
1617 , renderFrontendHtml
1718 ) where
@@ -68,9 +69,12 @@ type ObeliskWidget t x route m =
6869
6970data Frontend route = Frontend
7071 { _frontend_head :: !(forall t m x. ObeliskWidget t x route m => RoutedT t route m ())
72+ , _frontend_headRender :: HeadRender
7173 , _frontend_body :: !(forall t m x. ObeliskWidget t x route m => RoutedT t route m ())
7274 }
7375
76+data HeadRender = HeadRender_Static | HeadRender_Dynamic
77+
7478type Widget' x = ImmediateDomBuilderT DomTimeline (DomCoreWidget x)
7579
7680-- | A widget that isn't attached to any particular part of the DOM hierarchy
@@ -102,16 +106,16 @@ runWithHeadAndBody app = withJSContextSingletonMono $ \jsSing -> do
102106 headFragment <- createDocumentFragment globalDoc
103107 bodyFragment <- createDocumentFragment globalDoc
104108 unreadyChildren <- liftIO $ newIORef 0
105− let commit = do
109+ let commitHead = do
106110 headElement <- getHeadUnchecked globalDoc
111+ replaceElementContents headElement headFragment
112+ commitBody = do
107113 bodyElement <- getBodyUnchecked globalDoc
108− void $ inAnimationFrame' $ \_ -> do
109− replaceElementContents headElement headFragment
110− replaceElementContents bodyElement bodyFragment
114+ replaceElementContents bodyElement bodyFragment
111115 liftIO $ attachWidget''' $ \events -> flip runWithJSContextSingleton jsSing $ do
112116 (postBuild, postBuildTriggerRef) <- newEventWithTriggerRef
113− let appendImmediateDom :: DOM.DocumentFragment -> Widget' () c -> FloatingWidget () c
114− appendImmediateDom df w = do
117+ let appendImmediateDom :: JSM () -> DOM.DocumentFragment -> Widget' () c -> FloatingWidget () c
118+ appendImmediateDom commit df w = do
115119 events' <- TriggerEvent.askEvents
116120 lift $ do
117121 doc <- getOwnerDocumentUnchecked df
@@ -122,10 +126,10 @@ runWithHeadAndBody app = withJSContextSingletonMono $ \jsSing -> do
122126 , _immediateDomBuilderEnv_commitAction = commit
123127 }
124128 runImmediateDomBuilderT w builderEnv events'
125− flip runPostBuildT postBuild $ flip runTriggerEventT events $ app (appendImmediateDom headFragment) (appendImmediateDom bodyFragment)
126− liftIO (readIORef unreadyChildren) >>= \case
127− 0 -> DOM.liftJSM commit
128− _ -> return ()
129+ flip runPostBuildT postBuild $ flip runTriggerEventT events $ app (appendImmediateDom commitHead headFragment) (appendImmediateDom commitBody bodyFragment)
130+ -- liftIO (readIORef unreadyChildren) >>= \case
131+ -- 0 -> DOM.liftJSM commit
132+ -- _ -> return ()
129133 return postBuildTriggerRef
130134
131135runFrontend :: forall backendRoute route. Encoder Identity Identity (R (Sum backendRoute (ObeliskRoute route))) PageName -> Frontend (R route) -> JSM ()
@@ -147,7 +151,9 @@ runFrontend validFullEncoder frontend = do
147151 -> m a
148152 runMyRouteViewT = runRouteViewT ve
149153 runWithHeadAndBody $ \appendHead appendBody -> runMyRouteViewT $ do
150− mapRoutedT (mapSetRouteT appendHead) $ _frontend_head frontend
154+ case _frontend_headRender frontend of
155+ HeadRender_Dynamic -> mapRoutedT (mapSetRouteT appendHead) $ _frontend_head frontend
156+ HeadRender_Static -> return ()
151157 mapRoutedT (mapSetRouteT appendBody) $ _frontend_body frontend
152158
153159renderFrontendHtml