Skip to content

Server

The server.ts file is the server-side rendering (SSR) entry point for your section. It renders your Vue component to HTML on the server, producing the initial markup that is sent to the browser before JavaScript loads.

How It Works

During a build or server request, the SSR entry point:

  1. Initializes a Vue app instance with your section component
  2. Receives the context (store info, global design) and data (content, design values)
  3. Renders the component to an HTML string using Vue's server renderer
  4. Returns the HTML along with the serialized state for client-side hydration

This ensures the section is visible immediately on page load, before any JavaScript executes.

Usage

typescript
// server.ts
import { createVueServerApp } from '@lightspeed/crane-api';
import Section from './Section.vue';
import type { Content, Design } from './type';

export default createVueServerApp<Content, Design>(Section);

The generic parameters <Content, Design> provide type safety for the data passed during rendering.

Extensions

createVueServerApp accepts an optional second argument for hooking into the app lifecycle:

typescript
createVueServerApp<Content, Design>(Section, {
  init(app) {
    // Called after the Vue app is created, before rendering.
    // Use this to register plugins, directives, or global components.
  },
  render(app, context, data) {
    // Called just before the component renders to HTML.
    // Access the context (store info, global design) and data (content, design).
  },
});

All extension callbacks are optional.

💡 Recommended

For most sections, the basic usage without extensions is sufficient. Extensions are useful when you need to register Vue plugins or perform setup before the render pass.

SSR Considerations

Since server-side rendering runs in Node.js, browser APIs are not available:

  • No window, document, or navigator — guard any browser-specific code with typeof window !== 'undefined' or move it to onMounted in your Vue component
  • No event listeners — attach them in onMounted on the client side
  • No DOM measurements — use safe defaults during SSR and update in onMounted

See Best Practices — SSR vs Client-Only Rendering for patterns.

  • Client — Client-side hydration entry point
  • UI Composables — Accessing content and design data in your Vue component