Asset Handling
VitePress leverages Vite’s powerful asset handling system to automatically process, optimize, and bundle your static assets.Referencing Static Assets
All Markdown files are compiled into Vue components and processed by Vite, enabling sophisticated asset handling.Relative URLs
Reference assets using relative paths from your markdown files:VitePress processes assets using Vite’s asset pipeline. Common image, media, and font filetypes are automatically detected and included as assets.
How Assets Are Processed
Detection
VitePress automatically detects references to images, fonts, videos, and other media files in:
- Markdown files
- Vue components in your theme
- CSS and style files
Optimization
During production builds:
- Assets are copied to the output directory with hashed filenames
- Images smaller than 4KB are base64-inlined
- Unreferenced assets are not copied
Size Threshold Configuration
Customize the inlining threshold via Vite config:The Public Directory
Thepublic directory is for static assets that should be served as-is without processing.
Structure
When to Use Public Directory
- Use Public For
- Don't Use Public For
robots.txt,sitemap.xml- Favicons and PWA icons
- Files that must keep their exact filename
- Large assets not referenced in source
- Legacy assets with hardcoded URLs
Referencing Public Assets
Base URL Configuration
When deploying to a non-root URL, configure thebase option:
Automatic Path Adjustment
How Base URL Works
How Base URL Works
All static asset paths are automatically adjusted for the Becomes:No manual path updates needed when changing
base value:In Markdown:base!Dynamic Paths in Components
For dynamic asset paths in Vue components, use thewithBase helper:
Importing Assets in Components
Import assets directly in Vue components for optimal processing:Images
Fonts
JSON and Other Data
Image Optimization
VitePress provides several ways to optimize images:Lazy Loading
Enable lazy loading for all markdown images:Implementation details in
src/node/markdown/plugins/image.tsResponsive Images
Use the<picture> element for responsive images:
External Image Optimization
Integrate with image optimization services:Asset Handling Strategies
Development vs Production
- Development
- Production
- Assets served directly from source
- No hashing or optimization
- Fast hot module replacement
- Source maps enabled
Best Practices
Optimize Before Committing
Use tools like ImageOptim or TinyPNG before adding images to your repository
Special Asset Types
SVG Files
SVGs can be used as images or imported as components:Video Files
Downloadable Files
Troubleshooting
Asset Not Found
If assets aren’t loading:- Check the file path is correct relative to the markdown file
- Verify the file exists in your source directory
- Ensure the file extension is included
- Check for typos in the filename (paths are case-sensitive)
Base URL Issues
If assets work locally but not in production:- Verify
baseis correctly set in config - Use
withBase()for dynamic paths in components - Check that public assets use absolute paths starting with
/