Include more not found context.

This commit is contained in:
Dillon Kearns 2021-06-10 11:53:36 -07:00
parent 07bed53a79
commit 908e9a4243
4 changed files with 112 additions and 36 deletions

View File

@ -51,6 +51,7 @@ import Head
import NotFoundReason exposing (NotFoundReason)
import Pages.PageUrl exposing (PageUrl)
import Path exposing (Path)
import RoutePattern exposing (RoutePattern)
import Shared
import View exposing (View)
@ -71,7 +72,7 @@ type alias PageWithState routeParams templateData templateModel templateMsg =
, init : Maybe PageUrl -> Shared.Model -> StaticPayload templateData routeParams -> ( templateModel, Cmd templateMsg )
, update : PageUrl -> StaticPayload templateData routeParams -> Maybe Browser.Navigation.Key -> templateMsg -> templateModel -> Shared.Model -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, subscriptions : Maybe PageUrl -> routeParams -> Path -> templateModel -> Shared.Model -> Sub templateMsg
, handleRoute : (routeParams -> List ( String, String )) -> routeParams -> DataSource (Maybe NotFoundReason)
, handleRoute : { moduleName : List String, routePattern : RoutePattern } -> (routeParams -> List ( String, String )) -> routeParams -> DataSource (Maybe NotFoundReason)
, kind : String
}
@ -99,7 +100,11 @@ type Builder routeParams templateData
StaticPayload templateData routeParams
-> List Head.Tag
, serverless : Bool
, handleRoute : (routeParams -> List ( String, String )) -> routeParams -> DataSource (Maybe NotFoundReason)
, handleRoute :
{ moduleName : List String, routePattern : RoutePattern }
-> (routeParams -> List ( String, String ))
-> routeParams
-> DataSource (Maybe NotFoundReason)
, kind : String
}
@ -222,7 +227,7 @@ singleRoute { data, head } =
, staticRoutes = DataSource.succeed [ {} ]
, head = head
, serverless = False
, handleRoute = \_ _ -> DataSource.succeed Nothing
, handleRoute = \_ _ _ -> DataSource.succeed Nothing
, kind = "static"
}
@ -241,7 +246,7 @@ prerenderedRoute { data, head, routes } =
, head = head
, serverless = False
, handleRoute =
\toRecord routeParams ->
\moduleContext toRecord routeParams ->
routes
|> DataSource.map
(\allRoutes ->
@ -251,7 +256,7 @@ prerenderedRoute { data, head, routes } =
else
-- TODO pass in toString function, and use a custom one to avoid Debug.toString
Just <|
NotFoundReason.NotPrerendered
NotFoundReason.NotPrerendered moduleContext
(allRoutes
|> List.map toRecord
)
@ -275,7 +280,7 @@ prerenderedRouteWithFallback { data, head, routes, handleFallback } =
, head = head
, serverless = False
, handleRoute =
\toRecord routeParams ->
\moduleContext toRecord routeParams ->
handleFallback routeParams
|> DataSource.andThen
(\handleFallbackResult ->
@ -297,7 +302,7 @@ prerenderedRouteWithFallback { data, head, routes, handleFallback } =
else
Just <|
NotFoundReason.NotPrerenderedOrHandledByFallback
NotFoundReason.NotPrerenderedOrHandledByFallback moduleContext
(allRoutes
|> List.map toRecord
)
@ -321,7 +326,7 @@ serverlessRoute { data, head, routeFound } =
, head = head
, serverless = True
, handleRoute =
\_ routeParams ->
\moduleContext _ routeParams ->
routeFound routeParams
|> DataSource.map
(\found ->
@ -329,7 +334,7 @@ serverlessRoute { data, head, routeFound } =
Nothing
else
Just NotFoundReason.UnhandledServerRoute
Just (NotFoundReason.UnhandledServerRoute moduleContext)
)
, kind = "serverless"
}

View File

@ -450,7 +450,11 @@ handleRoute maybeRoute =
name
)} routeParams) ->\n Page.${name.join(
"."
)}.page.handleRoute (\\param -> [ ${routeHelpers
)}.page.handleRoute { moduleName = [ ${name
.map((part) => `"${part}"`)
.join(", ")} ], routePattern = ${routeHelpers.toElmPathPattern(
name
)} } (\\param -> [ ${routeHelpers
.parseRouteParams(name)
.map(
(param) =>

View File

@ -7,6 +7,12 @@ import Path exposing (Path)
import RoutePattern exposing (RoutePattern)
type alias ModuleContext =
{ moduleName : List String
, routePattern : RoutePattern
}
type alias Payload =
{ path : Path
, reason : NotFoundReason
@ -19,9 +25,9 @@ type alias Record =
type NotFoundReason
= NoMatchingRoute
| NotPrerendered (List Record)
| NotPrerenderedOrHandledByFallback (List Record)
| UnhandledServerRoute
| NotPrerendered ModuleContext (List Record)
| NotPrerenderedOrHandledByFallback ModuleContext (List Record)
| UnhandledServerRoute ModuleContext
document :
@ -61,7 +67,7 @@ document pathPatterns payload =
)
]
NotPrerendered routes ->
NotPrerendered moduleContext routes ->
[ Html.h1 []
[ Html.text "Page Not Found"
]
@ -72,12 +78,18 @@ document pathPatterns payload =
)
]
, Html.text " successfully matched the route "
, Html.br [] []
, Html.br [] []
, Html.code []
[ Html.text routePattern
[ RoutePattern.view moduleContext.routePattern
]
, Html.br [] []
, Html.br [] []
, Html.text " from the Page Module "
, Html.br [] []
, Html.br [] []
, Html.code []
[ Html.text routeThing
[ Html.text (moduleName moduleContext)
]
, Html.br [] []
, Html.br [] []
@ -119,7 +131,7 @@ document pathPatterns payload =
[ Html.text "Try changing "
, Html.code [] [ Html.text "routes" ]
, Html.text " in "
, Html.code [] [ Html.text "src/Blog/Slug_.elm " ]
, Html.code [] [ Html.text (moduleName moduleContext) ]
, Html.text " to make sure it includes these "
, Html.code [] [ Html.text "RouteParams" ]
, Html.text "."
@ -134,16 +146,6 @@ document pathPatterns payload =
}
routePattern : String
routePattern =
"/blog/:slug"
routeThing : String
routeThing =
"src/Blog/Slug_.elm"
codec : Codec Payload
codec =
Codec.object Payload
@ -164,22 +166,30 @@ reasonCodec =
NoMatchingRoute ->
vNoMatchingRoute
NotPrerendered prerenderedRoutes ->
vNotPrerendered prerenderedRoutes
NotPrerendered moduleContext prerenderedRoutes ->
vNotPrerendered moduleContext prerenderedRoutes
NotPrerenderedOrHandledByFallback prerenderedRoutes ->
vNotPrerenderedOrHandledByFallback prerenderedRoutes
NotPrerenderedOrHandledByFallback moduleContext prerenderedRoutes ->
vNotPrerenderedOrHandledByFallback moduleContext prerenderedRoutes
UnhandledServerRoute ->
vUnhandledServerRoute
UnhandledServerRoute moduleContext ->
vUnhandledServerRoute moduleContext
)
|> Codec.variant0 "NoMatchingRoute" NoMatchingRoute
|> Codec.variant1 "NotPrerendered" NotPrerendered (Codec.list recordCodec)
|> Codec.variant1 "NotPrerenderedOrHandledByFallback" NotPrerenderedOrHandledByFallback (Codec.list recordCodec)
|> Codec.variant0 "UnhandledServerRoute" UnhandledServerRoute
|> Codec.variant2 "NotPrerendered" NotPrerendered moduleContextCodec (Codec.list recordCodec)
|> Codec.variant2 "NotPrerenderedOrHandledByFallback" NotPrerenderedOrHandledByFallback moduleContextCodec (Codec.list recordCodec)
|> Codec.variant1 "UnhandledServerRoute" UnhandledServerRoute moduleContextCodec
|> Codec.buildCustom
moduleContextCodec : Codec ModuleContext
moduleContextCodec =
Codec.object ModuleContext
|> Codec.field "moduleName" .moduleName (Codec.list Codec.string)
|> Codec.field "routePattern" .routePattern RoutePattern.codec
|> Codec.buildObject
recordCodec : Codec (List ( String, String ))
recordCodec =
Codec.list (Codec.tuple Codec.string Codec.string)
@ -196,3 +206,8 @@ recordToString fields =
|> String.join ", "
)
++ " }"
moduleName : ModuleContext -> String
moduleName moduleContext =
("src" :: moduleContext.moduleName |> String.join "/") ++ ".elm"

View File

@ -2,9 +2,11 @@ module RoutePattern exposing
( Ending(..)
, RoutePattern
, Segment(..)
, codec
, view
)
import Codec exposing (Codec)
import Html exposing (Html)
@ -25,6 +27,50 @@ type Segment
| DynamicSegment String
codec : Codec RoutePattern
codec =
Codec.object RoutePattern
|> Codec.field "segments" .segments (Codec.list segmentCodec)
|> Codec.field "ending" .ending (Codec.maybe endingCodec)
|> Codec.buildObject
segmentCodec : Codec Segment
segmentCodec =
Codec.custom
(\vStatic vDynamic value ->
case value of
StaticSegment string ->
vStatic string
DynamicSegment string ->
vDynamic string
)
|> Codec.variant1 "StaticSegment" StaticSegment Codec.string
|> Codec.variant1 "DynamicSegment" DynamicSegment Codec.string
|> Codec.buildCustom
endingCodec : Codec Ending
endingCodec =
Codec.custom
(\vOptional vRequiredSplat vOptionalSplat value ->
case value of
Optional string ->
vOptional string
RequiredSplat ->
vRequiredSplat
OptionalSplat ->
vOptionalSplat
)
|> Codec.variant1 "Optional" Optional Codec.string
|> Codec.variant0 "RequiredSplat" RequiredSplat
|> Codec.variant0 "OptionalSplat" OptionalSplat
|> Codec.buildCustom
segmentToString : Segment -> String
segmentToString segment =
case segment of
@ -35,6 +81,12 @@ segmentToString segment =
":" ++ name
type alias ModuleContext =
{ moduleName : List String
, routePattern : RoutePattern
}
view : RoutePattern -> Html msg
view routePattern =
Html.span []