Gatsby: Dynamically-Rendered Navigation Tutorial

Created on 26 Mar 2018  路  7Comments  路  Source: gatsbyjs/gatsby

I see there's a stub for Dynamically-Rendered Navigation.

By this do you mean a nav component that keeps track of what state it is in based on the current page or state of another component? If so, I think I could work on this tutorial.

stale? documentation

Most helpful comment

I think the idea there was for navigation built automatically from the site's pages data e.g. you have a number of team member pages and whenever someone new joins, the menu is updated automatically.

All 7 comments

I think the idea there was for navigation built automatically from the site's pages data e.g. you have a number of team member pages and whenever someone new joins, the menu is updated automatically.

Any examples of this working somewhere? thanks

@imshuffling, I don't know of anyone who has built this yet, but it's something commonly available in other static site generators. Wintersmith and Jekyll are two such generators that generate navigation using template engines like nunjucks and handlebars.

@cdvillard manage to achieve what I wanted in the end. Using Contentful.

E.g I have 3 content types (yes poorly named Template 1, 2, 3...)

Rough menu structure as below.

-- Template 1
-- Template 1
-- Template 1
---- Template 2
---- Template 2
-- Template 1
---- Template 3
---- Template 3
---- Template 3

I then have a content type called "Navigation" which is a multi reference field allowing you to add Template 1 pages, this will create our top level nav.

screen shot 2018-08-08 at 11 47 45

To get the nested pages I simply have a reference field called "ChildPages" on each Template 1 page

screen shot 2018-08-08 at 11 49 53

And now the graphQL query which i have in my layout.js file

       query LayoutQuery {
         contentfulNavigation {
           navigationItem {
             menuTitle
             title
             slug
             childPages {
               ... on ContentfulTemplate2 {
                 menuTitle
                 title
                 slug
                 id
               }
               ... on ContentfulTemplate3 {
                 menuTitle
                 title
                 slug
                 id
               }
             }
           }
         }
       }

Hope this helps someone.

FYI I'm using Gatsby V2.

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

It would be pretty easy to do this with gatsby-source-filesystem. I added this to gatsby-config.js:

 {
    resolve: `gatsby-source-filesystem`,
    options: {
      path: `${__dirname}/src/pages`,
      name: 'pages',
    },
},

Then in my layout component I create a StaticQuery:

<StaticQuery
        query={graphql`
          query {
            allSitePage {
              edges {
                node {
                  path
                }
              }
            }
          }
        `}
        render={data => {
...

Then filter out the paths you don't want (like 404 and test). That's the best way I can figure out for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benstr picture benstr  路  3Comments

timbrandin picture timbrandin  路  3Comments

mikestopcontinues picture mikestopcontinues  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

signalwerk picture signalwerk  路  3Comments