unhead@betaQuick Answer: To reduce bundle size, use import.meta.client to conditionally add tags only on the client. For Vite, use the Unhead Vite plugin to tree-shake server-only code.
Unhead runs on both server and client. You can conditionally render tags in one environment to optimize bundle size.
Client-only tags render exclusively in the browser, reducing server-side bundle size.
Use import.meta.client to conditionally add tags:
import { useHead } from '@unhead/react'
// Using import.meta environment variables
if (import.meta.client) {
useHead({
script: [
{
src: 'https://example.com/analytics.js',
defer: true
}
]
})
}
Server-only tags render during SSR, reducing client-side bundle size.
Use import.meta.server to conditionally add tags:
import { useHead } from '@unhead/react'
if (import.meta.server) {
useHead({
meta: [
{
property: 'og:image',
content: 'https://example.com/my-image.jpg'
}
]
})
}
Load analytics only after the application has hydrated:
import { useHead } from '@unhead/react'
if (import.meta.client) {
useHead({
script: [
{
src: 'https://example.com/analytics.js',
defer: true,
async: true
}
]
})
}
Add SEO metadata that doesn't need client updates:
import { useHead } from '@unhead/react'
if (import.meta.server) {
useHead({
meta: [
{ name: 'robots', content: 'index, follow' },
{ name: 'description', content: 'Site description' }
]
})
}
titleTemplate affects title rendering - include on both client and server to avoid title flashingtagPosition or tagPriority may behave differently if not consistently appliedEnsure dependent tags are included in both environments when needed.