🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Recommended content model for nested documents/pages in Sanity.io

5 replies
Last updated: Feb 11, 2022
Hello, I'm curious what would be the most recommended content model for nested documents/pages? For example, lets say I have a site that goes https://example.com/country/state/county/city , which then ends up being https://example.com/usa/georgia/fulton/atlanta . A user can also view content lower in the tree like https://example.com/usa/georgia , for example.
I initially thought of making a document type for each path (i.e.,
, etc.) and then creating a hierarchical structure by referencing them together under a singleton document . I've looked at the docs about connected content , but I'm still trying to figure out how to use references for this particular use case. Thanks!
Feb 10, 2022, 10:16 PM
Hey User! You could handle this through reference fields. For example, a city would reference a state which would then reference a country. You can then use GROQ on your frontend to dynamically build out the paths. Alternatively, you could use the method I described in this thread to build out the slugs in your document.
Feb 10, 2022, 10:54 PM
Thanks, User! This looks great! Quick question, are there any pros or cons between referencing from bottom to top (
references a
etc.) vs. top to bottom (
references a
and so on)?
Feb 11, 2022, 12:07 AM
References are bi-directional when you query them so, in that sense, it doesn't matter where you place it. However when it comes to the Studio UI, you'll only see the reference field in the document where you set the reference. To clarify: if you set a reference to a country in a city document, the city document would show that relationship not the country document. I'd say place it wherever will be most intuitive for your editors to look for it. Does that make sense?
Feb 11, 2022, 12:16 AM
Ahhh, I see. Thanks for clarifying. That makes sense. I guess this goes back to how User described how the bi-directional querying works. I think I know what to do now. Thanks again!
Feb 11, 2022, 12:20 AM
You're welcome!
Feb 11, 2022, 12:21 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?