Don't call user's init function during pre-rendering phase.

This commit is contained in:
Dillon Kearns 2020-01-20 08:02:46 -08:00
parent fe6e046610
commit defe0beb33
2 changed files with 35 additions and 2 deletions

View File

@ -9,7 +9,8 @@ module.exports = function pagesInit(
document.addEventListener("DOMContentLoaded", function() {
let app = mainElmModule.init({
flags: {
secrets: null
secrets: null,
isPrerendering: navigator.userAgent.indexOf("Headless") >= 0,
}
});

View File

@ -254,6 +254,17 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla
case contentCache of
Ok okCache ->
let
phase =
case Decode.decodeValue (Decode.field "isPrerendering" Decode.bool) flags of
Ok True ->
Prerender
Ok False ->
Client
Err _ ->
Client
( userModel, userCmd ) =
initUserModel maybePagePath
@ -284,6 +295,7 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla
, url = url
, userModel = userModel
, contentCache = contentCache
, phase = phase
}
, cmd
)
@ -297,6 +309,7 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla
, url = url
, userModel = userModel
, contentCache = contentCache
, phase = Client
}
, Cmd.batch
[ userCmd |> Cmd.map UserMsg
@ -333,9 +346,15 @@ type alias ModelDetails userModel metadata view =
, url : Url.Url
, contentCache : ContentCache metadata view
, userModel : userModel
, phase : Phase
}
type Phase
= Prerender
| Client
update :
String
->
@ -524,7 +543,20 @@ application config =
\msg outerModel ->
case outerModel of
Model model ->
update config.canonicalSiteUrl config.view config.pathKey config.onPageChange config.toJsPort config.document config.update msg model
let
userUpdate =
case model.phase of
Prerender ->
noOpUpdate
Client ->
config.update
noOpUpdate =
\userMsg userModel ->
( userModel, Cmd.none )
in
update config.canonicalSiteUrl config.view config.pathKey config.onPageChange config.toJsPort config.document userUpdate msg model
|> Tuple.mapFirst Model
|> Tuple.mapSecond (Cmd.map AppMsg)