Watch a live product demo 👀 See how Sanity powers richer commerce experiences

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

The Sanity Composable Content Cloud is the 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.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing