How to Manage a Navigation Menu in NextJS with WordPress

admin

Administrator
Staff member
I'm building a NextJS app using headless WordPress with GraphQL. It's not clear from the documentation where I should be calling the query to create the site navigation.
<a href="https://github.com/lfades/next.js/tree/examples/cms-wordpress/examples/cms-wordpress" rel="noreferrer">https://github.com/lfades/next.js/tree/examples/cms-wordpress/examples/cms-wordpress</a>
The navigation is controlled dynamically by WordPress Menus (
Code:
Appearance &gt; Menus
) on the backend and I can successfully access these
Code:
menuItems
via GraphQL without any issue on the
Code:
index.js
and
Code:
posts/[slug].js
page templates in Next JS.
Code:
// index.js
export default function Index({ primaryMenu = [] }) {
   return (
     &lt;Layout&gt;
         &lt;Header&gt; 
          {primaryMenu.map((item, index) =&gt; {
             return (&lt;a href={item.url}&gt;{item.label}&lt;/a&gt;)
          )}
         &lt;/Header&gt;
     &lt;/Layout&gt;
   );

}

export async function getStaticProps() {
  const primaryMenu = await getPrimaryMenu(); // Get menu via GraphQL
  return {
    props: { primaryMenu },
  };
}
The issue I'm having with this is I am repeating the
Code:
getStaticProps
function on each template and I should be able to use some sort of global query for this, either in the
Code:
&lt;header/&gt;
component itself or another method. I'm unable to find documentation on how to do this and it doesn't work in components.
Any guidance (or examples) on where a global query such as a dynamic Navigation query would live in a NextJS app is appreciated.