Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds back SSR #126

Merged
merged 15 commits into from
Aug 6, 2024
Prev Previous commit
Next Next commit
Moves cache to end
  • Loading branch information
mikesol committed Aug 3, 2024
commit 2ac8a2c0660cd6912d7c7d036459e6c68a1efb6b
78 changes: 37 additions & 41 deletions deku-core/src/Deku/Toplevel.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import Prelude
import Control.Monad.ST.Class (liftST)
import Control.Monad.ST.Internal as ST
import Control.Monad.ST.Uncurried (runSTFn1, runSTFn3)
import Control.Plus (empty)
import Data.Bifunctor (lmap)
import Data.Filterable (filterMap)
import Data.FoldableWithIndex (foldlWithIndex, forWithIndex_)
import Data.Map as Map
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (un)
import Data.Set as Set
import Data.String as String
Expand Down Expand Up @@ -125,35 +124,14 @@ ssrInElement elt (Nut nut) = do
forWithIndex_ textCache \tag value -> do
transformTextNode (Ancestry.toStringRepresentationInDOM tag) $ fromDekuText
(un SSRTextRenderingInfo value).backingText
let
cache = foldlWithIndex
( \i b (SSRTextRenderingInfo { ancestry, isImpure }) -> Map.insert i
(SerializableSSRTextRenderingInfo { ancestry, isImpure })
b
)
( foldlWithIndex
( \i
b
( SSRElementRenderingInfo
{ ancestry, isImpure }
) -> Map.insert i
( SerializableSSRElementRenderingInfo
{ ancestry, isImpure }
)
b
)
Map.empty
elementCache
)
textCache
let
setMapOpPar = Set.fromFoldable
<<< filterMap (unsafeCollectLineage >=> truncateLineageBy1)
<<< (Set.toUnfoldable :: _ -> Array _)
setMapOp = Set.fromFoldable
<<< filterMap unsafeCollectLineage
<<< (Set.toUnfoldable :: _ -> Array _)
--
--
allParDynAncestry <- setMapOpPar <$> (liftST $ ST.read dynCacheRef)
allParPortalAncestry <- setMapOpPar <$> (liftST $ ST.read portalCacheRef)
allParFixedAncestry <- setMapOpPar <$> (liftST $ ST.read fixedCacheRef)
Expand All @@ -165,7 +143,7 @@ ssrInElement elt (Nut nut) = do
let
allDekuEltAndTextAncestries =
foldl (\b a -> maybe b (flip Set.insert b) $ unsafeCollectLineage a)
Set.empty $ Map.keys cache
Set.empty $ Map.keys textCache <> Map.keys elementCache
safeEntries = Set.filter
( hasPlainAncestry
(allParDynAncestry <> allParPortalAncestry <> allParFixedAncestry)
Expand Down Expand Up @@ -202,7 +180,26 @@ ssrInElement elt (Nut nut) = do
{ html: String.replace (String.Pattern "data-deku-value")
(String.Replacement "value")
htmlString
, cache
, cache: foldlWithIndex
( \i b (SSRTextRenderingInfo { ancestry, isImpure }) -> Map.insert i
(SerializableSSRTextRenderingInfo { ancestry, isImpure })
b
)
( foldlWithIndex
( \i
b
( SSRElementRenderingInfo
{ ancestry, isImpure }
) -> Map.insert i
( SerializableSSRElementRenderingInfo
{ ancestry, isImpure }
)
b
)
Map.empty
elementCache
)
textCache
, livePortals
}

Expand Down Expand Up @@ -235,22 +232,21 @@ hydrateInElement { cache, livePortals } ielt (Nut nut) = do
attr <- getAttribute "data-deku-ssr" elt
case attr of
Nothing -> throwException (error "Could not get ssr rep")
Just k -> pure $
( Ancestry.unsafeFakeAncestry k /\ HydrationRenderingInfo
{ isImpure: fromMaybe
false
do
cacheValue <- Map.lookup (Ancestry.unsafeFakeAncestry k)
cache
case cacheValue of
Just k -> do
let serialized = Map.lookup (Ancestry.unsafeFakeAncestry k) cache
pure $
( Ancestry.unsafeFakeAncestry k /\ HydrationRenderingInfo
{ isImpure: serialized # maybe
false
case _ of
SerializableSSRElementRenderingInfo
{ isImpure } ->
pure isImpure
_ -> empty
, isBoring: false
, backingElement: elt
}
)
{ isImpure } -> isImpure
SerializableSSRTextRenderingInfo { isImpure } ->
isImpure
, isBoring: false
, backingElement: elt
}
)
textNodes <-
Map.fromFoldable <<< map (\{ k, v } -> Ancestry.unsafeFakeAncestry k /\ v)
<$>
Expand Down
Loading