GitHub v1.3.1
Lesson 08

The Client Runtime

How Coralite bridges the Serialization Boundary to bring your components to life in the browser.

The Serialization Boundary

In Coralite, your component code is split into two distinct execution contexts. The server() block runs in Node.js at build time. The client() block is serialized and runs in the browser.

Critically, the server() block is physically stripped from the client bundle via AST analysis. This is the Serialization Boundary. Server secrets can never leak to the browser.

html
Code copied!
  <template id="counter">
    <button ref="incrementBtn">+1</button>
    <span>{{ count }}</span>
  </template>
  
  <script type="module">
    export function server () {
      return { count: 0 }
    }
  
    export async function client ({ state, refs, signal }) {
      refs('incrementBtn').addEventListener('click', () => {
        state.count++
      }, { signal })
    }
  </script>

The client() Context

The client() function receives a single unified context object. The most important keys are refs, state, and signal.

javascript
Code copied!
  export async function client ({ state, refs, signal, emit }) {
    // state: The reactive state object
    // refs: A function to query elements by their ref attribute
    // signal: An AbortSignal that fires when the component disconnects
    // emit: A function to dispatch custom events
  }

Targeting DOM Nodes with refs()

Never use document.querySelector or global DOM queries inside your components. They break encapsulation and are slow. Use the refs() function to target elements within your component's scope.

Forbidden:

javascript
Code copied!
  // ❌ Breaks encapsulation
  document.querySelector('.my-button').addEventListener('click', fn)

Required:

javascript
Code copied!
  // ✅ Scoped, fast, and safe
  refs('my-button').addEventListener('click', fn, { signal })