Quick Answer: The Unhead Vite plugin enables build-time optimizations like tree-shaking server-only code and transforming useSeoMeta() for smaller bundles. Install with UnheadVite() in your Vite config.
Unhead provides official build plugins for Vite and Webpack that help optimize your application bundle size through automatic transformations and optimizations.
While these plugins are optional, they're highly recommended for production applications to ensure optimal performance and bundle size.
The build plugins perform several important optimizations:
useSeoMeta calls into raw useHead() calls (~3kb savings)First, install the addons package that contains the build plugins:
pnpm add -D @unhead/addons
yarn add -D @unhead/addons
npm install -D @unhead/addons
Add the Unhead Vite plugin to your vite.config.ts file:
import UnheadVite from '@unhead/addons/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
UnheadVite(),
],
})
Add the Unhead Webpack plugin to your webpack configuration:
import { UnheadWebpack } from '@unhead/addons/webpack'
export default {
plugins: [
UnheadWebpack(),
],
}
The build plugins accept configuration options to customize their behavior:
UnheadVite({
// Options here
})
| Option | Type | Default | Description |
|---|---|---|---|
transformSeoMeta | boolean | true | Transform useSeoMeta calls to useHead() |
To verify that the plugin is working correctly, you can:
When using Unhead with a framework, make sure to add the plugin to your build configuration:
// No configuration needed - Nuxt configures the plugin automatically
import UnheadVite from '@unhead/addons/vite'
// vite.config.ts
import vue from '@vitejs/plugin-vue'
export default {
plugins: [
vue(),
UnheadVite(),
],
}
import UnheadVite from '@unhead/addons/vite'
// vite.config.ts
import react from '@vitejs/plugin-react'
export default {
plugins: [
react(),
UnheadVite(),
],
}
You can customize the plugin's behavior based on your needs:
UnheadVite({
// Disable useSeoMeta transformation if you prefer to keep it
transformSeoMeta: false,
})