Handle base path for 404 pages.

This commit is contained in:
Dillon Kearns 2021-07-21 14:42:58 -07:00
parent 00c91c8e9d
commit 573c6d72dd
4 changed files with 42 additions and 30 deletions

View File

@ -66,6 +66,7 @@ async function outputString(
const contentJsonString = JSON.stringify({
is404: args.is404,
staticData: args.contentJson,
path: args.route,
});
fs.writeFileSync(`dist/${normalizedRoute}/index.html`, args.htmlString);
fs.writeFileSync(

View File

@ -73,7 +73,7 @@ parse it before returning it and store the parsed version in the Cache
-}
lazyLoad :
List String
-> { currentUrl : Url }
-> { currentUrl : Url, basePath : List String }
-> ContentCache
-> Task Http.Error ( Url, ContentJson, ContentCache )
lazyLoad path urls cache =
@ -142,6 +142,7 @@ httpTask url =
type alias ContentJson =
{ staticData : RequestsAndPending
, is404 : Bool
, path : Maybe String
, notFoundReason : Maybe NotFoundReason.Payload
}
@ -152,9 +153,10 @@ contentJsonDecoder =
|> Decode.andThen
(\is404Value ->
if is404Value then
Decode.map3 ContentJson
Decode.map4 ContentJson
(Decode.succeed Dict.empty)
(Decode.succeed is404Value)
(Decode.field "path" Decode.string |> Decode.map Just)
(Decode.at [ "staticData", "notFoundReason" ]
(Decode.string
|> Decode.andThen
@ -177,16 +179,17 @@ contentJsonDecoder =
)
else
Decode.map3 ContentJson
Decode.map4 ContentJson
(Decode.field "staticData" RequestsAndPending.decoder)
(Decode.succeed is404Value)
(Decode.succeed Nothing)
(Decode.succeed Nothing)
)
update :
ContentCache
-> { currentUrl : Url }
-> { currentUrl : Url, basePath : List String }
-> ContentJson
-> ContentCache
update cache urls rawContent =
@ -200,6 +203,7 @@ update cache urls rawContent =
Nothing ->
{ staticData = rawContent.staticData
, is404 = rawContent.is404
, path = rawContent.path
, notFoundReason = rawContent.notFoundReason
}
|> Parsed
@ -208,18 +212,18 @@ update cache urls rawContent =
cache
pathForUrl : { currentUrl : Url } -> Path
pathForUrl { currentUrl } =
pathForUrl : { currentUrl : Url, basePath : List String } -> Path
pathForUrl { currentUrl, basePath } =
currentUrl.path
--|> String.dropLeft (String.length baseUrl.path)
|> String.chopForwardSlashes
|> String.split "/"
|> List.filter ((/=) "")
|> List.drop (List.length basePath)
is404 :
ContentCache
-> { currentUrl : Url }
-> { currentUrl : Url, basePath : List String }
-> Bool
is404 dict urls =
dict
@ -235,7 +239,7 @@ is404 dict urls =
notFoundReason :
ContentCache
-> { currentUrl : Url }
-> { currentUrl : Url, basePath : List String }
-> Maybe NotFoundReason.Payload
notFoundReason dict urls =
dict

View File

@ -33,9 +33,10 @@ mainView :
-> { title : String, body : Html userMsg }
mainView config model =
let
urls : { currentUrl : Url }
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = model.url
, basePath = config.basePath
}
in
case ContentCache.notFoundReason model.contentCache urls of
@ -63,26 +64,15 @@ mainView config model =
}
urlToPath : Url -> Url -> Path
urlToPath url baseUrl =
url.path
|> String.dropLeft (String.length baseUrl.path)
|> String.chopForwardSlashes
|> String.split "/"
|> List.filter ((/=) "")
|> Path.join
urlsToPagePath :
{ currentUrl : Url
}
{ currentUrl : Url, basePath : List String }
-> Path
urlsToPagePath urls =
urls.currentUrl.path
--|> String.dropLeft (String.length urls.baseUrl.path)
|> String.chopForwardSlashes
|> String.split "/"
|> List.filter ((/=) "")
|> List.drop (List.length urls.basePath)
|> Path.join
@ -134,24 +124,37 @@ init config flags url key =
ContentCache.init
(Maybe.map
(\cj ->
( urls.currentUrl
|> config.urlToRoute
|> config.routeToPath
( currentPath
, cj
)
)
contentJson
)
currentPath : List String
currentPath =
flags
|> Decode.decodeValue
(Decode.at [ "contentJson", "staticData", "path" ]
(Decode.string
|> Decode.map Path.fromString
|> Decode.map Path.toSegments
)
)
|> Result.mapError Decode.errorToString
|> Result.toMaybe
|> Maybe.withDefault []
contentJson : Maybe ContentJson
contentJson =
flags
|> Decode.decodeValue (Decode.field "contentJson" contentJsonDecoder)
|> Result.toMaybe
urls : { currentUrl : Url }
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = url
, basePath = config.basePath
}
in
case contentJson |> Maybe.map .staticData of
@ -323,9 +326,10 @@ update config appMsg model =
navigatingToSamePage =
(url.path == model.url.path) && (url /= model.url)
urls : { currentUrl : Url }
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = url
, basePath = config.basePath
}
in
if navigatingToSamePage then
@ -496,9 +500,11 @@ update config appMsg model =
HotReloadComplete contentJson ->
let
urls : { currentUrl : Url }
urls : { currentUrl : Url, basePath : List String }
urls =
{ currentUrl = model.url }
{ currentUrl = model.url
, basePath = config.basePath
}
pageDataResult : Result BuildError pageData
pageDataResult =

View File

@ -986,6 +986,7 @@ render404Page config model path notFoundReason =
}
)
)
, ( "path", Path.toAbsolute path )
]
-- TODO include the needed info for content.json?