slots
Intercept and transform projected child content on the server before it is rendered.
TypeScript Signature
export const slots: Record<string, ( context: { nodes: any[]; state: record<string, any>;
isServer: boolean;
isClient: boolean;
slots: Record<string, any>;
}
) => any[] | undefined>
</string,></string,>
Server-Side Transformation
The slots export runs exclusively during the SSR build phase. It intercepts the raw DOM nodes projected into a slot, allows you to manipulate them (or extract text), and returns the transformed array of nodes that will be rendered into the final HTML.
This is how the code-block component applies syntax highlighting to its children.
export const slots = {
default: ({ nodes, state }) => {
// Guard against client execution
if (typeof window !== 'undefined') return
// Extract text and highlight it
let text = extractText(nodes)
const highlighted = hljs.highlight(text, { language: 'html' })
// Return new AST nodes
return parseHTML(highlighted.value)
}
}
The Code-Block Example
The code-block component is the perfect case study for this API. It extracts the raw text from the slot, strips leading indentation, runs it through highlight.js, and replaces the original text nodes with syntax-highlighted HTML nodes.
export const slots = {
default: ({ nodes }) => {
// 1. Ensure this only runs on the server
if (typeof window !== 'undefined') return
// 2. Process the content...
return processedNodes
}
}
Checking Slot Presence
Inside getters or client logic, you can check if a specific slot has content by using the slots.has() method on the context object.
export const getters = {
hasIcon: ({ slots }) => slots.has('icon')
}