👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Discussion on getting the slug of the most recent blog article for a button in a Navbar component in Next.js using Sanity.io.

26 replies
Last updated: Nov 26, 2021
Now my next question is trying to get the slug of the most recent blog article. I want to create a button that when clicked will always take the user to the most recent blog. Not sure what the query would be for this but so far I have
const query = groq `*[_type == "post"][0]{
    "slug": slug->current
}`;
and in my navbar component I'm passing in props which looks like

const Navbar = (props) => {
    const { slug } = props

    return ( some other code...)
}

Nov 25, 2021, 11:29 PM
Try:
"slug": slug.current


->
is for resolving references, which I guess isn’t the case here. The slug is an object that looks like this:
{
  slug: {
    current: 'a-slug'
  }
} 

Nov 26, 2021, 12:47 AM
that returns undefined, might be part of the way I'm passing it into my component?
Nov 26, 2021, 12:52 AM
might be. I’d console.log out that you’re at least getting the correct data
Nov 26, 2021, 12:56 AM
Thats what I'm doing, in my component, I'm using console log to log the slug of props and I get Undefined
Nov 26, 2021, 12:57 AM
Is there a better way to achieve what I'm trying?
Nov 26, 2021, 1:01 AM
It’s hard to say if it’s because the data isn’t there or if something happens on the way into the Navbar component. I’d start by confirming that you indeed get all the data you need where you run the
client.fetch
call.
Nov 26, 2021, 1:09 AM
Sorry to ask such amateur questions but how would I do that if my fetch is in an await function?
Navbar.getInitialProps = async function(context) {
    const { slug = "" } = context.query
    return await client.fetch(query, { slug })
}
Nov 26, 2021, 1:12 AM
No worries – we’re all here to learn!
Three things:
1.
getInitialProps
 is considered legacy in Next.js now, you should look at
getServerSideProps
 or `getStaticProps`/`getStaticPaths` instead. Here’s an example with Sanity .2. Is this code that’s located inside of
pages
? The name
Navbar
 hints that it might be a non-page component?3. You can change the code slightly so it’s easier to debug. Note that this will be logged in your terminal (not sure if Next puts it out in the browser as well)

Navbar.getInitialProps = async function(context) {
    const { slug = "" } = context.query
    const post = await client.fetch(query, { slug })
    console.log('post', JSON.stringify(post, null, 2))
    return post
}
Nov 26, 2021, 1:17 AM
(I highly recommend either going through the Learn Next.js docs, or watching Kap’s intro on FreeCodeCamp btw)
Nov 26, 2021, 1:19 AM
okay thanks, the Navbar is just a component piece that I'm sending to my layout so that I can have Navbar on every page without having to write it more than once
Nov 26, 2021, 1:19 AM
There you got it. Only components inside
pages
will actually run the
getInitialProps
function. That’s why you aren’t getting any data from it.
Nov 26, 2021, 1:19 AM
“Global data” has been a pain point with Next.js. They’re working on support for React server components which will make what you’re trying to do work.
Nov 26, 2021, 1:22 AM
Oh interesting, good to know. So this isnt really possible in a component the way I'm trying to do it. Unless of course I write the navbar on every page
Nov 26, 2021, 1:23 AM
Also 1 other question if I need to use getServerSideProps this tutorial is out of date and I'd have to do
return {
Nov 26, 2021, 1:29 AM
return {
   props: {
      title: post.title,
      body: post.body,
      image: post.mainImage
   }
}
Nov 26, 2021, 1:30 AM
yeah, we should really update that guide. And yes, you need to return the data under the
props
key 👍
Nov 26, 2021, 1:31 AM
lol thats what I had then the other night someone told me to use the updated version of sanity/next relation so I thought following a guide from you would be better than YouTube, oh well haha
Nov 26, 2021, 1:34 AM
I’ll start by adding a disclaimer to the guide 😅
Nov 26, 2021, 1:37 AM
Okay haha now I'm real confused because I was using
const url = `https://.<http://api.sanity.io/v1/data/query/production?query=${query}`;|api.sanity.io/v1/data/query/production?query=${query}`;>

const result = await fetch(url).then(res => res.json());
const post = result.result[0];
Should I still use this just with my new query?
Nov 26, 2021, 1:39 AM
I’d replace this with
client.fetch
like you have in earlier code snippet you pasted
Nov 26, 2021, 1:41 AM
gotcha thank you
Nov 26, 2021, 1:41 AM
but what props would I return because I was using the const post for post.title
Nov 26, 2021, 1:43 AM
Not sure what context you’re in 🙂
Nov 26, 2021, 1:45 AM
lol all good I'll keep working thanks for the help
Nov 26, 2021, 1:47 AM
I’ve updated the guide to use
getStaticProps
 and
getStaticPaths
 now 🙂
Nov 26, 2021, 1:59 AM
haha great thank you
Nov 26, 2021, 2:30 AM

Sanity– build remarkable experiences at scale

Sanity is a 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.

Was this answer helpful?