GitHub v1.3.1

slots

Stable

Intercept and transform projected child content on the server before it is rendered.

TypeScript Signature

typescript
Code copied!
  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.

javascript
Code copied!
  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.

javascript
Code copied!
  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.

javascript
Code copied!
  export const getters = {
    hasIcon: ({ slots }) => slots.has('icon')
  }