Getting Started with @viteplus/versions
@viteplus/versions is a powerful VitePress plugin that enables versioned documentation with seamless navigation, localization support, and automatic route generation. This guide will help you set up and configure the plugin for your documentation needs.
Community project
This is a community project and is not affiliated with or endorsed by VoidZero, Vite, VitePress, or Vite+. See #59 for the background of the name.
Upgrading from v2
Since v3 every version is served under its name, the latest one included. An address without a version, such as /guide, opens the same page of the latest version, but a nav activeMatch such as ^/guide/ has to become ^/[^/]+/guide/. See Moving from v2.
Installation
Install the package using your preferred package manager:
npm install @viteplus/versionspnpm add @viteplus/versionsyarn add @viteplus/versionsBasic Setup
1. Initialize Your VitePress Project
If you haven't already, create a VitePress project:
npm init vitepress2. Configure Versioning
Create or modify your VitePress configuration file (): .vitepress/config.ts
import { defineVersionedConfig } from '@viteplus/versions';
export default defineVersionedConfig({
title: 'My Documentation',
description: 'Versioned documentation with VitePress',
// Versioning configuration
versionsConfig: {
current: 'next', // Label of the version in sources
latest: 'v2.0', // The stable version, opened at the root
sources: 'src', // Current version source directory
archive: 'archive', // Archive directory for older versions
versions: {
'v1.0': { label: 'v1.0 (LTS)' } // Per-version switcher label
},
versionSwitcher: {
text: 'Version',
includeCurrentVersion: true
}
},
// Other VitePress configuration...
themeConfig: {
// ...
}
});Project Structure
Organize your documentation files following this structure:
docs/
├── .vitepress/
│ └── config.ts
├── src/ // Current version docs ← sources → /next/
│ ├── index.md
│ ├── guide/
│ │ └── ...
│ └── api/
│ └── ...
└── archive/ // Archived versions ← archive
├── v1.0/ → /v1.0/
│ ├── index.md
│ └── ...
└── v2.0/ → /v2.0/ (the latest version, also opened at /)
├── index.md
└── ...Every version is served under its own name, and / opens the home page of the latest version. Leave latest out and the current version is the latest one. Both directory names are configurable through sources and archive. They are filesystem locations only, so renaming them does not change a single URL.
Core Features
Version Switching
The plugin automatically adds a version switcher that allows users to navigate between documentation versions:
- Simple Configuration: Enable with minimal setup via
versionSwitcheroption - Custom Component: For advanced needs, use the
VersionSwitchercomponent directly
Localization
Support multiple languages with version-aware localization:
export default defineVersionedConfig({
// ...
locales: {
root: {
lang: 'en',
label: 'English'
},
fr: {
lang: 'fr',
label: 'Français'
}
}
});Version-Specific Navigation
Define different navigation structures for each version:
export default defineVersionedConfig({
// ...
themeConfig: {
nav: {
root: [
{ text: 'Home', link: '/' },
{ text: 'Guide', link: '/guide/' }
],
'v1.0': [
{ text: 'Home', link: '/' },
{ text: 'Legacy API', link: '/legacy-api/' }
]
}
}
});Stable and Unreleased Versions
Point latest at an archived version while sources holds the docs of a coming release. Readers keep landing on the stable version at /, which shows its versioned URL such as /v2.0/, the coming one is reviewed at /next/, and every version keeps a permanent URL under its name.
Automatic Path Rewriting
URLs are automatically structured based on versions and locales, creating clean, predictable paths. Absolute links in your markdown, such as /guide/, stay inside the version of the page that holds them.
Advanced Configuration
Custom URL Structure
Customize how URLs are generated with the rewritesHook:
import { join } from 'path/posix';
export default defineVersionedConfig({
// ...
versionsConfig: {
// ...
hooks: {
rewritesHook: (source, version, locale) => {
// The locale and the version stay in front, the rest is yours
return join(locale, version, source.toLowerCase());
}
}
}
});Enhanced Theme Integration
Integrate the version switcher into your custom theme:
// .vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme';
import VersionSwitcher from '@viteplus/versions/components/version-switcher.component.vue';
export default {
extends: DefaultTheme,
enhanceApp({ app }) {
app.component('VersionSwitcher', VersionSwitcher);
}
};Key Configuration Options
| Option | Description |
|---|---|
versionsConfig.current | Label of the version in sources (e.g., "next") |
versionsConfig.latest | The stable version, opened at the root |
versionsConfig.versions | Per-version settings: a switcher label, a path |
versionsConfig.sources | Directory containing current version docs |
versionsConfig.archive | Directory containing archived versions |
versionsConfig.versionSwitcher | Configuration for the version switcher |
versionsConfig.hooks.rewritesHook | Customize URL structure |
See also
- Configuration Options - every
versionsConfigoption - Sidebar - customize the sidebar per version
- URL Path Rewrites - control how URLs are generated
- Navigation - customize the nav bar per version
- Localization - set up multilingual documentation
- Version Switcher - customize the switcher component
- Release Notes - what changed in each release
