GitHub v1.3.1

Page API

Stable

Pages are thin orchestrators. They compose components and define the routing manifest.

The definePage Syntax (Legacy)

Components and pages share the same export surface: top-level named exports. Pages are entry points, not Web Components, so export default definePage({ ... }) is retained as a legacy shape; new pages use export function server, export const meta, and the other named exports.

html
Code copied!
  <html lang="en">
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <site-header></site-header>
    <main>
      <h1>{{ title }}</h1>
    </main>
    <site-footer></site-footer>
  </body>
  </html>
  
  <script type="module">
    import { definePage } from 'coralite'
  
    export default definePage({
      getters: {
        title: () => 'Home Page'
      }
    })
  </script>

Valid Page Blocks

Pages support a subset of the component API:

  • paths: For dynamic routing.
  • server: For SSR data fetching.
  • getters: For derived state.
  • style: For page-level CSS variables.
  • provide: For tree-based dependency injection.
  • client: For page-level interactivity.
  • onError: For page-level error boundaries.

Pages do not support attributes, slots, consume, or formControl.

Dynamic Routing with paths()

The paths() function allows you to generate multiple HTML files from a single page template. This is how you build a blog or a documentation site with hundreds of pages without duplicating the layout.

javascript
Code copied!
  <script type="module">
    import { definePage } from 'coralite'
  
    export default definePage({
      async paths () {
        const fs = await import('node:fs/promises')
        const files = await fs.readdir('src/data/blog')
    
        return files.map(file => ({
          path: `/blog/${file.replace('.json', '')}`,
          params: { id: file.replace('.json', '') }
        }))
      },
    
      async server ({ params }) {
        const fs = await import('node:fs/promises')
        const data = await fs.readFile(`src/data/blog/${params.id}.json`, 'utf8')
        return JSON.parse(data)
      }
    })
  </script>

The Page Boundary

Pages are strictly consumers of components. They must not query component internals or reach into their shadow/light DOM. The component's client() block is the only place where internal logic should live.

CORALITE-PAGE-201: Encapsulation Violation

Do not use document.querySelector or DOM manipulation inside a page's client() block to target elements inside a component. If a page needs to interact with a component, it should pass data via attributes and listen for custom events using emit().