Skip to content

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:

js
// .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:

  1. Use appropriate formats: WebP for photos, SVG for icons and illustrations
  2. Lazy loading: Images load only when scrolled into view
md
<!-- In your Markdown files -->
![Alt text](/path/to/image.webp){loading="lazy" width="300" height="200"}

For automatic image optimization, consider using plugins:

js
// .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:

js
// .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:

js
// .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:

js
// .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):

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:

js
// .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:

js
// .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

  1. Measure First: Use Lighthouse or WebPageTest to identify performance bottlenecks before optimizing.

  2. Optimize Images: Use appropriate formats and sizes for images.

  3. Minimize JavaScript: Avoid unnecessary client-side JavaScript.

  4. Use Static Rendering: Leverage VitePress's static site generation for optimal performance.

  5. Implement Progressive Enhancement: Ensure content is accessible even before JavaScript loads.

  6. 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:

bash
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:

html
<picture>
  <source srcset="/image.avif" type="image/avif">
  <source srcset="/image.webp" type="image/webp">
  <img src="/image.jpg" alt="Description">
</picture>

This document will be continuously updated. If you have any questions, please provide feedback through GitHub Issues.

VitePress Development Guide