✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Best practice for creating internal links with unique slugs in Sanity and Next.js

8 replies
Last updated: Sep 1, 2021
I am using Sanity with Nextjs.Let say that i have pages, blogPage and authorPage.
Then i want to create internalLink. I am creating references to those 3 types.
how should i use slug i am getting in correct way?
I do not want to omit /blog/ or /author/ . Page type can also be /whatever

<NextLink href={slug}>{children}</ NextLink> 
Aug 20, 2021, 1:30 PM
Hei!Yes and no.
In simple case i just query for slug and pass it to `&lt;NextLink href={
${props.mark.slug}
}&gt;` like that.Let say that
slug = "blog-name"
Some “magic” is happening and Next takes it to
/blog/blog-name
.First i was thinking that it will be best to query for type to and do something like that
`&lt;NextLink href={
${props.mark.type}/${props.mark.slug}
}&gt;`but “magic” is happening and it takes me to
/blog/blog/blog-name
or
/author/author-name
So if i will make sure that slugs er unik thats works.

I have dose edge cases.
Where
slug = "blog"
or
slug = "author"
but i wish to navigate to
/blog
or
/author
page.
The solution i came with is to use
validation and check if slug is unik
I am looking for best practice solution. Please give me some feedback
🙂
Aug 28, 2021, 8:57 AM
Hei!Yes and no.
In simple case i just query for slug and pass it to `&lt;NextLink href={
${props.mark.slug}
}&gt;` like that.Let say that
slug = "blog-name"
Some “magic” is happening and Next takes it to
/blog/blog-name
.First i was thinking that it will be best to query for type to and do something like that
`&lt;NextLink href={
${props.mark.type}/${props.mark.slug}
}&gt;`but “magic” is happening and it takes me to
/blog/blog/blog-name
or
/author/author-name
So if i will make sure that slugs er unik thats works.

I have dose edge cases.
Where
slug = "blog"
or
slug = "author"
but i wish to navigate to
/blog
or
/author
page.
The solution i came with is to use
validation and check if slug is unik
I am looking for best practice solution. Please give me some feedback
🙂
Aug 28, 2021, 8:57 AM
Ah, interesting! What does your file structure under
/pages
look like? I think that's where it's getting that extra
/blog/.
Aug 31, 2021, 8:20 PM
Ah, interesting! What does your file structure under
/pages
look like? I think that's where it's getting that extra
/blog/.
Aug 31, 2021, 8:20 PM
The interesting ting is that i do not have any of those problems any more. And i did not do nothing.pages/blog/[slug].tsx /index.tsx is the structure.
But i hope that `&lt;NextLink href={
${props.mark.type}/${props.mark.slug}
}&gt;` is the right way to go?
Aug 31, 2021, 8:45 PM
Looks good! Strange that it suddenly started working, though!
Aug 31, 2021, 8:46 PM
Thanks Racheal
Sep 1, 2021, 6:43 AM
Thanks Racheal
Sep 1, 2021, 6:43 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?