Create consumable navigation with a simple and straightforward visual builder
Create consumable navigation with a simple and straightforward visual builder
Strapi Navigation Plugin provides a website navigation / menu builder feature for Strapi Headless CMS admin panel. Navigation has the possibility to control the audience and can be consumed by the website with different output structure renderers:
As a ✅ verified plugin by Strapi team we're available on the Strapi Marketplace as well as In-App Marketplace where you can follow the installation instructions.
It's recommended to use yarn to install this plugin within your Strapi project. You can install yarn with these docs.
yarn add strapi-plugin-navigation@latest
After successful installation you've to re-build your Strapi instance. To archive that simply use:
yarn build
yarn develop
The UI Navigation plugin should appear in the Plugins section of Strapi sidebar after you run app again.
You can manage your multiple navigation containers by going to the Navigation manage view by clicking "Manage" button.
As a next step you must configure your the plugin by the way you want to. See Configuration section.
All done. Enjoy 🎉
Complete installation requirements are exact same as for Strapi itself and can be found in the documentation under Installation Requirements.
Supported Strapi versions:
This plugin is designed for Strapi v5 and is not working with v4.x. To get version for Strapi v4 install version v4.x.
We recommend always using the latest version of Strapi to start your new projects.
To start your journey with Navigation plugin you must first setup it using the dedicated Settings page or for any version, put your configuration in config/plugins.{js|ts}
. Anyway we're recommending the click-through option where your configuration is going to be properly validated.
On the dedicated page, you will be able to set up all crucial properties which drive the plugin and customize each individual collection for which Navigation plugin should be enabled.
Note The default configuration for your plugin is fetched from
config/plugins.js
or, if the file is not there, directly from the plugin itself. If you would like to customize the default state to which you might revert, please follow the next section.
Config for this plugin is stored as a part of the config/plugins.{js|ts}
or config/<env>/plugins.{js|ts}
file. You can use the following snippet to make sure that the config structure is correct. If you've got already configurations for other plugins stores by this way, you can use the navigation
along with them.
1 module.exports = ({ env }) => ({
2 // ...
3 navigation: {
4 enabled: true,
5 config: {
6 additionalFields: ['audience', { name: 'my_custom_field', type: 'boolean', label: 'My custom field' }],
7 contentTypes: ['api::page.page'],
8 contentTypesNameFields: {
9 'api::page.page': ['title']
10 },
11 pathDefaultFields: {
12 'api::page.page': ['slug']
13 },
14 allowedLevels: 2,
15 gql: {...},
16 }
17 }
18 });
additionalFields
- Additional fields for navigation items. More here allowedLevels
- Maximum level for which you're able to mark item as "Menu attached"contentTypes
- UIDs of related content typescontentTypesNameFields
- Definition of content type title fields like 'api::<collection name>.<content type name>': ['field_name_1', 'field_name_2']
, if not set titles are pulled from fields like ['title', 'subject', 'name']
. TIP - Proper content type uid you can find in the URL of Content Manager where you're managing relevant entities like: admin/content-manager/collectionType/< THE UID HERE >?page=1&pageSize=10&sort=Title:ASC&plugins[i18n][locale]=en
pathDefaultFields
- The attribute to copy the default path from per content type. Syntax: 'api::<collection name>.<content type name>': ['url_slug', 'path']
gql
- If you're using GraphQL that's the right place to put all necessary settings. More here i18nEnabled
- should you want to manage multi-locale content via navigation set this value Enabled
. More here cascadeMenuAttached
- If you don't want "Menu attached" to cascade on child items set this value Disabled
.It is advised to configure additional fields through the plugin's Settings Page. There you can find the table of custom fields and toggle input for the audience field. When enabled, the audience field can be customized through the content manager. Custom fields can be added, edited, toggled, and removed with the use of the table provided on the Settings Page. When removing custom fields be advised that their values in navigation items will be lost. Disabling the custom fields will not affect the data and can be done with no consequence of loosing information.
Creating configuration for additional fields with the config.(js|ts)
file should be done with caution. Config object contains the additionalFields
property of type Array<CustomField | 'audience'>
, where CustomField is of type { type: 'string' | 'boolean' | { "name": string, "url": string, "mime": string, "width": number, "height": number, "previewUrl": string }, name: string, label: string }
. When creating custom fields be advised that the name
property has to be unique. When editing a custom field it is advised not to edit its name
and type
properties. After config has been restored the custom fields that are not present in config.js
file will be deleted and their values in navigation items will be lost.
Using navigation with GraphQL requires both plugins to be installed and working. You can find installation guide for GraphQL plugin here. To properly configure GQL to work with navigation you should provide gql
prop. This should contain union types that will be used to define GQL response format for your data while fetching:
Important! If you're using
config/plugins.js
to configure your plugins , please putnavigation
property beforegraphql
. Otherwise types are not going to be properly added to GraphQL Schema. That's because of dynamic types which base on plugin configuration which are added onbootstrap
stage, notregister
. This is not valid if you're usinggraphql
plugin without any custom configuration, so most of cases in real.
1master: Int
2items: [NavigationItem]
3related: NavigationRelated
This prop should look as follows:
1gql: {
2 navigationItemRelated: ['<your GQL related content types>'],
3},
for example:
1gql: {
2 navigationItemRelated: ['Page', 'UploadFile'],
3},
where Page
and UploadFile
are your type names for the Content Types you're referring by navigation items relations.
Fully integrated and follows the official i18n Strapi patterns.
Plugin provides granular permissions based on Strapi RBAC functionality within the editorial interface & Admin API. Those settings are editable via the Setings -> Administration Panel -> Roles.
For any role different than Super Admin, to access the Navigation panel you must set following permissions:
Is applied for Public API both for REST and GraphQL. You can manage is by two different ways. Those settings are editable via the Setings -> Users & Permissions Plugin -> Roles.
Bearer <token>
.Note: Token usage & Read-Only tokens If you're aiming to use token based approach, for every call you must provide proper token in headers as
Bearer <token>
.Important: As the Read-Only tokens are dedicated to support just
find
andfindAll
endpoints from Strapi Content API, they are not covering access to plugin Public APIrender
andrenderChild
endpoints. We recommend to use theCustom
token type for fully granural and secured approach instead ofFull Access
ones.Reference: Strapi - API Tokens
1{
2 "id": 1,
3 "documentId": "njx99iv4p4txuqp307ye8625",
4 "title": "News",
5 "type": "INTERNAL",
6 "path": "news",
7 "externalPath": null,
8 "uiRouterKey": "News",
9 "menuAttached": false,
10 "parent": 8, // Parent Navigation Item 'id', null in case of root level
11 "master": 1, // Navigation 'id'
12 "createdAt": "2020-09-29T13:29:19.086Z",
13 "updatedAt": "2020-09-29T13:29:19.128Z",
14 "related": {/*<Content Type model >*/ },
15 "audience": []
16}
1{
2 "title": "News",
3 "menuAttached": true,
4 "path": "/news",
5 "type": "INTERNAL",
6 "uiRouterKey": "news",
7 "slug": "benefits",
8 "external": false,
9 "related": {
10 // <Content Type model >
11 },
12 "items": [
13 {
14 "title": "External url",
15 "menuAttached": true,
16 "path": "http://example.com",
17 "type": "EXTERNAL",
18 "uiRouterKey": "generic",
19 "external": true
20 },
21 // < Tree Navigation Item models >
22 ]
23}
1{
2 "id": "News",
3 "title": "News",
4 "related": {
5 "contentType": "page",
6 "collectionName": "pages",
7 "id": 1
8 },
9 "path": "/news",
10 "slug": "news",
11 "parent": null, // Parent Navigation Item 'id', null in case of root level
12 "menuAttached": true
13}
Plugin supports both REST API and GraphQL API exposed by Strapi.
Query Params
navigationIdOrSlug
- ID or slug for which your navigation structure is generated like for REST API:
https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625
https://localhost:1337/api/navigation/render/main-menu
type
- Enum value representing structure type of returned navigation:
https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?type=FLAT
menu
(menuOnly
for GQL) - Boolean value for querying only navigation items that are attached to menu should be rendered eg.
https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?menu=true
path
- String value for querying navigation items by its path:
https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?path=/home/about-us
GET <host>/api/navigation/?locale=<locale>&orderBy=<orderBy>&orderDirection=<orderDirection>
NOTE: All params are optional
Example URL: https://localhost:1337/api/navigation?locale=en
Example response body
1[
2 {
3 "id": 383,
4 "documentId": "njx99iv4p4txuqp307ye8625",
5 "name": "Floor",
6 "slug": "floor-pl",
7 "visible": true,
8 "createdAt": "2023-09-29T12:45:54.399Z",
9 "updatedAt": "2023-09-29T13:44:08.702Z",
10 "locale": "pl"
11 },
12 {
13 "id": 384,
14 "documentId": "njx99iv4p4txuqp307ye8625",
15 "name": "Floor",
16 "slug": "floor-fr",
17 "visible": true,
18 "createdAt": "2023-09-29T12:45:54.399Z",
19 "updatedAt": "2023-09-29T13:44:08.725Z",
20 "locale": "fr"
21 },
22 {
23 "id": 382,
24 "documentId": "njx99iv4p4txuqp307ye8625",
25 "name": "Floor",
26 "slug": "floor",
27 "visible": true,
28 "createdAt": "2023-09-29T12:45:54.173Z",
29 "updatedAt": "2023-09-29T13:44:08.747Z",
30 "locale": "en"
31 },
32 {
33 "id": 374,
34 "documentId": "njx99iv4p4txuqp307ye8625",
35 "name": "Main navigation",
36 "slug": "main-navigation-pl",
37 "visible": true,
38 "createdAt": "2023-09-29T12:22:30.373Z",
39 "updatedAt": "2023-09-29T13:44:08.631Z",
40 "locale": "pl"
41 },
42 {
43 "id": 375,
44 "documentId": "njx99iv4p4txuqp307ye8625",
45 "name": "Main navigation",
46 "slug": "main-navigation-fr",
47 "visible": true,
48 "createdAt": "2023-09-29T12:22:30.373Z",
49 "updatedAt": "2023-09-29T13:44:08.658Z",
50 "locale": "fr"
51 },
52 {
53 "id": 373,
54 "documentId": "njx99iv4p4txuqp307ye8625",
55 "name": "Main navigation",
56 "slug": "main-navigation",
57 "visible": true,
58 "createdAt": "2023-09-29T12:22:30.356Z",
59 "updatedAt": "2023-09-29T13:44:08.680Z",
60 "locale": "en"
61 }
62]
GET <host>/api/navigation/render/<navigationIdOrSlug>?type=<type>
Return a rendered navigation structure depends on passed type (TREE
, RFR
or nothing to render as FLAT
).
Example URL: https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625
Example response body
1[
2 {
3 "id": 1,
4 "documentId": "njx99iv4p4txuqp307ye8625",
5 "title": "News",
6 "type": "INTERNAL",
7 "path": "news",
8 "externalPath": null,
9 "uiRouterKey": "News",
10 "menuAttached": false,
11 "parent": null,
12 "master": 1,
13 "created_at": "2020-09-29T13:29:19.086Z",
14 "updated_at": "2020-09-29T13:29:19.128Z",
15 "related": {
16 "__contentType": "Page",
17 "id": 1,
18 "documentId": "njx99iv4p4txuqp307ye8625",
19 "title": "News",
20 // ...
21 }
22 },
23 // ...
24]
Example URL: https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?type=TREE
Example response body
1[
2 {
3 "title": "News",
4 "menuAttached": true,
5 "path": "/news",
6 "type": "INTERNAL",
7 "uiRouterKey": "news",
8 "slug": "benefits",
9 "external": false,
10 "related": {
11 "__contentType": "Page",
12 "id": 1,
13 "title": "News",
14 // ...
15 },
16 "items": [
17 {
18 "title": "External url",
19 "menuAttached": true,
20 "path": "http://example.com",
21 "type": "EXTERNAL",
22 "uiRouterKey": "generic",
23 "external": true
24 },
25 // ...
26 ]
27 },
28 // ...
29]
Example URL: https://localhost:1337/api/navigation/render/njx99iv4p4txuqp307ye8625?type=RFR
Example response body
1{
2 "pages": {
3 "News": {
4 "id": "News",
5 "title": "News",
6 "related": {
7 "contentType": "page",
8 "collectionName": "pages",
9 "id": 1
10 },
11 "path": "/news",
12 "slug": "news",
13 "parent": null,
14 "menuAttached": true
15 },
16 "Community": {
17 "id": "Community",
18 "title": "Community",
19 "related": {
20 "contentType": "page",
21 "collectionName": "pages",
22 "id": 2
23 },
24 "path": "/community",
25 "slug": "community",
26 "parent": null,
27 "menuAttached": true
28 },
29 "Highlights": {
30 "id": "Highlights",
31 "title": "Highlights",
32 "related": {
33 "contentType": "page",
34 "collectionName": "pages",
35 "id": 3
36 },
37 "path": "/community/highlights",
38 "slug": "community-highlights",
39 "parent": "Community",
40 "menuAttached": false
41 },
42 // ...
43 },
44 "nav": {
45 "root": [
46 {
47 "label": "News",
48 "type": "internal",
49 "page": "News"
50 },
51 {
52 "label": "Community",
53 "type": "internal",
54 "page": "Community"
55 },
56 {
57 "label": "External url",
58 "type": "external",
59 "url": "http://example.com"
60 },
61 // ...
62 ],
63 "Community": [
64 {
65 "label": "Highlights",
66 "type": "internal",
67 "page": "Highlights"
68 },
69 // ...
70 ],
71 // ...
72 }
73}
Same as REST API returns a rendered navigation structure depends on passed type (TREE
, RFR
or nothing to render as FLAT
).
Example request
1query {
2 renderNavigation(
3 navigationIdOrSlug: "main-navigation"
4 type: TREE
5 menuOnly: false
6 ) {
7 id
8 title
9 path
10 related {
11 id
12 __typename
13
14 ... on Page {
15 Title
16 }
17
18 ... on WithFlowType {
19 Name
20 }
21 }
22 items {
23 id
24 title
25 path
26 related {
27 id
28 __typename
29
30 ... on Page {
31 Title
32 }
33
34 ... on WithFlowType {
35 Name
36 }
37 }
38 }
39 }
40}
Example response
1{
2 "data": {
3 "renderNavigation": [
4 {
5 "id": 8,
6 "title": "Test page",
7 "path": "/test-path",
8 "related": {
9 "id": 3,
10 "__typename": "WithFlowType",
11 "Name": "Test"
12 },
13 "items": [
14 {
15 "id": 11,
16 "title": "Nested",
17 "path": "/test-path/nested-one",
18 "related": {
19 "id": 1,
20 "__typename": "Page",
21 "Title": "Eg. Page title"
22 }
23 }
24 ]
25 },
26 {
27 "id": 10,
28 "title": "Another page",
29 "path": "/another",
30 "related": {
31 "__typename": "Page",
32 "Title": "Eg. Page title"
33 },
34 "items": []
35 }
36 ]
37 }
38}
Slug generation is available as a controller and service. If you have custom requirements outside of what this plugin provides you can add your own logic with plugins extensions.
For example:
1// path: /admin/src/index.js
2
3module.exports = {
4 // ...
5 bootstrap({ strapi }) {
6 const navigationCommonService = strapi.plugin("navigation").service("common");
7 const originalGetSlug = navigationCommonService.getSlug;
8 const preprocess = (q) => {
9 return q + "suffix";
10 };
11
12 navigationCommonService.getSlug = (query) => {
13 return originalGetSlug(preprocess(query));
14 };
15 },
16};
Navigation plugin allows to register lifecycle hooks for Navigation
and NavigationItem
content types.
You can read more about lifecycle hooks here. (You can set a listener for all of the hooks).
Lifecycle hooks can be register either in register()
or bootstrap()
methods of your server. You can register more than one listener for a specified lifecycle hook. For example: you want to do three things on navigation item creation and do not want to handle all of these actions in one big function. You can split logic in as many listeners as you want.
Listeners can by sync and async
.
Be aware that lifecycle hooks registered in
register()
may be fired by plugin's bootstrapping. If you want listen to events triggered after server's startup usebootstrap()
.
Example:
1 const navigationCommonService = strapi
2 .plugin("navigation")
3 .service("common");
4
5 navigationCommonService.registerLifecycleHook({
6 callback: async ({ action, result }) => {
7 const saveResult = await logIntoSystem(action, result);
8
9 console.log(saveResult);
10 },
11 contentTypeName: "navigation",
12 hookName: "afterCreate",
13 });
14
15 navigationCommonService.registerLifecycleHook({
16 callback: async ({ action, result }) => {
17 const saveResult = await logIntoSystem(action, result);
18
19 console.log(saveResult);
20 },
21 contentTypeName: "navigation-item",
22 hookName: "afterCreate",
23 });
Note: Yet using the
4.x
compatible version of the plugin. Integration migration expected once the maintenance team release their5.x
compatible version.
If your strapi server uses REST Cache plugin this plugin can take integrate with it. All you need to do is to enable it in configuration of Navigation plugin. After integration is enabled all client calls will be wrapped with caching middleware.
In admin panel new controls will be available. Cache clearing is done manually or after cache will timeout(rest-cache
plugin's settings are used).
Navigation edit screen will have "Clear cache" button.
Navigation management modal items will also have icon button for clearing the cache.
Live example of plugin usage can be found in the VirtusLab Strapi Examples repository.
Q: I would like to use GraphQL schemas but I'm not getting renderNavigation
query or even proper types as Navigation, NavigationItem etc. What should I do?
A: There is a one trick you might try. Strapi by default is ordering plugins by the way which takes strapi-plugin-graphql
to initialize earlier than other plugins so types might not be injected. If you don't have it yet, please create config/plugins.{js|ts}
file and put there following lines (put graphql
at the end):
1module.exports = {
2 'navigation': { enabled: true },
3 'graphql': { enabled: true },
4};
If you already got it, make sure that navigation
plugin is inserted before graphql
. That should do the job.
Feel free to fork and make a Pull Request to this plugin project. All the input is warmly welcome!
Clone repository
1git clone git@github.com:VirtusLab-Open-Source/strapi-plugin-navigation.git
Run install
& watch:link
command
1// Install all dependencies
2yarn install
3
4// Watch for file changes using `plugin-sdk` and follow the instructions provided by this official Strapi developer tool
5yarn watch:link
Within the Strapi project, modify config/plugins.{js|ts}
for imgix
1//...
2'navigation': {
3 enabled: true,
4 //...
5}
6//...
For general help using Strapi, please refer to the official Strapi documentation. For additional help, you can use one of these channels to ask a question:
[VirtusLab]
prefix and DM.MIT License Copyright (c) VirtusLab Sp. z o.o. & Strapi Solutions.
npm install strapi-plugin-navigation
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.