Exchanging custom data between routes
and data
functions in a page module?
#214
-
Hello, I'm in the process of migrating my blog to elm-pages v2 as a way to learn Elm. So far, I already learned a lot of Elm idioms and I like it a lot. I do have an elm-pages specific question. I have a page module for my blog articles which contains this piece of code: type alias RouteParams =
{ year : String
, month : String
, slug : String
}
routes : DataSource (List RouteParams)
routes =
Posts.all |> DataSource.map (List.map (\post -> RouteParams post.year post.month post.slug ))
data : RouteParams -> DataSource Data
data routeParams =
routeParams
|> findFileByRouteParams
|> DataSource.andThen
( \filePath ->
filePath
|> MarkdownCodec.withFrontmatter Data blogPostMetadataDecoder TailwindMarkdownRenderer.renderer
) In the type alias Post =
{ year : String
, month : String
, slug : String
, filePath : String
} As you can see, I'm dropping the But when the Ringo |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Given a URL (which will be parsed into That's it. If there was a way to carry over data as part of pre-rendering routes, then it would mean the dev server would need to pre-render all routes each time in order to give you the data for a specific route (that's bad for performance). The same performance cost would apply to rendering a single page in serverless mode (a feature that's currently in alpha).
So instead, In your case here, with a consistent naming pattern, you can go look up a file based on all the information in the Hope that helps clarify the design and mental model. |
Beta Was this translation helpful? Give feedback.
elm-pages
has an intentionally lean architecture and mental model. The model is:Given a URL (which will be parsed into
RouteParams
for a given Page Module), what DataSource should I go get?That's it. If there was a way to carry over data as part of pre-rendering routes, then it would mean the dev server would need to pre-render all routes each time in order to give you the data for a specific route (that's bad for performance). The same performance cost would apply to rendering a single page in serverless mode (a feature that's currently in alpha).
elm-pages
would also need to carry over the underlying data from building up the pre-rendered routes from thatDataSource
. If you don't need…