--- title: Lazy Head since: 3.5.0 description: 'Keep the Unhead DOM runtime out of the initial bundle and choose when to apply queued head entries.' navigation: title: 'Lazy Head' --- The lazy head queues client entries while the server tags stay in the document. Your app or framework calls `head.load()` when it needs to apply the queued entries. ::note The lazy head is experimental. Its API may change in a minor release. :: ## Enable lazy head in Vite Set the build option in your Vite config. Keep your existing client entry and composable calls. ```ts [vite.config.ts] import { Unhead } from '@unhead/vue/vite' import { defineConfig } from 'vite' export default defineConfig({ plugins: Unhead({ lazyHead: true }), }) ``` The production build defers the client renderer until after Vue hydration. Unhead waits for a pending root `` to resolve. If setup requests a script with `useScript()`, Unhead can load the renderer early so the script can finish. Streaming SSR writes patches to the same queue. Keep your existing server streaming setup. With Vue Router, the switched head discards the initial stream after successful navigation to a different path. Failed navigation keeps the stream active. If your integration owns navigation, use the explicit lazy entry and call `head.discardStream()` when leaving the initial page. The shell supports queued entries, plugins, and Vue's `useScript()`. APIs that inspect resolved entries or render directly become available once the runtime loads. Register hooks during setup with `head.hooks.hook()` or through the `createHead()` options. ## Choose when to load This example uses Vue Router and assumes `createApp()` returns the app and router. Keep `createHead()` from `@unhead/vue/server` in the server entry. In the client entry, install the lazy head as a Vue plugin. Components can keep calling `useHead()` and `useSeoMeta()`. ```ts [src/entry-client.ts] import { createHead } from '@unhead/vue/lazy' import { nextTick } from 'vue' import { createApp } from './main' const { app, router } = createApp() const head = createHead() app.use(head) router.isReady().then(() => { app.mount('#app') head.finishHydration() router.afterEach(async (_to, _from, failure) => { if (failure) return await nextTick() await head.load().catch(error => console.error(error)) }) }) ``` This example loads the runtime after the first successful navigation. At that point, Unhead applies the queued entries and handles future updates. If `` delays the first page, call `head.finishHydration()` on its first `resolve` event. If later pages also wait for ``, call `head.load()` when the new page resolves. With an out-in transition, you can wait for the new page to enter. If a client value must apply on the first page, call `head.load()` after hydration. Choose the timing in the client entry or framework integration. ## Check the first interaction Measure the first click or tap during startup and again after hydration. Use Event Timing or the Interactions track in Chrome DevTools to measure time to next paint. Check that the click triggers the same action in both builds. Compare initial LCP and interaction timings with eager head under the same network and CPU settings. If a navigation only changes metadata such as the title, test giving the browser a paint opportunity before calling `load()`. Keep scripts and styles that the destination needs to render on the critical path. ## Streaming SSR Keep `createStreamableHead()` and `wrapStream()` on the server. If you use the explicit lazy entry, omit the `streaming` bundler option so the lazy head can connect to the server queue. In the client entry, set `streamKey` to the key used by the server. The default server key is `__unhead__`. ```ts import { createHead } from '@unhead/vue/lazy' const head = createHead({ streamKey: '__unhead__' }) ``` Install the plugin and call `head.finishHydration()` as above. When the framework calls `head.load()`, Unhead applies queued patches and listens for later patches. Call `load()` early if the page needs a streamed stylesheet or script during startup. When navigation leaves the streamed page, call `head.discardStream()` before applying the destination entries. It drops pending patches, ignores later patches, and removes applied stream tags on the next render. You can call it before the runtime loads. ## Pages with no server head For a client-only app or an SPA fallback, omit `streamKey`. Call `head.finishHydration()` before pushing entries, then call `head.load()` during startup. ## Scripts and plugins Vue's `useScript()` queues script tags until activation. If an integration subscribes directly to `head.hooks` during setup, pass a `HookableCore` instance as `hookable` when creating the shell. ```ts import { createHead } from '@unhead/vue/lazy' import { HookableCore } from 'hookable' const head = createHead({ hookable: new HookableCore() }) ``` For direct use outside Vue, import `useScript` from `unhead/scripts` and pass the client head as the first argument. Hooks and plugins take effect when the runtime loads. Use `lazyPlugins` to load plugin code with the runtime. ```ts const head = createHead({ lazyPlugins: () => import('@unhead/vue/plugins').then(m => [m.TemplateParamsPlugin]), }) ``` ## Preload the runtime Unhead’s Vite plugin adds a low-priority `modulepreload` link to production HTML entries that use the lazy head. Streaming SSR can serve that HTML as its template. If you use the explicit lazy entry, call `head.load()` to apply queued entries. To fetch the runtime only when `head.load()` runs, set `Unhead({ lazyHeadPreload: false })` in your Vite config. ## Dev warning On a buffered SSR page, `finishHydration()` can warn when a queued client tag differs from a server tag. The warning names the tag and, when available, the Vue component. Render the same value on both sides, or call `head.load()` when the client value needs to apply. See the [lazy head API](/docs/head/api/lazy-head) for options, methods, and load errors.