RFC: Port the `routes` resolver to Next.js
See original GitHub issuePart of the port of SliceMachine to Next consists in porting the routes
resolver.
What is routes
To use prismic with your application, you need at some point to be able to resolve the URL of a prismic document.
To do so, in Prismic we have the concept of linkResolver
but with SliceMachine, we introduced a new way that is called routes
.
It’s a declarative way of configuring your routes and it’s send to prismic so the API returns the URL of the document right away without having to compute it on your application.
Context
Today a typical routes
configuration would look like this:
[{
path: "/posts/:uid",
type: "post"
}, {
path: "/",
type: "homepage"
}]
But in Next.js, when you build a link you need two things, the url
and the href
.
It would typically look like this:
<Link href="/posts/[uid]" as="/posts/myFirstPost" />
This href
property is specific to Next.js and we need to make it easy to build from the routes
configuration.
Implementation
We could create a specific model dedicated to Next.js that would generated from the routes
and it would look like this:
{
post: {
path: "/posts/:uid",
href: "posts/[uid]"
},
homepage: {
path: "/",
href: "/",
}
}
That model would be exported in a module inside the user project so it could be used at any time to build links.
A Link would have this form considering that you have a docLink
that represents a content relationship:
import Routes from 'routes';
<Link href={Routes[docLink.type].href} as={docLink.url} />
It also mean that anytime you want to add a route, you only edit this module and we would have some kind of lib that automatically convert it back to the original format so it can be send to Prismic and automatically resolve the URLs in the API.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top GitHub Comments
while the Next resolver would look like:
Proposed by @sadache
I guess some of them do, if they come from this starter: https://github.com/prismicio/nextjs-blog, I don’t really know otherwise.
If we want to keep using resolvers, that would be perfectly fine to me. But as we removed the
linkResolver
in profit ofroutes
, we probably want to ditch thehrefResolver
too. A difference between routes (JSON) and hrefResolver (js) is also that we may be able to access your routes from inside the SliceZone, which makes it possible for us to write a default resolver there