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

View File

@ -450,7 +450,11 @@ handleRoute maybeRoute =
name name
)} routeParams) ->\n Page.${name.join( )} 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) .parseRouteParams(name)
.map( .map(
(param) => (param) =>

View File

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

View File

@ -2,9 +2,11 @@ module RoutePattern exposing
( Ending(..) ( Ending(..)
, RoutePattern , RoutePattern
, Segment(..) , Segment(..)
, codec
, view , view
) )
import Codec exposing (Codec)
import Html exposing (Html) import Html exposing (Html)
@ -25,6 +27,50 @@ type Segment
| DynamicSegment String | 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 -> String
segmentToString segment = segmentToString segment =
case segment of case segment of
@ -35,6 +81,12 @@ segmentToString segment =
":" ++ name ":" ++ name
type alias ModuleContext =
{ moduleName : List String
, routePattern : RoutePattern
}
view : RoutePattern -> Html msg view : RoutePattern -> Html msg
view routePattern = view routePattern =
Html.span [] Html.span []