close
  • English
  • Multi version

    Rspress's default theme supports multi-version document management. Next, we will introduce how to access multi-version documents.

    multiVersion config

    Configure the version list and default version through multiVersion, for example:

    rspress.config.ts
    import { defineConfig } from '@rspress/core';
    
    export default defineConfig({
      multiVersion: {
        default: 'v1',
        versions: ['v1', 'v2'],
      },
    });

    Here, default is the default version, and versions is the version list.

    Adding multi-version documents

    According to the version list you configured, add multi-version documents under the docs directory, for example:

    docs
    v1
    index.mdx
    guide
    index.mdx
    v2
    index.mdx
    guide
    index.mdx

    In Rspress's conventional routing, for the default version, the version path prefix will be automatically omitted. For example, v1/index.mdx will be rendered as the / route, while v2/index.mdx will be rendered as the /v2/ route.

    Tip

    For links in the document, you do not need to manually add the version prefix. Rspress will automatically add the corresponding version prefix according to the version of the current document. For example, the link /guide/ in v2/index.mdx will be rendered as /v2/guide/.

    Using with i18n

    Multi-version can be used together with internationalization. When both are enabled, the directory structure uses version as the top level, then language subdirectories within each version:

    rspress.config.ts
    import { defineConfig } from '@rspress/core';
    
    export default defineConfig({
      lang: 'en',
      locales: [
        {
          lang: 'en',
          label: 'English',
        },
        {
          lang: 'zh',
          label: '简体中文',
        },
      ],
      multiVersion: {
        default: 'v1',
        versions: ['v1', 'v2'],
      },
    });

    The document directory structure should be organized as follows:

    docs
    v1
    en
    _nav.json
    index.md
    guide
    index.md
    zh
    _nav.json
    index.md
    guide
    index.md
    v2
    en
    _nav.json
    index.md
    guide
    index.md
    zh
    _nav.json
    index.md
    guide
    index.md

    The generated routes follow the pattern /{version}/{lang}/:

    File pathRoute
    v1/en/index.md/ (default version + default language, both prefixes omitted)
    v1/zh/index.md/zh/
    v2/en/index.md/v2/
    v2/zh/index.md/v2/zh/

    Get the current version in components

    In components, you can get the current version through useVersion, for example:

    import { useVersion } from '@rspress/core/runtime';
    
    export default () => {
      const version = useVersion();
      return <div>Current version: {version}</div>;
    };

    By default, search.versioned is true, which means the search will only query the index corresponding to the currently selected version. If you want to search across all versions, you can set it to false:

    rspress.config.ts
    import { defineConfig } from '@rspress/core';
    
    export default defineConfig({
      multiVersion: {
        default: 'v1',
        versions: ['v1', 'v2'],
      },
      search: {
        versioned: false,
      },
    });