Index of every page on this site: llms.txt
TypeScript
Hooks

ssr:rendered Hook

The ssr:rendered hook runs after Unhead serializes all resolved tags. It receives both the tags and the final SSRHeadPayload. Synchronous changes to ctx.html become the value returned by head.render().

Hook Signature

export interface Hook {
  'ssr:rendered': (ctx: SSRRenderContext) => HookResult
}

interface SSRRenderContext {
  tags: HeadTag[]
  html: SSRHeadPayload
}

interface SSRHeadPayload {
  headTags: string
  bodyTags: string
  bodyTagsOpen: string
  htmlAttrs: string
  bodyAttrs: string
}

Although the hook type permits a promise, head.render() does not await it. Inspect or change the payload synchronously.

Usage Example

import { defineHeadPlugin } from 'unhead/plugins'

export const renderedSizePlugin = defineHeadPlugin({
  key: 'rendered-size',
  hooks: {
    'ssr:rendered': ({ html }) => {
      const size = html.headTags.length + html.bodyTagsOpen.length + html.bodyTags.length
      console.log(`Rendered head payload: ${size} characters`)
    }
  }
})

Cache the payload returned by head.render() rather than storing request-specific data on globalThis.

Did this page help you?