GitHub v1.3.1
Lesson 11

Server-Side Rendering

How Coralite fetches data at build time and hydrates components without shipping server code to the browser.

The server() Block

The server() block is an asynchronous function that runs exclusively in Node.js during the build phase. It is the perfect place to fetch data from a database, read files, or call external APIs.

html
Code copied!
  <template id="user-profile">
    <div class="profile">
      <h2>{{ name }}</h2>
      <p>{{ role }}</p>
    </div>
  </template>
  
  <script type="module">
    export async function server () {
      // This code runs ONLY during the build
      const fs = await import('node:fs/promises')
      const raw = await fs.readFile('data/user.json', 'utf8')
      const user = JSON.parse(raw)
    
      // Return data to hydrate state
      return {
        name: user.name,
        role: user.role
      }
    }
  </script>

The Serialization Bridge

Once the server() block returns its data, Coralite renders the component's HTML and injects a JSON hydration script into the page. This allows the client to resume with the exact same state.

html
Code copied!
  <user-profile data-cid="user-profile-0">
    <div class="profile">
      <h2>Alice</h2>
      <p>Administrator</p>
    </div>
  </user-profile>
  
  <script type="application/json" data-coralite-hydration="">
    {"name":"Alice","role":"Administrator"}
  </script>

The Stripping Phase

After the HTML is generated, the Coralite compiler physically removes the server() block from the component module. When the JavaScript is shipped to the browser, all server-side logic—including any secrets, database credentials, or file paths—is gone.

This is the Structural Guarantee of Coralite: server code cannot leak to the client.