question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Port the `routes` resolver to Next.js

See original GitHub issue

Part 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:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
arnaudlewiscommented, Jun 16, 2020
<LinkComponent docLink={docLink} />

while the Next resolver would look like:

import routes from '../my-routes'

return LinkComponent: ({ docLink }) => {
  return <Link href={routes[docLink.type].href} as={docLink.url} />
}

Proposed by @sadache

1reaction
hypervillaincommented, Jun 15, 2020

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 of routes, we probably want to ditch the hrefResolver 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Layouts RFC | Next.js
Nested routes and layouts, client and server routing, React 18 features, and designed for Server Components.
Read more >
NextJS is CHANGING - is this Remix??? - YouTube
The new NextJS Layout RFC included some really exciting changes I wanted to chat aboutHuge shoutout to Lee and Delba for hanging out...
Read more >
RFC 8804 - Content Delivery Network Interconnection (CDNI ...
Content Delivery Network Interconnection (CDNI) Request Routing Extensions RFC 8804 · Status · IESG evaluation record · IESG writeups · Email expansions ·...
Read more >
RFC 7626: DNS Privacy Considerations
Internet Engineering Task Force (IETF) S. Bortzmeyer Request for Comments: 7626 AFNIC ... A client, the stub resolver, issues a DNS query to...
Read more >
service-names-port-numbers.txt
User Ports are assigned by IANA using the "IETF Review" process, ... 38 tcp Route Access Protocol [Robert_Ullmann] [Robert_Ullmann] rap 38 udp Route...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found