Runtime API
VitePress exposes several client-side runtime APIs that can be imported fromvitepress in your theme or markdown files.
Utilities
withBase
Appends the site base to internal URLs.The URL path to append the base to
string - The path with base prepended
Behavior:
- External URLs (starting with
http://,https://, etc.) are returned unchanged - Relative URLs (not starting with
/) are returned unchanged - Only internal absolute URLs (starting with
/) get the base prepended
inBrowser
Boolean flag indicating if the code is running in a browser environment.boolean
Use this to conditionally execute browser-only code and avoid SSR errors.
onContentUpdated
Register a callback that runs every time the markdown content is updated in the DOM.Function to call when content is updated
- Re-initializing syntax highlighters
- Updating image lightboxes
- Refreshing third-party widgets
- Running custom DOM manipulations
getScrollOffset
Calculates the current scroll offset based on the site configuration.number - The scroll offset in pixels
Configuration: Respects the scrollOffset option in your VitePress config:
defineClientComponent
Defines a client-only component that loads asynchronously.Async function that returns the component
Arguments to pass to the component (optional)
Callback function to run after component loads (optional)
- Loading components that use browser-only APIs
- Deferring heavy components until after hydration
- Integrating third-party libraries that don’t support SSR
Components
Content
Renders the current page’s markdown content.The tag or component to render the content as
- Automatically renders the current route’s page component
- Triggers content updated callbacks when DOM changes
- Displays “404 Page Not Found” when no component is available
ClientOnly
Wrapper component that only renders its content on the client side.- Content is not rendered during SSR
- Content appears only after the page is hydrated in the browser
- No props required
- Components using
window,document, or other browser APIs - Third-party widgets that don’t support SSR
- Dynamic content that should only render client-side
Global Properties
VitePress injects global properties into all Vue components:$frontmatter
Access the current page’s frontmatter data.Record<string, any>
$params
Access dynamic route parameters for content loader pages.Record<string, any>
See Data Loading for more information on dynamic routes.