From 78746c1195c709882e6008b74483fa0cc9ffdcc6 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 09:29:57 -0700 Subject: [PATCH 1/6] Avoid Dict to List conversions to improve performance. --- src/DataSource.elm | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/DataSource.elm b/src/DataSource.elm index 5b9e7ea6..7f64ab70 100644 --- a/src/DataSource.elm +++ b/src/DataSource.elm @@ -481,16 +481,19 @@ It would not work correctly if it chose between two responses that were reduced -} combineReducedDicts : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo combineReducedDicts dict1 dict2 = - (Dict.toList dict1 ++ Dict.toList dict2) - |> fromListDedupe Pages.StaticHttpRequest.merge + if Dict.size dict1 > Dict.size dict2 then + uniqueInsertAll dict2 dict1 + + else + uniqueInsertAll dict1 dict2 -fromListDedupe : (comparable -> a -> a -> a) -> List ( comparable, a ) -> Dict comparable a -fromListDedupe combineFn xs = - List.foldl - (\( key, value ) acc -> Dict.Extra.insertDedupe (combineFn key) key value acc) - Dict.empty - xs +uniqueInsertAll : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo +uniqueInsertAll dictToDedupeMerge startingDict = + Dict.foldl + (\key value acc -> Dict.Extra.insertDedupe (Pages.StaticHttpRequest.merge key) key value acc) + startingDict + dictToDedupeMerge lookup : KeepOrDiscard -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value ) From c4a0ca832cbf70ecfaf7612dc83092dfaccb91f1 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 11:22:27 -0700 Subject: [PATCH 2/6] Remove extra layer of request in succeed. --- src/DataSource.elm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/DataSource.elm b/src/DataSource.elm index 7f64ab70..6b5d0cab 100644 --- a/src/DataSource.elm +++ b/src/DataSource.elm @@ -637,11 +637,7 @@ andMap = -} succeed : a -> DataSource a succeed value = - Request Dict.empty - ( [] - , \_ _ _ -> - ApiRoute Dict.empty value - ) + ApiRoute Dict.empty value {-| Stop the StaticHttp chain with the given error message. If you reach a `fail` in your request, From e218a480a504fbaa7909760579ce0616855c9d48 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 17:32:54 -0700 Subject: [PATCH 3/6] Fix test output. --- tests/StaticHttpRequestsTests.elm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/StaticHttpRequestsTests.elm b/tests/StaticHttpRequestsTests.elm index 766d3a06..ee2c01fb 100644 --- a/tests/StaticHttpRequestsTests.elm +++ b/tests/StaticHttpRequestsTests.elm @@ -875,11 +875,11 @@ I encountered DataSource.distill with two matching keys that had differing encod Look for DataSource.distill with the key "stars" The first encoded value was: -86 +123 ------------------------------- The second encoded value was: -123""") +86""") ] --, describe "generateFiles" From a1078e65c95cbb3a09cc08e42f9cbe4dce342cc0 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 19:12:24 -0700 Subject: [PATCH 4/6] Remove redundant log statement. --- generator/src/render-worker.js | 1 - 1 file changed, 1 deletion(-) diff --git a/generator/src/render-worker.js b/generator/src/render-worker.js index 9a23825d..af45f20d 100644 --- a/generator/src/render-worker.js +++ b/generator/src/render-worker.js @@ -64,7 +64,6 @@ async function outputString( switch (fromElm.kind) { case "html": { const args = fromElm; - console.log(`Pre-rendered /${args.route}`); const normalizedRoute = args.route.replace(/index$/, ""); await fs.tryMkdir(`./dist/${normalizedRoute}`); const contentJsonString = JSON.stringify({ From 4ce6b18ed24272f412086cd51e16b9868c1d2498 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 19:43:06 -0700 Subject: [PATCH 5/6] Skip unnecessary 404 check in build to avoid extra computations. --- generator/src/render.js | 1 + src/Pages/Internal/Platform/Cli.elm | 39 ++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/generator/src/render.js b/generator/src/render.js index 72276c87..c7243f48 100755 --- a/generator/src/render.js +++ b/generator/src/render.js @@ -82,6 +82,7 @@ function runElmApp( flags: { secrets: process.env, staticHttpCache: global.staticHttpCache || {}, + mode, request: { payload: modifiedRequest, kind: "single-page", diff --git a/src/Pages/Internal/Platform/Cli.elm b/src/Pages/Internal/Platform/Cli.elm index f3dc3594..f8deb0a7 100644 --- a/src/Pages/Internal/Platform/Cli.elm +++ b/src/Pages/Internal/Platform/Cli.elm @@ -55,6 +55,7 @@ type alias Model route = , unprocessedPages : List ( Path, route ) , staticRoutes : Maybe (List ( Path, route )) , maybeRequestJson : RenderRequest route + , isDevServer : Bool } @@ -331,12 +332,14 @@ flagsDecoder : Decode.Decoder { secrets : SecretsDict , staticHttpCache : Dict String (Maybe String) + , isDevServer : Bool } flagsDecoder = - Decode.map2 - (\secrets staticHttpCache -> + Decode.map3 + (\secrets staticHttpCache isDevServer -> { secrets = secrets , staticHttpCache = staticHttpCache + , isDevServer = isDevServer } ) (Decode.field "secrets" SecretsDict.decoder) @@ -347,6 +350,7 @@ flagsDecoder = ) ) ) + (Decode.field "mode" Decode.string |> Decode.map (\mode -> mode == "dev-server")) {-| -} @@ -358,8 +362,8 @@ init : -> ( Model route, Effect ) init renderRequest contentCache config flags = case Decode.decodeValue flagsDecoder flags of - Ok { secrets, staticHttpCache } -> - initLegacy renderRequest { secrets = secrets, staticHttpCache = staticHttpCache } contentCache config flags + Ok { secrets, staticHttpCache, isDevServer } -> + initLegacy renderRequest { secrets = secrets, staticHttpCache = staticHttpCache, isDevServer = isDevServer } contentCache config flags Err error -> updateAndSendPortIfDone @@ -379,17 +383,18 @@ init renderRequest contentCache config flags = , unprocessedPages = [] , staticRoutes = Just [] , maybeRequestJson = renderRequest + , isDevServer = False } initLegacy : RenderRequest route - -> { a | secrets : SecretsDict, staticHttpCache : Dict String (Maybe String) } + -> { secrets : SecretsDict, staticHttpCache : Dict String (Maybe String), isDevServer : Bool } -> ContentCache -> ProgramConfig userMsg userModel route siteData pageData sharedData -> Decode.Value -> ( Model route, Effect ) -initLegacy renderRequest { secrets, staticHttpCache } contentCache config flags = +initLegacy renderRequest { secrets, staticHttpCache, isDevServer } contentCache config flags = let staticResponses : StaticResponses staticResponses = @@ -403,7 +408,12 @@ initLegacy renderRequest { secrets, staticHttpCache } contentCache config flags (config.data serverRequestPayload.frontmatter) config.sharedData ) - (config.handleRoute serverRequestPayload.frontmatter) + (if isDevServer then + config.handleRoute serverRequestPayload.frontmatter + + else + DataSource.succeed Nothing + ) RenderRequest.Api ( path, ApiRoute apiRequest ) -> StaticResponses.renderApiRequest @@ -451,6 +461,7 @@ initLegacy renderRequest { secrets, staticHttpCache } contentCache config flags , unprocessedPages = unprocessedPages , staticRoutes = unprocessedPagesState , maybeRequestJson = renderRequest + , isDevServer = isDevServer } in StaticResponses.nextStep config initialModel Nothing @@ -662,7 +673,12 @@ nextStepToEffect contentCache config model ( updatedStaticResponsesModel, nextSt pageFoundResult : Result BuildError (Maybe NotFoundReason) pageFoundResult = StaticHttpRequest.resolve ApplicationType.Browser - (config.handleRoute payload.frontmatter) + (if model.isDevServer then + config.handleRoute payload.frontmatter + + else + DataSource.succeed Nothing + ) model.allRawResponses |> Result.mapError (StaticHttpRequest.toBuildError (payload.path |> Path.toAbsolute)) in @@ -821,7 +837,12 @@ sendSinglePageProgress contentJson config model = pageFoundResult : Result BuildError (Maybe NotFoundReason) pageFoundResult = StaticHttpRequest.resolve ApplicationType.Browser - (config.handleRoute route) + (if model.isDevServer then + config.handleRoute route + + else + DataSource.succeed Nothing + ) model.allRawResponses |> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path) From 0c4f1c3a4d4afc143b60bd5b95b8b5c1e415da5f Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 19:52:56 -0700 Subject: [PATCH 6/6] Pass in flag in test. --- tests/StaticHttpRequestsTests.elm | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/StaticHttpRequestsTests.elm b/tests/StaticHttpRequestsTests.elm index ee2c01fb..f75d3e5b 100644 --- a/tests/StaticHttpRequestsTests.elm +++ b/tests/StaticHttpRequestsTests.elm @@ -1239,6 +1239,7 @@ startWithRoutes pageToLoad staticRoutes staticHttpCache pages = |> Encode.dict identity Encode.string ) , ( "staticHttpCache", encodedStaticHttpCache ) + , ( "mode", Encode.string "dev-server" ) ] encodedStaticHttpCache : Encode.Value