Join our next Developer Virtual Meetup – Live Q&A, Project Showcase, Swag raffle, and more 🔥

How to define the homepage dynamically using a custom slug in Sanity.io

3 replies
Last updated: Jun 29, 2023
I am fetching my homepage based on it's slug "frontpage" which is defined in the slug input value. But my main concern and issue is a client changes this slug, the frontpage / homepage won't work anymore. Is there anything I can do or any advice on how to define the homepage and use this defined value dynamic slug instead?
I have a settings page I was thinking having a reference to a page where you define the homepage and based on that page to fetch the data on the index page?

Are there any smarter or cleaner ways of doing this?
Jun 13, 2023, 1:52 PM
Hi (Removed Name),
You could fetch your homepage based on
_id
, which can't be changed like the slug can. The
_id
is randomly generated, but you can customize it. Here's a video that demonstrates how to do that.
Jun 13, 2023, 5:38 PM
user E
thank you!
Jun 13, 2023, 5:51 PM
I've got this setup in my Site Settings singleton and it seems to be working quite well. You have to know how to query it from the root index route in your frontend, but that'll vary by framework. I'm using Remix for what it's worth. I have a hidden flag of isFrontpage on the page document itself. And when I publish my settings (using a custom publish action), it updates (or patches) whatever page reference I've set that field to. In this case, Home. And then in my preview list for pages, I can use that hidden isFrontpage field to add a (Frontpage) label to the page that is the front-page. This way, you could change the front page from the settings to whatever page you want.
Jun 29, 2023, 5:10 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?