Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Next.js - How to Make a Route Based on a GROQ Query

26 replies
Last updated: Jun 20, 2022
Hi, I'm making a project with Next.js and sanity. I want to make a route based on a groq query, but it is nested and not possible with nextjs. How would I solve this problem? THe params cannot be nested I think
export async function getStaticPaths() {
  const paths = await client.fetch(
    `*[_type == "personen"]{
    slug,
    Projecten[] -> {
    'params': {
      'projectSlug': slug.current 
      }
    }
  } `
  );

  return {
    paths,
    fallback: true,
  };
}
Jun 20, 2022, 11:49 AM
THis is the error message that I get
Jun 20, 2022, 12:40 PM
That‘s because you’re returning incorrect data as the
paths
value. Are you trying to return all the project paths?
Jun 20, 2022, 1:25 PM
no only the current one
Jun 20, 2022, 1:26 PM
You might be mixing
getStaticProps
and
getStaticPaths
.
getStaticPaths
is used to determine all the paths matching that route. So that will be all the projects.
getStaticProps
is used to determine what data to render for each path.
Jun 20, 2022, 1:28 PM
When I click on the reference of Project[0] a new page has to open with the current project
Jun 20, 2022, 1:28 PM
Right. And then you’re trying to have a page per project on your frontend, right?
Jun 20, 2022, 1:29 PM
yes
Jun 20, 2022, 1:29 PM
I have a page where I get a overview of all three projects (based on the person’s name, in this case Raquel) and when I click on one project I have to go to that current project what is clicked on
Jun 20, 2022, 1:30 PM
Right, so for your project page itself,
getStaticPaths
needs to query all projects and return their slug.
Jun 20, 2022, 1:32 PM
const projects = await client.fetch(
  `*[_type == "projecten"] { "slug": slug.current }`
);
const paths = projects.map(project => ({ params: { slug: project.slug } }))

return { paths, fallback: true }
Jun 20, 2022, 1:32 PM
Yes but it is based on the person right, because it is from a reference from the person?
Jun 20, 2022, 1:33 PM
Not for your project route no.
/pages/projectDetail/[projectSlug].js
is not about a person. It’s the page for a project. Regardless of who it belongs to.
Jun 20, 2022, 1:34 PM
oh okay, so when I click on example project 1 from the projectOverview page where I preview/display all the three the projects I go to that page, and it doesn’t matter from what person it is?
Jun 20, 2022, 1:36 PM
Exactly. Because you go to the page for that project, so at this stage it doesn’t matter whether you come from a person’s page, the home page, a Google search, Twitter, etc.
Jun 20, 2022, 1:38 PM
Error: A required parameter (projectSlug) was not provided as a string in getStaticPaths for /projectDetail/[projectSlug]
I get this error message when I use the this in my getStaticProps?

  const projects = await client.fetch(
    `*[_type == "projectDetail"] { "projectSlug": slug.current }`
  );
  const paths = projects.map((project) => ({ params: { projectSlug: project.slug } }));

  return { paths, fallback: true };
Jun 20, 2022, 1:45 PM
If you call your property
projectSlug
in the query, you need to read it as
project.projectSlug
in the map. Right now you’re reading
project.slug
which is undefined.
Jun 20, 2022, 1:46 PM
alright thanks a lot! 🙂
Jun 20, 2022, 1:47 PM
the route is working! But it is not linked with the projectOverview. These are the paths in the projectOverview, how can I fix this?
export async function getStaticPaths() {
  const paths = await client.fetch(
    `*[_type == "personen" && defined(slug.current)]{
      "params": {
        "slug": slug.current
      }
    }`
  );

  return {
    paths,
    fallback: true,
  };
}
export const getStaticProps = async ({ params: { slug } }) => {
  const query = `*[_type == "personen" && slug.current == $slug][0]{
    name,
    slug,
    Projecten[] -> {
    projectNaam,
    description,
    projectImage,
    } 
  }  
  `;

  const project = await client.fetch(query, { slug });
  return {
    props: { project },
  };
};
Jun 20, 2022, 1:53 PM
Thanks for helping me, I’ve been stuck on this for the last few days
Jun 20, 2022, 1:54 PM
I’m a little confused what you mean with "linked to the project overview.” You mean like your project overview page linking to all these project pages?
Jun 20, 2022, 1:56 PM
The projectOverview looks like this and it is based of the paths I send above. It is referenced in the query. The project overview is the list of projects from the person (in the screenshot from the sanity studio)
Jun 20, 2022, 1:59 PM
WHen I click on project 1 raquel I want to go to that project, but It can’t find that route right now I get undefined
Jun 20, 2022, 1:59 PM
The projectOverview page is based on the person, I hope you get what I mean? In the screenshot (https://sanity-io-land.slack.com/files/U03LEHK1D7E/F03LCL9E75G/screenshot_2022-06-20_at_15.27.41.png ) I only have one person, Raquel but there are more persons
Jun 20, 2022, 2:05 PM
This is what the component looks like of the projectOverview
Jun 20, 2022, 2:11 PM
I figured it out, thank you for your help!
Jun 20, 2022, 2:23 PM
Yay, amazing. 🙂
Jun 20, 2022, 2:27 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing