CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vue--server-renderer

Server-side rendering library for Vue.js applications with support for streaming and multiple environments

Pending
Overview
Eval results
Files

internal-helpers.mddocs/

Internal Runtime Helpers

Advanced compilation helpers and internal utilities for custom template compilation and specialized SSR scenarios. These functions are primarily used by Vue's compiler output but can be useful for advanced use cases, custom renderers, and library authors.

Capabilities

Core Rendering Helpers

ssrRenderVNode

Core function for rendering individual VNodes to SSR buffer. This is the foundation of all other rendering functions.

/**
 * Renders a VNode to SSR buffer with optional parent component context
 * @param vnode - VNode to render
 * @param parentComponent - Optional parent component instance for context
 * @param slotScopeId - Optional slot scope ID for scoped slots
 * @returns SSR buffer or Promise resolving to SSR buffer
 */
function ssrRenderVNode(
  vnode: VNode,
  parentComponent?: ComponentInternalInstance | null,
  slotScopeId?: string
): SSRBuffer | Promise<SSRBuffer>;

type SSRBuffer = SSRBufferItem[] & { hasAsync?: boolean };
type SSRBufferItem = string | SSRBuffer | Promise<SSRBuffer>;
type Props = Record<string, unknown>;

type SSRSlots = Record<string, SSRSlot>;
type SSRSlot = (
  props: Props,
  push: PushFn,
  parentComponent: ComponentInternalInstance | null,
  scopeId: string | null,
) => void;

type PushFn = (item: SSRBufferItem) => void;

ssrRenderComponent

Renders a component instance to SSR output.

/**
 * Renders a component to SSR buffer
 * @param comp - Component to render
 * @param props - Component props
 * @param children - Component children (slots)
 * @param parentComponent - Parent component context
 * @param slotScopeId - Slot scope ID
 * @returns Rendered component output
 */
function ssrRenderComponent(
  comp: Component,
  props: Props | null,
  children: Slots | SSRSlots | null,
  parentComponent: ComponentInternalInstance | null,
  slotScopeId?: string
): SSRBuffer | Promise<SSRBuffer>;

Attribute Rendering Helpers

ssrRenderAttrs

Renders all attributes for an element, handling special cases like class, style, and boolean attributes.

/**
 * Renders all attributes for an element as a string
 * @param props - Props object containing attributes
 * @param tag - Optional HTML tag name for tag-specific handling
 * @returns String containing all rendered attributes
 */
function ssrRenderAttrs(
  props: Record<string, unknown>,
  tag?: string
): string;

Usage Examples:

import { ssrRenderAttrs } from "@vue/server-renderer";

// Basic attribute rendering
const attrs = ssrRenderAttrs({
  id: 'my-element',
  class: ['active', 'primary'],
  'data-value': 42,
  disabled: true
});
console.log(attrs);
// Output: ' id="my-element" class="active primary" data-value="42" disabled'

// Tag-specific handling
const textareaAttrs = ssrRenderAttrs({
  value: 'Some text',
  rows: 10
}, 'textarea');
// 'value' is excluded for textarea tags

Individual Attribute Helpers

/**
 * Renders a single attribute
 * @param key - Attribute name
 * @param value - Attribute value
 * @param tag - Optional HTML tag for context
 * @returns Rendered attribute string
 */
function ssrRenderAttr(key: string, value: unknown, tag?: string): string;

/**
 * Renders a dynamic attribute with proper escaping
 * @param key - Attribute name
 * @param value - Attribute value
 * @param tag - Optional HTML tag for context
 * @returns Rendered attribute string
 */
function ssrRenderDynamicAttr(key: string, value: unknown, tag?: string): string;

/**
 * Renders class attribute with array/object support
 * @param value - Class value (string, array, or object)
 * @returns Normalized class string
 */
function ssrRenderClass(value: unknown): string;

/**
 * Renders style attribute with object support
 * @param value - Style value (string or object)
 * @returns Normalized style string
 */
function ssrRenderStyle(value: unknown): string;

Content Rendering Helpers

ssrInterpolate

Safely interpolates values for SSR output with proper HTML escaping.

/**
 * Safely interpolates a value for SSR output
 * @param value - Value to interpolate
 * @returns HTML-escaped string representation
 */
function ssrInterpolate(value: unknown): string;

Usage Examples:

import { ssrInterpolate } from "@vue/server-renderer";

// Safe interpolation with escaping
console.log(ssrInterpolate('<script>alert("xss")</script>'));
// Output: "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"

console.log(ssrInterpolate({ name: 'John', age: 30 }));
// Output: "[object Object]"

console.log(ssrInterpolate(['a', 'b', 'c']));
// Output: "a,b,c"

console.log(ssrInterpolate(null));
// Output: ""

console.log(ssrInterpolate(undefined));
// Output: ""

ssrRenderList

Renders v-for loops in SSR context.

/**
 * Renders a list using v-for logic
 * @param source - Source data (array, object, string, or number)
 * @param renderItem - Function to render each item
 * @returns Array of rendered items
 */
function ssrRenderList(
  source: any,
  renderItem: (value: any, key: any, index: number) => any
): any[];

Usage Examples:

import { ssrRenderList, ssrInterpolate } from "@vue/server-renderer";

// Render array
const items = ['apple', 'banana', 'cherry'];
const listHtml = ssrRenderList(items, (item, index) => {
  return `<li key="${index}">${ssrInterpolate(item)}</li>`;
}).join('');
console.log(listHtml);
// Output: <li key="0">apple</li><li key="1">banana</li><li key="2">cherry</li>

// Render object
const user = { name: 'John', age: 30, city: 'New York' };
const userFields = ssrRenderList(user, (value, key) => {
  return `<div>${key}: ${ssrInterpolate(value)}</div>`;
}).join('');

Slot and Teleport Helpers

ssrRenderSlot

Renders slot content in SSR context.

/**
 * Renders slot content with fallback support
 * @param slots - Slots object (regular or SSR slots)
 * @param slotName - Slot name
 * @param slotProps - Props to pass to slot
 * @param fallbackRenderFn - Fallback content function
 * @param push - Push function for SSR buffer
 * @param parentComponent - Parent component context
 * @param slotScopeId - Slot scope ID
 */
function ssrRenderSlot(
  slots: Slots | SSRSlots,
  slotName: string,
  slotProps: Props,
  fallbackRenderFn: (() => void) | null,
  push: PushFn,
  parentComponent: ComponentInternalInstance,
  slotScopeId?: string
): void;

/**
 * Internal slot rendering helper without fragment wrapping
 * @param slots - Slots object (regular or SSR slots)
 * @param slotName - Slot name
 * @param slotProps - Props to pass to slot
 * @param fallbackRenderFn - Fallback content function
 * @param push - Push function for SSR buffer
 * @param parentComponent - Parent component context
 * @param slotScopeId - Slot scope ID
 * @param transition - Whether slot is in transition
 */
function ssrRenderSlotInner(
  slots: Slots | SSRSlots,
  slotName: string,
  slotProps: Props,
  fallbackRenderFn: (() => void) | null,
  push: PushFn,
  parentComponent: ComponentInternalInstance,
  slotScopeId?: string,
  transition?: boolean
): void;

ssrRenderTeleport

Handles teleport components in SSR context.

/**
 * Renders teleport content to appropriate targets
 * @param parentPush - Parent push function
 * @param content - Teleport content renderer
 * @param target - Teleport target selector
 * @param disabled - Whether teleport is disabled
 * @param parentComponent - Parent component context
 */
function ssrRenderTeleport(
  parentPush: PushFn,
  content: () => void,
  target: string | null,
  disabled: boolean,
  parentComponent: ComponentInternalInstance
): void;

ssrRenderSuspense

Handles Suspense components in SSR context.

/**
 * Renders Suspense component with async content handling
 * @param push - Push function for SSR buffer
 * @param suspenseInstance - Suspense component instance
 */
function ssrRenderSuspense(
  push: PushFn,
  suspenseInstance: ComponentInternalInstance
): void;

Form Input Helpers

v-model Helpers

Special helpers for handling v-model directives in SSR context.

/**
 * Loose equality comparison for v-model
 * @param a - First value
 * @param b - Second value
 * @returns Whether values are loosely equal
 */
function ssrLooseEqual(a: unknown, b: unknown): boolean;

/**
 * Checks if array loosely contains a value
 * @param arr - Array to search
 * @param value - Value to find
 * @returns Whether array contains the value
 */
function ssrLooseContain(arr: unknown[], value: unknown): boolean;

/**
 * Renders dynamic v-model attributes based on input type
 * @param type - Input type (radio, checkbox, etc.)
 * @param model - Model value
 * @param value - Input value
 * @returns Appropriate attribute string
 */
function ssrRenderDynamicModel(
  type: unknown,
  model: unknown,
  value: unknown
): string;

/**
 * Gets dynamic model properties for v-bind + v-model
 * @param existingProps - Existing props object
 * @param model - Model value
 * @returns Properties to merge with existing props
 */
function ssrGetDynamicModelProps(
  existingProps: any,
  model: unknown
): { checked: true } | { value: any } | null;

Usage Examples:

import { 
  ssrRenderDynamicModel, 
  ssrGetDynamicModelProps 
} from "@vue/server-renderer";

// Radio button v-model
const radioAttr = ssrRenderDynamicModel('radio', 'selected', 'option1');
console.log(radioAttr); // ' checked' if model === 'option1'

// Checkbox v-model with array
const checkboxAttr = ssrRenderDynamicModel('checkbox', ['a', 'b'], 'b');
console.log(checkboxAttr); // ' checked'

// Dynamic props
const props = ssrGetDynamicModelProps({ type: 'text' }, 'hello');
console.log(props); // { value: 'hello' }

Directive Helpers

ssrGetDirectiveProps

Extracts props from directive bindings for SSR rendering.

/**
 * Extracts props from directive bindings
 * @param instance - Component instance
 * @param dir - Directive binding
 * @param value - Directive value
 * @param arg - Directive argument
 * @param modifiers - Directive modifiers
 * @returns Props object from directive
 */
function ssrGetDirectiveProps(
  instance: ComponentInternalInstance,
  dir: DirectiveBinding,
  value?: any,
  arg?: string,
  modifiers?: Record<string, boolean>
): Record<string, any> | null;

ssrIncludeBooleanAttr

Determines whether boolean attributes should be included.

/**
 * Determines if a boolean attribute should be included
 * @param value - Attribute value
 * @returns Whether to include the attribute
 */
function ssrIncludeBooleanAttr(value: unknown): boolean;

Advanced Usage Examples

Custom Renderer

import { 
  ssrRenderVNode, 
  ssrRenderAttrs, 
  ssrInterpolate 
} from "@vue/server-renderer";
import { h } from "vue";

// Custom rendering function
async function customRender(vnode: VNode): Promise<string> {
  const buffer = await ssrRenderVNode(vnode);
  
  // Convert buffer to string (simplified)
  function bufferToString(buf: SSRBuffer): string {
    return buf.map(item => 
      typeof item === 'string' ? item : bufferToString(item)
    ).join('');
  }
  
  return bufferToString(buffer);
}

// Usage
const vnode = h('div', { class: 'custom' }, [
  h('h1', 'Custom Renderer'),
  h('p', 'Rendered with internal helpers')
]);

const html = await customRender(vnode);

Template Compilation Integration

import { 
  ssrRenderAttrs, 
  ssrInterpolate, 
  ssrRenderList 
} from "@vue/server-renderer";

// Compiled template function (generated by Vue compiler)
function render(_ctx: any, _push: PushFn) {
  _push('<div');
  _push(ssrRenderAttrs({ 
    class: _ctx.containerClass,
    id: _ctx.elementId 
  }));
  _push('>');
  
  _push('<h1>');
  _push(ssrInterpolate(_ctx.title));
  _push('</h1>');
  
  _push('<ul>');
  ssrRenderList(_ctx.items, (item, index) => {
    _push('<li key="');
    _push(ssrInterpolate(index));
    _push('">');
    _push(ssrInterpolate(item.name));
    _push('</li>');
  });
  _push('</ul>');
  
  _push('</div>');
}

Performance Optimization

import { ssrRenderAttrs, ssrInterpolate } from "@vue/server-renderer";

// Cache frequently used attribute combinations
const attrCache = new Map();

function cachedRenderAttrs(props: Record<string, unknown>): string {
  const key = JSON.stringify(props);
  if (!attrCache.has(key)) {
    attrCache.set(key, ssrRenderAttrs(props));
  }
  return attrCache.get(key);
}

// Batch interpolation for arrays
function batchInterpolate(values: unknown[]): string[] {
  return values.map(ssrInterpolate);
}

Security Considerations

All internal helpers include proper HTML escaping and XSS protection:

import { ssrInterpolate, ssrRenderAttrs } from "@vue/server-renderer";

// All user content is automatically escaped
const userInput = '<script>alert("xss")</script>';
const safeOutput = ssrInterpolate(userInput);
// Output: "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"

// Attribute values are also escaped
const attrs = ssrRenderAttrs({
  title: 'Hello "world"',
  'data-content': '<div>content</div>'
});
// Output: ' title="Hello &quot;world&quot;" data-content="&lt;div&gt;content&lt;/div&gt;"'

Integration Notes

These helpers are designed to work with:

  • Vue's template compiler output
  • Custom rendering systems
  • Server-side template engines
  • Build-time optimizations
  • Development tools and debugging

They provide the building blocks for all higher-level rendering functions and maintain consistency with Vue's client-side rendering behavior.

Install with Tessl CLI

npx tessl i tessl/npm-vue--server-renderer

docs

index.md

internal-helpers.md

nodejs-integration.md

streaming-rendering.md

string-rendering.md

web-streams.md

tile.json