Best practice for creating internal links with unique slugs in Sanity and Next.js
8 replies
Last updated: Sep 1, 2021
M
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
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
M
Hei!Yes and no.
In simple case i just query for slug and pass it to `<NextLink href={
`<NextLink href={
I have dose edge cases.
Where
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
🙂
In simple case i just query for slug and pass it to `<NextLink href={
${props.mark.slug}}>` 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
`<NextLink href={
${props.mark.type}/${props.mark.slug}}>`but “magic” is happening and it takes me to
/blog/blog/blog-nameor
/author/author-nameSo 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
/blogor
/authorpage.
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
M
Hei!Yes and no.
In simple case i just query for slug and pass it to `<NextLink href={
`<NextLink href={
I have dose edge cases.
Where
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
🙂
In simple case i just query for slug and pass it to `<NextLink href={
${props.mark.slug}}>` 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
`<NextLink href={
${props.mark.type}/${props.mark.slug}}>`but “magic” is happening and it takes me to
/blog/blog/blog-nameor
/author/author-nameSo 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
/blogor
/authorpage.
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
/pageslook 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
/pageslook like? I think that's where it's getting that extra
/blog/.
Aug 31, 2021, 8:20 PM
M
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 `<NextLink href={
But i hope that `<NextLink href={
${props.mark.type}/${props.mark.slug}}>` is the right way to go?
Aug 31, 2021, 8:45 PM
M
Thanks Racheal
Sep 1, 2021, 6:43 AM
M
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.