Tree-Shaking
The tree-shaking transform removes standalone calls to deprecated server composables and useSchemaOrg from client builds. It is enabled by default.
Removed calls
This transform removes calls to composables that should only run on the server. On client builds these calls are dead code; removing them reduces bundle size.
Composables removed from client bundles:
useServerHead()(deprecated, useuseHead())useServerHeadSafe()(deprecated, useuseHeadSafe())useServerSeoMeta()(deprecated, useuseSeoMeta())useSchemaOrg()
useHead(unheadInstance, { title: 'My Page' })
useSchemaOrg(unheadInstance, [
defineWebPage({ name: 'My Page' }),
])
useHead(unheadInstance, { title: 'My Page' })
useServerHead, useServerHeadSafe, and useServerSeoMeta are deprecated aliases. They exist for backward compatibility and are identical to their non-server counterparts. New code should use useHead, useHeadSafe, and useSeoMeta directly. The tree-shaking transform still removes calls to the deprecated names for codebases that haven't migrated yet.The transform only runs on client builds. SSR builds are left untouched. It removes standalone call statements; a call used as part of an assignment or larger expression is retained.
Setup
Tree-shaking is enabled by default when using the unified Vite plugin:
import { Unhead } from '@unhead/vue/vite'
export default defineConfig({
plugins: [Unhead()],
})
Disable
Unhead({
treeshake: false,
})
Options
Unhead({
treeshake: {
// Override the shared file filter for this transform only
filter: {
include: [/my-special-file/],
exclude: [/some-file/],
},
},
})
| Option | Type | Default | Description |
|---|---|---|---|
filter | object | inherited | Include/exclude file patterns (overrides the shared filter) |
How It Works
The transform parses each file and walks the AST for standalone calls to the server composable names. It recognizes calls imported from Unhead packages and unbound auto-imported names, but leaves locally declared functions with the same names alone. Matching statements are removed while preserving sourcemaps.