Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Handling navigation for singletons in Sanity.io without slugs

5 replies
Last updated: Apr 21, 2023
I have a singleton Homepage and multiple Page type for all other pages. Each page has a slug which works great when creating an navigation array for the website. But my biggest problem is how can I set a menu Title + Link for the singleton using a slug? Same story for the Blog overview this is a singleton type too and have no slug.
I was thinking to have a slug field with a default fixed slug like "frontpage" and set it read only, so this way I can select it from a reference?

Any tips please
🙂 I am stuck at this pretty confusing.. on how to deal with this.
Home & Blog => Singleton no slugs.
Pages: About, Contact, Team etc with slugs.
Apr 21, 2023, 6:57 PM
Hi User, and i had been looking over your original post, trying to guess just what you had and knew.
First, the 'blog' entry does have a slug, 'blog', and should fit in an Internal Link item which I think you have, in at least the style of this out-of-date and overly compicated description of such a Nav scheme.

It's typical in other programming to do just as you suggest, have a known alias, 'home', etc. as the slug for a root '/' page -- think you're on the right track there.

What complicates the picture is just what the Studio might do with that alias 'home'. There was never the promised Part 3 for that article, so we don't know how the Nav menu was ever linked into Studio operation.

It's not an area I've gotten into yet in such a way, but I suspect the menu operation gets defined via the Structure Builder, doc'd beginning here:
https://www.sanity.io/docs/structure-builder-introduction
One thing that might simplify I do know, that you can often isolate to the structure area you want to affect, and just write the code for that alone, leaving the rest to default.

As it sounds like you inherited this project, you might find such things already done, in or imported into your Studio's sanity.config.js/ts file.

The shortcut I mention is demonstrated here, at this point in the overview article for adding a preview panel alongside the document editor:
https://www.sanity.io/docs/structure-builder-introduction#b4971351a508
Hope this gives you enough of a leg up to solve this, especially as it sounds you have a very suitable idea...
Apr 21, 2023, 8:25 PM
user Q
Thanks! So, I have a navigation array with internal & external links. Internal is a reference to Page & Post and external is a free to use slug input to link to external websites etc.
Each Page & Post has a slug which is required this way I can link it and get the
slug.current
(slug) of that specific document.
Now, I have created 2 singletons Blog and Home because I don't want clients to create a Page named "blog" and by accident remove it. I need the singleton Blog to display all the posts.

The homepage singleton is for the index (page) route inside my Next 13 application.

Both singletons have no slug because I am fetching the data of each inside the root index for both routes.

The problem is because I have no slug on these singletons there is no way to create a reference or connection.

So for now both have slug fields which are "ReadOnly" and for the homepage I am checking if slug is equal to rewrite to "/" else return current slug same thing for the blog.

have additional array navigation item named Singleton Link which links to Home or Blog and returns the slug of each singleton. Otherwise I can't create my navigation.

Can't come up with a better solution here.

Everything else is straight to the point, each page has a pageBuilder of sections to create and order the page a client wants to. The blog also has the pageBuilder but the Archive Overview Query of posts is always available.
Apr 21, 2023, 11:13 PM
That sounds workable, and...if it works for you 🙂
I think that when we operate within a complex system like Sanity, and can't get at operations to alter or configure them, it's an entirely sensible thing to make local workarounds, as it feels you have done.

Take satisfaction in it, and enjoy your week-end, User!

Best,
User
Apr 21, 2023, 11:17 PM
Thank you very much User. I used to overcomplicate things and trying always to have PERFECT code and clean as possible. But I realised, the more I focus on that the less my I use my brains to think outside of the box and focus on getting things done instead of focussing on getting things to look perfect. For now it works maybe in a couple of days there will be a better solution from my side for it.
Apr 21, 2023, 11:18 PM
Appreciating Italian design as I do, this feels a way to keep ît to human scale, as one may think on a Friday :)
Apr 21, 2023, 11:22 PM

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?