The IsRoute
typeclass is used to mainly define a Route Prism-based encoding for your route types. In other words, deriving IsRoute
for the example route,
-- An example of nested routes
-- Route's expected encoding is given as a comment.
data Route
= Route_Index -- index.html
| Route_About -- about.html
| Route_Contact -- contact.html
| Route_Blog BlogRoute -- blog/<BlogRoute>
data BlogRoute
= BlogRoute_Index -- index.html
| BlogRoute_Post Slug -- post/<Slug>
newtype Slug = Slug { unSlug :: String }
enables us to convert between Route_Blog BlogRoute_Index
and /blog/index.html
and vice versa (as shown in the tutorial).
Explanation
IsRoute
is defined as:
class IsRoute r where
-- (1)
type RouteModel r :: Type
-- (2)
routePrism :: RouteModel r -> Prism_ FilePath r
-- (3)
routeUniverse :: RouteModel r -> [r]
-
RouteModel
is (optionally) used to specify the value that is used for encoding routes (as defined byroutePrism
).-
For the blog route example above, this maybe be a type that contains your blog posts. It can also be
()
if you do not care about it.
-
For the blog route example above, this maybe be a type that contains your blog posts. It can also be
-
routePrism
gives us an optics prism (see Route Prism) that can be used to encode and decode between the route type and the.html
filepath. -
routeUniverse
simply returns a list of routes to statically generate.
Generic deriving
IsRoute
may be derived genericallly; see Generic deriving.