From e1af771fc74ba5b7abaf9a33a55e5716e9a51160 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Thu, 9 Feb 2023 12:01:16 -0800 Subject: [PATCH] Handle dev server reloads from 404 pages. --- src/Pages/Internal/Platform.elm | 81 ++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/src/Pages/Internal/Platform.elm b/src/Pages/Internal/Platform.elm index c8dfde42..31b2453f 100644 --- a/src/Pages/Internal/Platform.elm +++ b/src/Pages/Internal/Platform.elm @@ -764,7 +764,86 @@ update config appMsg model = _ -> ( model, NoEffect ) ) - |> Result.withDefault ( model, NoEffect ) + |> Result.withDefault + (let + pageDataResult : Maybe (InitKind sharedData pageData actionData errorPage) + pageDataResult = + case Bytes.Decode.decode config.decodeResponse pageDataBytes of + Just (ResponseSketch.RenderPage _ _) -> + Nothing + + Just (ResponseSketch.HotUpdate pageData shared actionData) -> + OkPage shared pageData actionData + |> Just + + Just (ResponseSketch.NotFound notFound) -> + NotFound notFound + |> Just + + _ -> + Nothing + in + case pageDataResult of + Just (OkPage sharedData pageData actionData) -> + let + urls : { currentUrl : Url, basePath : List String } + urls = + { currentUrl = model.url + , basePath = config.basePath + } + + pagePath : Path + pagePath = + urlsToPagePath urls + + userFlags : Pages.Flags.Flags + userFlags = + model.userFlags + |> Decode.decodeValue + (Decode.field "userFlags" Decode.value) + |> Result.withDefault Json.Encode.null + |> Pages.Flags.BrowserFlags + + ( userModel, userCmd ) = + Just + { path = + { path = pagePath + , query = model.url.query + , fragment = model.url.fragment + } + , metadata = config.urlToRoute model.url + , pageUrl = + Just + { protocol = model.url.protocol + , host = model.url.host + , port_ = model.url.port_ + , path = pagePath + , query = model.url.query |> Maybe.map QueryParams.fromString + , fragment = model.url.fragment + } + } + |> config.init userFlags sharedData pageData actionData + + cmd : Effect userMsg pageData actionData sharedData userEffect errorPage + cmd = + UserCmd userCmd + in + ( { model + | pageData = + Ok + { userModel = userModel + , sharedData = sharedData + , pageData = pageData + , actionData = actionData + } + , notFound = Nothing + } + , cmd + ) + + _ -> + ( model, NoEffect ) + ) FetcherStarted fetcherKey transitionId fetcherData initiatedAt -> ( { model