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

How to Add Dynamic Navigation Menus to a Gatsby Page Template

5 replies
Last updated: Sep 3, 2021
are there any examples out there or ways to add dynamic nav menus created in studio to a gatsby page template? It seems everything in gatsby 'page-template-land' is sort of partitioned off from the dynamic data from pages created in the sanity content lake, for example in the kitchen sink demo, the 'blog' portion of the site, theres no nav bar. Would there be a way to query for this and inject a default nav bar (if exists) into a static page template? or am i thinking about this the wrong way
Sep 1, 2021, 7:04 AM
i think one way i can solve this is just move a default 'nav bar' into my site settings and just pull that into my header. The only issue i see is how to then handle static links to pages created statically (landing pages like /blog and /frontpage)
Sep 1, 2021, 2:05 PM
nevermind, a 'route' link to my static page route will solve this
Sep 1, 2021, 2:24 PM
Not sure I’m understanding correctly, and I see you’ve found a solution, but wanted to point you to layout components in Gatsby which is basically how to create a Layout component to wrap all your templates and pages in so you can add things like the nav bar, footer, etc. to it and have it on every page/view, plus the benefit of not unmounting/mounting those comps on page load.
Then in the nav bar comp you’d use a graphql static query to pull in the navigation data from a schema that could, for example, be an array of references to all your “page” type, so you can return the current slugs and titles as nav items and have the ability to automatically route directly to pages you’ve made from the studio. I actually just wrote a navigation schema that allows the user to add/remove/edit both singleton and dropdown nav items, but it technically belongs to the agency I work for at this point so can’t post it here for you. Just wanted to explain how I usually approach navigation/routing schemas with Gatsby and Sanity!
Sep 2, 2021, 5:23 PM
Thanks very much tyler, yes my layout component is what i ended up modifying for this, it was an issue of me not fully understanding the sanity generated routes that are created in studio, with the static ones that are defined in gatsby page templates. And what i ended up doing is in my schema, just pushing my navBar up to the higher level siteSettings for my site, then on my graphQl queries, changing my siteSettings queries to pull down navBar for each page. This would reduce a page from being able to have its own nav bar and instead move to a model where the entire site has a universal nav bar as defined in siteSettings, but for my useCase that is all that is needed.
Sep 3, 2021, 12:17 AM
Thanks very much tyler, yes my layout component is what i ended up modifying for this, it was an issue of me not fully understanding the sanity generated routes that are created in studio, with the static ones that are defined in gatsby page templates. And what i ended up doing is in my schema, just pushing my navBar up to the higher level siteSettings for my site, then on my graphQl queries, changing my siteSettings queries to pull down navBar for each page. This would reduce a page from being able to have its own nav bar and instead move to a model where the entire site has a universal nav bar as defined in siteSettings, but for my useCase that is all that is needed.
Sep 3, 2021, 12:17 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?