Tree-Shaking · Unhead

[Unhead Home](https://unhead.unjs.io/ "Home")

- [Docs](https://unhead.unjs.io/docs/typescript/head/guides/get-started/overview)
- [Tools](https://unhead.unjs.io/tools)
- [Learn](https://unhead.unjs.io/learn/guides/what-is-capo)

[Releases](https://unhead.unjs.io/releases)

Search…```k`` /`

[Unhead on GitHub](https://github.com/unjs/unhead)

[User Guides](https://unhead.unjs.io/docs/typescript/head/guides/get-started/overview)

[API](https://unhead.unjs.io/docs/typescript/head/api/get-started/overview)

[Releases](https://unhead.unjs.io/docs/typescript/releases/v3)

TypeScript

- [Switch to TypeScript](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/tree-shaking)
- [Switch to Vue](https://unhead.unjs.io/docs/vue/head/guides/build-plugins/tree-shaking)
- [Switch to React](https://unhead.unjs.io/docs/react/head/guides/build-plugins/tree-shaking)
- [Switch to Svelte](https://unhead.unjs.io/docs/svelte/head/guides/build-plugins/tree-shaking)
- [Switch to Solid.js](https://unhead.unjs.io/docs/solid-js/head/guides/build-plugins/tree-shaking)
- [Switch to Angular](https://unhead.unjs.io/docs/angular/head/guides/build-plugins/tree-shaking)
- [Switch to Nuxt](https://unhead.unjs.io/docs/nuxt/head/guides/build-plugins/tree-shaking)

v3 (stable)

Head

- [Discord Support](https://discord.com/invite/275MBUBvgP)
- [TypeScript Playground](https://stackblitz.com/edit/github-hhxywsb5)

- Get Started
  - [Overview](https://unhead.unjs.io/docs/typescript/head/guides/get-started/overview)
  - [Introduction to Unhead](https://unhead.unjs.io/docs/typescript/head/guides/get-started/intro-to-unhead)
  - [Starter Recipes](https://unhead.unjs.io/docs/typescript/head/guides/get-started/starter-recipes)
- Core Concepts
  - [Titles & Title Templates](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/titles)
  - [Tag Sorting & Placement](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/positions)
  - [Class & Style Attributes](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/class-attr)
  - [Inline Style & Scripts](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/inner-content)
  - [Tag Deduplication](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/handling-duplicates)
  - [DOM Event Handling](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/dom-event-handling)
  - [Script Loading](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/loading-scripts)
- Build Plugins
  - [Overview](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/overview)
  - [Tree-Shaking](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/tree-shaking)
  - [useSeoMeta Transform](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/seo-meta-transform)
  - [Minify Transform](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/minify-transform)
  - [Devtools](https://unhead.unjs.io/docs/typescript/head/guides/build-plugins/devtools)
- Plugins
  - [Template Params](https://unhead.unjs.io/docs/typescript/head/guides/plugins/template-params)
  - [Alias Sorting](https://unhead.unjs.io/docs/typescript/head/guides/plugins/alias-sorting)
  - [Canonical Plugin](https://unhead.unjs.io/docs/typescript/head/guides/plugins/canonical)
  - [Infer SEO Meta](https://unhead.unjs.io/docs/typescript/head/guides/plugins/infer-seo-meta-tags)
  - [Minify](https://unhead.unjs.io/docs/typescript/head/guides/plugins/minify)
  - [Validate](https://unhead.unjs.io/docs/typescript/head/guides/plugins/validate)

Build Plugins

# Tree-Shaking

[Copy for LLMs](https://raw.githubusercontent.com/unjs/unhead/refs/heads/main/docs/head/1.guides/build-plugins/1.tree-shaking.md)

Last updated Apr 9, 2026 by [Harlan Wilton](https://github.com/harlan-zw) in [refactor(bundler)!: named Unhead export, ctx-based transforms, dev-mode validate (#733)](https://github.com/unjs/unhead/pull/733).

On this page

- [What Does It Do?](#what-does-it-do)
- [Setup](#setup)
- [Options](#options)
- [How It Works](#how-it-works)

**Quick Answer:** The tree-shaking transform strips deprecated server composables and `useSchemaOrg` calls from client bundles so they never ship to the browser. Enabled by default.

## [What Does It Do?](#what-does-it-do)

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, use `useHead()`)
- `useServerHeadSafe()` (deprecated, use `useHeadSafe()`)
- `useServerSeoMeta()` (deprecated, use `useSeoMeta()`)
- `useSchemaOrg()`

Before (source)

After (client build)

```
useHead(unheadInstance, { title: 'My Page' })

useSchemaOrg([
  defineWebPage({ name: 'My Page' }),
])
```

```
useHead(unheadInstance, { title: 'My Page' })
```

`useServerHead`, `useServerHeadSafe`, and `useServerSeoMeta` are deprecated aliases. They exist for backwards 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.

## [Setup](#setup)

Tree-shaking is enabled by default when using the [unified Vite plugin](https://unhead.unjs.io/docs/head/guides/build-plugins/overview):

```
import { Unhead } from '@unhead/vue/vite'

export default defineConfig({
  plugins: [Unhead()],
})
```

### [Disable](#disable)

```
Unhead({
  treeshake: false,
})
```

## [Options](#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](#how-it-works)

The transform parses each file with [oxc-parser](https://github.com/nicolo-ribaudo/oxc-parser/tree/nicolo/oxc-parser) and walks the AST looking for `ExpressionStatement` nodes that call one of the server composable names. Matching statements are removed from the source using [magic-string](https://github.com/Rich-Harris/magic-string), preserving sourcemaps.

[Edit this page](https://github.com/unjs/unhead/edit/main/docs/head/1.guides/build-plugins/1.tree-shaking.md)

[Markdown For LLMs](https://raw.githubusercontent.com/unjs/unhead/refs/heads/main/docs/head/1.guides/build-plugins/1.tree-shaking.md)

Did this page help you?

[Overview Unified Vite and Webpack plugins for Unhead. Build optimizations, tree-shaking, and useSeoMeta transforms in a single plugin call.](https://unhead.unjs.io/docs/head/guides/build-plugins/overview) [useSeoMeta Transform Convert useSeoMeta() calls to useHead() at build time, saving ~3kb from your client bundle.](https://unhead.unjs.io/docs/head/guides/build-plugins/seo-meta-transform)

On this page

- [What Does It Do?](#what-does-it-do)
- [Setup](#setup)
- [Options](#options)
- [How It Works](#how-it-works)

[GitHub](https://github.com/unjs/unhead) [ Discord](https://discord.com/invite/275MBUBvgP)

[ /llms.txt](https://unhead.unjs.io/llms.txt)

[Part of the UnJS ecosystem](https://unjs.io/)

### Head Management

- [Getting Started](https://unhead.unjs.io/docs/typescript/head/guides/get-started/overview)
- [useHead](https://unhead.unjs.io/docs/typescript/head/api/composables/use-head)
- [useSeoMeta](https://unhead.unjs.io/docs/typescript/head/api/composables/use-seo-meta)
- [useHeadSafe](https://unhead.unjs.io/docs/typescript/head/api/composables/use-head-safe)
- [useScript](https://unhead.unjs.io/docs/typescript/head/api/composables/use-script)

### Schema.org

- [Getting Started](https://unhead.unjs.io/docs/typescript/schema-org/guides/get-started/overview)
- [useSchemaOrg](https://unhead.unjs.io/docs/typescript/schema-org/api/composables/use-schema-org)
- [Nodes](https://unhead.unjs.io/docs/typescript/schema-org/guides/core-concepts/nodes)
- [Recipes](https://unhead.unjs.io/docs/typescript/schema-org/guides/recipes/identity)

### Guides

- [Titles](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/titles)
- [Streaming SSR](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/streaming)
- [DOM Events](https://unhead.unjs.io/docs/typescript/head/guides/core-concepts/dom-event-handling)
- [Plugins](https://unhead.unjs.io/docs/typescript/head/guides/plugins/template-params)

### Tools

- [Meta Tag Generator](https://unhead.unjs.io/tools/meta-tag-generator)
- [OG Image Generator](https://unhead.unjs.io/tools/og-image-generator)
- [Schema.org Generator](https://unhead.unjs.io/tools/schema-generator)
- [Capo.js Analyzer](https://unhead.unjs.io/tools/capo-analyzer)

### Articles

- [What is Capo.js?](https://unhead.unjs.io/learn/guides/what-is-capo)

### Research

- [State of <head> in 2026](https://unhead.unjs.io/learn/research/state-of-head-2026)
- [Streaming Head Performance](https://unhead.unjs.io/learn/research/streaming-head-performance)
- [Capo.js Performance Research](https://unhead.unjs.io/learn/research/capo-performance-research)

Copyright © 2025-2026 Harlan Wilton - [MIT License](https://github.com/unjs/unhead/blob/main/license)