Build Optimization
Overview
Build optimization in VitePress helps you create faster, more efficient documentation sites. This guide covers techniques to optimize your VitePress build process and improve the performance of your deployed site.
Core Optimization Techniques
Asset Optimization
VitePress leverages Vite's built-in optimizations for assets:
// .vitepress/config.js
export default {
vite: {
build: {
// Configure chunk size warnings threshold (in kB)
chunkSizeWarningLimit: 1000,
// Customize rollup options
rollupOptions: {
output: {
manualChunks: {
// Split vendor code into separate chunks
'vue-vendor': ['vue', 'vue-router'],
'markdown': ['markdown-it', '@mdit-vue/plugin-toc']
}
}
}
}
}
}
Image Optimization
Optimize images to reduce load times:
- Use appropriate formats: WebP for photos, SVG for icons and illustrations
- Lazy loading: Images load only when scrolled into view
<!-- In your Markdown files -->
{loading="lazy" width="300" height="200"}
For automatic image optimization, consider using plugins:
// .vitepress/config.js
import imagemin from 'vite-plugin-imagemin'
export default {
vite: {
plugins: [
imagemin({
gifsicle: { optimizationLevel: 7 },
mozjpeg: { quality: 80 },
pngquant: { quality: [0.7, 0.9] },
webp: { quality: 80 }
})
]
}
}
Code Splitting
VitePress automatically performs code splitting, but you can optimize it further:
// .vitepress/config.js
export default {
vite: {
build: {
// Adjust chunk size
chunkSizeWarningLimit: 1000,
// Custom splitting strategy
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
return id.toString().split('node_modules/')[1].split('/')[0].toString();
}
}
}
}
}
}
}
Performance Optimization
Preloading Key Assets
Preload critical resources:
// .vitepress/config.js
export default {
head: [
['link', { rel: 'preload', href: '/fonts/my-font.woff2', as: 'font', type: 'font/woff2', crossorigin: '' }]
]
}
Prefetching Pages
VitePress can prefetch linked pages to make navigation feel instant:
// .vitepress/config.js
export default {
themeConfig: {
// Enable prefetching (default: true)
prefetch: true
}
}
Caching Strategies
Configure caching headers in your hosting platform:
- Long cache for hashed assets (JS, CSS with content hash)
- Short cache for HTML and configuration files
Example for Netlify (netlify.toml
):
[[headers]]
for = "/_assets/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
[[headers]]
for = "/*.html"
[headers.values]
Cache-Control = "public, max-age=0, must-revalidate"
Build Performance
Faster Development Builds
Speed up development builds:
// .vitepress/config.js
export default {
vite: {
// Optimize dependencies for faster startup
optimizeDeps: {
include: ['vue', 'markdown-it']
},
// Use esbuild for faster transpilation
esbuild: {
jsxFactory: 'h',
jsxFragment: 'Fragment'
}
}
}
Production Build Optimization
For production builds:
// .vitepress/config.js
export default {
vite: {
build: {
// Minify output
minify: 'terser',
terserOptions: {
compress: {
// Remove console.logs in production
drop_console: true
}
},
// Generate sourcemaps for debugging
sourcemap: false
}
}
}
Best Practices
Measure First: Use Lighthouse or WebPageTest to identify performance bottlenecks before optimizing.
Optimize Images: Use appropriate formats and sizes for images.
Minimize JavaScript: Avoid unnecessary client-side JavaScript.
Use Static Rendering: Leverage VitePress's static site generation for optimal performance.
Implement Progressive Enhancement: Ensure content is accessible even before JavaScript loads.
Monitor Performance: Regularly check performance metrics after deploying changes.
Frequently Asked Questions
How can I reduce the size of my VitePress bundle?
- Remove unused components and features
- Use dynamic imports for rarely used components
- Optimize images and other assets
- Consider using a CDN for large libraries
How do I debug slow build times?
Use the --debug
flag when building:
vitepress build --debug
This will output detailed timing information about the build process.
Can I use modern image formats like AVIF?
Yes, you can use modern formats, but you may need to provide fallbacks for older browsers:
<picture>
<source srcset="/image.avif" type="image/avif">
<source srcset="/image.webp" type="image/webp">
<img src="/image.jpg" alt="Description">
</picture>
Related Resources
This document will be continuously updated. If you have any questions, please provide feedback through GitHub Issues.