Get the previous and next items of a content relationship programatically

I'm working on a project that lets our content editors set up courses, each with a collection of lessons and potentially quizzes. I've got this working by having a custom Course type, and custom Lesson and Quiz types. When a course is created, content editors utilize a content relationship group field to include the lessons and quizzes. Right now it's getting all the data pulled in exactly how I need. That being said, I'm trying to figure out a more effective way to allow our users to navigate between lessons, essentially getting the next and previous items from the content relationship. Right now I've got a directory structure with two dynamic page routes:

-- [lessonUid].tsx

So in [lessonUid].tsx I am querying the course by the courseUid passed into my dynamic page routes, and getting all the associated lessons and quizzes:


const { courseUid, lessonUid } = req.query as { courseUid: string, lessonUid: string };

const course = await prismic.getByUID('course', courseUid, {
  fetchLinks: ['lessons', 'quiz'],

Then I'm taking the length of the array returned from this query and getting the index of the current lesson from the lessonUid param:

const lessonIndex = { item: { uid: string } }) => {
  return content.item.uid;

After this, I can set a constant for nextLesson or previousLesson like so:

const prevLesson =[lessonIndex - 1];
const nextLesson =[lessonIndex + 1];

That being said, this is a fair amount of manual work; what I'm wondering is, is there an easy way to get the previous and next items from a content relationship programatically? Directly in the query that is? I'd rather not have to do all of this work every time a lesson is requested. I'm open to getting the content in a completely different way as well, I just want to find a more efficient mode of getting the job done.

Hi @jwinton,

Welcome to the community forum and thanks for reaching out to us.

I believe you can apply pagination for going over link by link of one content relationship, but only one type of content relationship at a time. I mean only lessons in one query and apply pagination there and write a different query for quizzes.

What version of @prismicio/client are you using?
V5: @prismicio/client Technical Reference - Prismic
V6: @prismicio/client Technical Reference - Prismic

Let me know if you have any further questions.


Thanks; I think the solution I outlined above will end up being the most effective, but do you have a place I could look at your proposed solution in case my use case changes? I'm using version 6.4.3 of @prismicio/client.

Hello @jwinton

I don't have exact documentation of my proposed solution, but you can look at our @prismicio/client technical reference article to apply pagination: @prismicio/client Technical Reference - Prismic

Let me know if you have any further questions.