Strapi Navigation Plugin

repository·master·Indexed 18 days ago

https://github.com/virtuslab-open-source/strapi-plugin-navigation

A visual builder for creating website navigation and menus within the Strapi admin panel. It supports complex nested structures, audience control, and multiple output formats (Flat, Tree, RFR) via REST and GraphQL APIs. The plugin includes features for custom additional fields, RBAC permission integration, and compatibility with strapi-plugin-rest-cache.

Tokens
18K
Snippets
58
Records
72
Agent score
62%

What's inside strapi-plugin-navigation

  1. Configure Public API Authorization Strategies

    master

    The plugin's Public API (REST and GraphQL) can be secured using two different strategies via Settings -> Users & Permissions Plugin -> Roles:

    User-based Authorization

    • Public: The default role for unauthenticated users. Enabling the plugin API here makes it fully public with no permission checks.
    • Authenticated: The default role for Strapi Users. Requires a Bearer <token> for all calls.

    Token-based Authorization

    • Full Access: Provides full access to all Strapi Content APIs and plugin endpoints.
    • Custom (Recommended): Allows granular access management for both Strapi Content API endpoints and the plugin's Public API.

    Note on Read-Only Tokens: Strapi's standard Read-Only tokens only support find and findAll endpoints for Content APIs. They do not cover the plugin's Public API render and renderChild endpoints. For secured access to these endpoints, use a Custom token type.

  2. How to contribute to the plugin

    master

    To contribute to the strapi-plugin-navigation project, fork the repository and follow these steps to set up a local development environment:

    1. Clone the repository:
      git clone git@github.com:VirtusLab-Open-Source/strapi-plugin-navigation.git
    2. Install dependencies and start the watch mode using plugin-sdk:
      yarn install
      yarn watch:link
    3. In your local Strapi project, enable the plugin by modifying config/plugins.{js|ts}:
      'navigation': {
        enabled: true,
        //...
      }
    4. Run your Strapi instance to test changes.
    git clone git@github.com:VirtusLab-Open-Source/strapi-plugin-navigation.git
    
    yarn install
    
    yarn watch:link
  3. Migrate from v2.x or v3.0.0-beta.x to v3.0.0

    master

    To migrate the strapi-plugin-navigation plugin to version 3.0.0, you must manually copy specific migration files into your project's database/migrations directory. The files required depend on your current version:

    • If you are on any 2.x version:

      • migrations/strapi-plugin-navigation-3.0.0-no-1-related-id-to-documentid.js
      • migrations/strapi-plugin-navigation-3.0.0-no-4-additional-fields.js
    • If you are on version < 3.0.0-beta.4:

      • migrations/strapi-plugin-navigation-3.0.0-no-2-locale-slug-regular-slug.js
    • If you are on version < 3.0.0-beta.6:

      • migrations/strapi-plugin-navigation-3.0.0-no-3-morph-relation.js

    After copying the files, refer to the Strapi 5 Documentation to apply the database migrations.

  4. Install the Strapi Navigation plugin

    master

    You can install the plugin via the Strapi Marketplace (verified) or via the command line using yarn. After installation, you must rebuild your Strapi instance to see the plugin in the sidebar.

    Installation steps:

    1. Install the package:
      yarn add strapi-plugin-navigation@latest
    2. Rebuild and restart Strapi:
      yarn build
      yarn develop

    Once running, the UI Navigation plugin will appear in the Plugins section of the Strapi sidebar. You can manage multiple navigation containers by clicking the "Manage" button in the Navigation view.

    yarn add strapi-plugin-navigation@latest
    
    yarn build
    yarn develop
  5. Integrate with Strapi REST Cache

    master

    If your Strapi server uses the strapi-plugin-rest-cache, you can enable integration in the Navigation plugin configuration. Once enabled, all client calls to the navigation API will be wrapped with caching middleware.

    Features in Admin Panel:

    • A "Clear cache" button will appear on the Navigation edit screen.
    • Icon buttons for clearing the cache will be available in the Navigation management modal items.
  6. Manage Admin Panel RBAC Permissions

    master

    The plugin integrates with Strapi's Role-Based Access Control (RBAC). To grant access to the Navigation panel for any role other than Super Admin, navigate to Settings -> Administration Panel -> Roles and configure the following permissions:

    • Mandatory: Plugins -> Navigation -> Read (Required to see the Navigation Panel).
    • Optional: Plugins -> Navigation -> Update (Allows changing the navigation structure).
    • Optional: Plugins -> Navigation -> Settings (Allows changing plugin settings).
  7. Configure Navigation Custom Fields

    master

    The plugin allows defining custom fields for navigation items. These fields can be of several types:

    • select: A dropdown or multi-select field. Requires options (array of strings) and a multi boolean.
    • boolean: A simple true/false field.
    • string: A text input field.
    • media: A field for selecting media assets (images, videos, etc.).

    Custom field names must not contain spaces (otherwise a noSpaceError is thrown).

    export const navigationItemCustomField = z.discriminatedUnion('type', [
      navigationItemCustomFieldPrimitive,
      navigationItemCustomFieldMedia,
      navigationItemCustomFieldSelect,
    ]);
  8. Define custom fields for navigation items

    master

    You can extend navigation items by defining additionalFields in the plugin configuration. These fields use a discriminated union based on the type property.

    Supported field types:

    Select Fields (type: 'select')

    Used for dropdowns or multi-select menus.

    • name: A unique identifier (must not contain spaces).
    • label: The display name.
    • multi: Boolean indicating if multiple selections are allowed.
    • options: An array of strings for the selection values.

    Primitive Fields (type: 'boolean' | 'string')

    Used for simple text or toggle inputs.

    • type: Either 'boolean' or 'string'.
    • name: A unique identifier (must not contain spaces).
    • label: The display name.

    Media Fields (type: 'media')

    Used for attaching media assets.

    • name: A unique identifier (must not contain spaces).
    • label: The display name.

    All custom fields support optional properties: description, placeholder, required (boolean), and enabled (boolean).

    // Example of a custom select field in additionalFields
    {
      type: 'select',
      name: 'priority_level',
      label: 'Priority',
      multi: false,
      options: ['Low', 'Medium', 'High']
    }
  9. Configure GraphQL for Navigation

    master

    To use the navigation plugin with GraphQL, you must provide a gql property in your plugin configuration. This property defines union types used to format the GraphQL response for related content.

    Critical Configuration Order: If you are using config/plugins.js, you must place the navigation property before the graphql property. Because navigation types are dynamic and added during the bootstrap stage rather than register, placing navigation after graphql will prevent the types from being correctly added to the GraphQL Schema.

    Use the navigationItemRelated key to list the names of your Content Types that are referenced via navigation item relations.

    // config/plugins.js
    module.exports = {
      navigation: {
        gql: {
          navigationItemRelated: ['Page', 'UploadFile'],
        },
      },
      graphql: {
        // ... graphql config
      },
    };
  10. Define custom additional fields for navigation items

    master

    You can extend navigation items with custom metadata using the additionalFields property. While it is recommended to use the Settings Page to manage these, you can define them in your config/plugins.{js|ts} file.

    additionalFields accepts an array containing the string 'audience' (a built-in field) or CustomField objects.

    CustomField Schema

    String/Boolean/Media types:

    {
      type: 'string' | 'boolean' | 'media';
      name: string;
      label: string;
      description?: string;
      placeholder?: string;
      required?: boolean;
      enabled?: boolean;
    }

    Select types:

    {
      type: 'select';
      name: string;
      label: string;
      description?: string;
      placeholder?: string;
      multi: boolean;
      options: string[];
      required?: boolean;
      enabled?: boolean;
    }

    Important Warnings:

    • The name property must be unique.
    • When editing a custom field, do not change the name or type properties. If you do, the plugin will treat it as a new field and delete the old one, causing loss of data in existing navigation items.
    • If you remove a field from the configuration file, its values in existing navigation items will be lost.
    // Example of additionalFields configuration
    additionalFields: [
      'audience', 
      {
        type: 'select',
        name: 'category',
        label: 'Category',
        multi: false,
        options: ['News', 'Events', 'Products'],
        required: false
      }
    ]
  11. Configure caching for navigation data via rest-cache

    master

    The strapi-plugin-navigation can automatically integrate with the rest-cache plugin to cache client-side navigation routes.

    When rest-cache is installed and enabled, the plugin sets up a cache middleware for each client route. The cache configuration uses the following defaults:

    • Path: /api/navigation{routePath} (or the specific pluginName defined in the route info).
    • Method: GET
    • Max Age: Defaults to 6 minutes (6 * 60 * 1000 ms) if not specified in the rest-cache plugin configuration.
    • Cache Keys: Includes query parameters (useQueryParams: true) and supports idOrSlug and childUIKey as parameter names.

    To ensure this works, the rest-cache plugin must be present and provide a middleware('recv') function.

    // The plugin internally calls setupCacheStrategy which performs the following logic:
    // 1. Checks if 'rest-cache' plugin is enabled.
    // 2. Retrieves the 'recv' middleware from 'rest-cache'.
    // 3. Iterates through clientRoutes and registers them with the cache middleware.
    // 4. Injects the new router into the Strapi server.