Create Dynamic Menus in Storyblok and Nuxt
Storyblok is the first headless CMS that works for developers & marketers alike.
In this part of the tutorial series, we will make the menu in our header component dynamic, so that you can manage it directly from Storyblok.
If you’re in a hurry, have a look at our live demo in Stackblitz! Alternatively, you can explore or fork the code from the Nuxt Ultimate Tutorial GitHub Repository.
Requirements
This tutorial is part 3 of the Ultimate Tutorial Series for Nuxt. We recommend that you follow the previous tutorials before starting this one.
Enter the name config
{1} and choose Content type block {2}.
Now you can create a new field with the name header_menu
{1} and choose the field type Blocks {2}.
In this field, we would like to provide the possibility to add menu links as nested blocks. To accomplish that, let’s create another new block. This time it should be a Nested block {1} with the name menu_link
{2}.
Now we can add a new field called link
in this newly created block and choose Link as the field type.
Alright, our component schemas are almost done! Just one more step: to avoid that just any block could be nested in our header_menu
, we want to make sure that only specific components can be inserted {1}. Now you can choose the menu_link
block in the whitelist {2}.
With that out of the way, we can now go to the Content section of our Storyblok space. Here, we want to create a new story with the name Config {2}, using our recently created content type Config {3}.
If you open this newly created Config story, you can now nest as many menu_link
blocks in the header_menu
field as you would like. For now, let’s add our Blog and About page.
Note that the components you put in the storyblok
folder, and will be imported dynamically using <StoryblokComponent />
, must be named with a hyphen in your Storyblok space: 'example-component' and with Pascal case in your project: 'ExampleComponent'.
Rendering the Menu in Nuxt
Having taken care of our setup in Storyblok, we can now turn to the code and implement our dynamic menu in the frontend. To accomplish that, let’s update components/Header.vue
with the following code:
If you go back to the Visual Editor now, you can see your menu being rendered correctly. Feel free to experiment a little bit with it by adding or reordering the entries and saving the Config story.
Fantastic – but what’s actually happening in the code? First of all, we’re using useStoryblokApi
to fetch the Config story without loading the Storyblok Bridge automatically. You can learn more about the differences between useAsyncStoryblok
, useStoryblokApi
and useStoryblokBridge
in the documentation on Github. What’s important to notice is that an additional parameter – resolve_links
– is passed to the apiOptions
. This is used to actually get the URLs of the stories that we link internally. You can learn more about this and other parameters in our Content Delivery API docs.
Next, a reactive object by the name of headerMenu
is created and the contents of the header_menu
field that we created earlier is stored as its value. This can then be used in the template using a simple for loop.
Wrapping Up
Congratulations, you have successfully created a dynamic menu in Storyblok and Nuxt!
Continue reading and learn How to Create Custom Components in Storyblok and Nuxt.
Resource | Link |
---|---|
Storyblok Nuxt 3 Ultimate Tutorial | https://www.storyblok.com/tp/storyblok-nuxt-ultimate-tutorial |
Storyblok Nuxt 3 Module | https://github.com/storyblok/storyblok-nuxt |
Storyblok Nuxt Technology Hub | https://www.storyblok.com/tc/nuxtjs |
Storyblok APIs | https://www.storyblok.com/docs/api |
Nuxt 3 | https://v3.nuxtjs.org/ |