Version Switcher Configuration
The version switcher is a key component in @viteplus/versions that allows users to navigate between different documentation versions. This document explains the two methods for implementing version switching in your documentation.
Basic Configuration Method
The simplest way to add version switching is through the versionSwitcher property in your versionsConfig. This method provides a straightforward dropdown menu in your navigation bar.
Configuration Options
const config = {
versionsConfig: {
// other version settings...
versionSwitcher: {
text: 'Switch Version', // The display label for the dropdown
includeCurrentVersion: true // Whether to list the latest version first, tagged (latest)
}
}
}You can also disable the version switcher completely:
versionsConfig: {
// other version settings...
versionSwitcher: false
}How It Works
When enabled, the plugin automatically:
- Builds a list of all available versions from your documentation
- Creates a dropdown menu in your navigation bar with the specified label
- Lists every version as a menu item linking to its home page, showing its label
- Optionally lists the
latestversion first, tagged(latest)(controlled byincludeCurrentVersion)
Limitations
The basic version switcher has some limitations:
- It doesn't preserve the user's current page position when switching versions
- It doesn't maintain locale settings when changing versions
- It uses a generic dropdown style that might not match custom themes
Advanced Component Method
For more control over version switching, @viteplus/versions provides a custom Vue component: VersionSwitcher. This component offers enhanced functionality and can be styled to match your theme.
Step 1: Register the Component
First, register the component in your theme setup:
// docs/.vitepress/theme/index.ts
import { h } from 'vue';
import DefaultTheme from 'vitepress/theme';
import VersionSwitcher from '@viteplus/versions/components/version-switcher.component.vue';
export default {
extends: DefaultTheme,
Layout: () => {
return h(DefaultTheme.Layout, null, {
// Custom layout slots if needed
});
},
enhanceApp({ app }) {
// Register the component
app.component('VersionSwitcher', VersionSwitcher);
}
};Step 1b: Catch Switches Onto Missing Pages
Not every version holds every page, and not every page is translated. The VersionSwitcher already links to a page that exists, but VitePress's language menu links each page to the same page in every language, translated or not. installVersionFallback catches a switch of version or language onto a page that does not exist there:
// docs/.vitepress/theme/index.ts
import { installVersionFallback } from '@viteplus/versions/fallback';
export default {
extends: DefaultTheme,
enhanceApp({ app, router, siteData }) {
app.component('VersionSwitcher', VersionSwitcher);
installVersionFallback(router, siteData);
}
};| On | Asked for | Opens | Why |
|---|---|---|---|
/v2.x/config/cli | /de/v2.x/config/cli | /de/v2.x/ | Language switch, the page is not translated |
/v1.x/hooks | /de/v1.x/hooks | /de/v2.x/ | Language switch, v1.x has no German pages at all |
/de/v2.x/guide | /de/v1.x/guide | /de/v1.x/ | Version switch, v1.x has no German guide |
/v2.x/guide | /v2.x/missing | 404 | Not a switch: a missing page keeps its 404 |
Only a switch is caught, meaning the same page with another version or language. Every other missing address, whether typed, bookmarked, or linked, shows the not-found page, so a broken link stays visible.
Step 2: Add to Navigation
Then, include the component in your navigation configuration:
// docs/.vitepress/config.ts
export default defineVersionedConfig({
// Other configuration...
themeConfig: {
nav: {
root: [
{ text: 'Home', link: '/' },
// Add the version switcher component
{ component: 'VersionSwitcher' }
]
}
}
});Benefits of the Component Approach
The VersionSwitcher component offers significant advantages:
- Preserves Navigation Context: When switching versions, it keeps your position in the documentation by carrying the current page path across.
- Locale Support: Maintains the current locale when switching between versions.
- Responsive Design: Adapts to both desktop and mobile viewports with appropriate styling.
- Custom Styling: Can be styled to match your theme's design system.
- Dynamic Behavior: Shows only relevant version options based on the current context.
The component reads the versions, their labels, and the routes of every version from themeConfig.versioning, which the plugin publishes once for the whole site. A custom theme that renders VersionSwitcher itself needs no props beyond label, latestLabel, and screenMenu; a versioningPlugin prop still overrides the site data.
Marking the Latest Version
The switcher tags the latest version, both on the flyout button and in the menu, so v2.1.x reads as v2.1.x (latest). Without latest, that is the current version. Set latestLabel in the nav item's props to change the tag, for example to translate it, or set it to an empty string to turn it off:
nav: [
{ component: 'VersionSwitcher', props: { latestLabel: 'neueste' } }
]A latest version whose label already reads as the tag, such as the default current: 'latest', is shown as it is rather than as latest (latest).
Version Labels
Both switchers show a version's label instead of its name when it has one:
versionsConfig: {
versions: {
'v1.x': { label: 'v1.x (LTS)' }
}
}The menu entry reads v1.x (LTS), and its link still points at /v1.x/.
Changing the Label
The switcher reads Switch Version on the mobile menu button and in the accessible label of the desktop flyout. Set label in the nav item's props to change it:
nav: [
{ component: 'VersionSwitcher', props: { label: 'Version wechseln' } }
]Translating the Switcher
Each locale has its own nav, so give each one its own VersionSwitcher with its own label and latestLabel:
export default defineVersionedConfig({
versionsConfig: {
current: 'v3.0.x',
versionSwitcher: false
},
locales: {
root: {
lang: 'en',
label: 'English',
themeConfig: {
nav: [
{ component: 'VersionSwitcher' }
]
}
},
de: {
lang: 'de',
label: 'Deutsch',
themeConfig: {
nav: [
{ component: 'VersionSwitcher', props: { label: 'Version wechseln', latestLabel: 'neueste' } }
]
}
}
}
});🌐 Keep the switcher out of the global nav
Items of the global themeConfig.nav are added to the nav of every locale, and so is the basic dropdown. Set versionSwitcher: false and place the component only in each locale's nav, or every language shows the same untranslated switcher.
🏠 Missing pages
A version rarely carries every page of the one before it. The plugin lists the routes each version publishes, so when the page you are reading is absent from the version you pick, the switcher links to that version's home page instead of a URL that would answer 404. Routes come from the same resolver that builds the rewrites, so a listed route is always a served one.
TIP
The version list is built from current, the archive subfolders, and the versions with a path, so you never list versions by hand. The switcher stays hidden until there is more than one version.
