Skip to content

@ogygia/core

Core

@ogygia/core is the runtime, the server handle and the plugin API behind ogygia. Apps get it through ogygia; plugin authors build on it, the way every other ogygia package does.

On this page

@ogygia/core holds ogygia’s browser runtime, its server handle, and the plugin API that every other ogygia package is built on. @ogygia/compiler holds the build side, including definePlugin. Apps don’t install either one themselves: they install ogygia, which depends on both and re-exports them subpath for subpath, so ogygia/server and @ogygia/core/server are the same module, and so are ogygia/vite and @ogygia/compiler/vite.

This section is for people who write a plugin, or want to read how the packages work. Content, flags, postrender, federation, the server router, devtools and testing are all plugins on this API: a Vite plugin the app lists beside ogygia(), a server half that runs inside ogygia.handle(), and a browser half that boots with the page’s runtime.

Early adopters

The plugin API is internal: ogygia’s own packages are its only users so far, and its shape can change in any release, a patch included, with no deprecation. You can build on it today, but pin the exact ogygia version your plugin was written against, and expect to update it when you upgrade.

Installation

A plugin package lists @ogygia/compiler and @ogygia/core as peer dependencies, so it runs against the copies the app’s ogygia brought in, never a second copy of its own. Since the API can change in a patch release, name the exact version you wrote and tested the plugin against.

{
  "name": "ogygia-plugin-powered-by",
  "type": "module",
  "exports": { "./vite": "./dist/vite.js" },
  "files": ["dist"],
  "peerDependencies": {
    "@ogygia/compiler": "0.16.2",
    "@ogygia/core": "0.16.2"
  }
}

First plugin

A plugin is the Vite plugin that definePlugin returns. It names its server half by location, and the server half’s response hook adds a header to each response in place, pages and endpoints alike. Returning a new Response would replace the page and drop its streamed tail, and a response an endpoint got from fetch() has headers you can’t change, which is what the try is for. Server hooks explains both.

The app installs the package and lists the plugin beside ogygia(), before sveltekit(). Nothing goes in its hooks: ogygia.handle() runs the server half.

import {
  definePlugin,
  type OgygiaPlugin
} from '@ogygia/compiler/vite';

export function poweredBy(): OgygiaPlugin {
  return definePlugin({
    name: 'ogygia-plugin-powered-by',
    server: import.meta.resolve('./server.js')
  });
}
import { defineServer } from '@ogygia/core/plugin';

export default defineServer({
  response(ctx, res) {
    try {
      res.headers.set('x-powered-by', 'ogygia');
    } catch {
      // a response an endpoint passed on from fetch() has headers that can't change
    }
  }
});
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { poweredBy } from 'ogygia-plugin-powered-by/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [ogygia(), poweredBy(), sveltekit()]
});

How it works

A plugin is three pieces, each in its own file, so server code never reaches a browser bundle:

PieceMade withRunsPage
The plugindefinePlugin from @ogygia/compiler/viteIn the build, when Vite loads its config: options, needs, build contributions.Plugins
The server halfdefineServer from @ogygia/core/pluginInside ogygia.handle(): once at start, then at each stage of every request.Server hooks
The browser halfdefineClient from @ogygia/core/plugin/clientIn the page’s runtime: once at boot, then at the runtime’s moments.Browser hooks

The build finds every plugin in the app’s plugins list, checks it, and imports its halves statically into the server bundle and the runtime’s boot. Nothing is looked up at run time. A plugin that isn’t listed adds nothing, and a hook that no plugin uses folds out of ogygia’s own code.

Plugins work together without importing each other. A plugin calls a shared hook that others implement (app.notify.invalidate(tags) clears every store that holds those pages), and writes or reads request facts that any code on the request can see (a flag read lands in facts.reads, which postrender checks before it stores a page). Contexts and facts covers both.

Package contents

PageWhat it covers
PluginsdefinePlugin and its fields, plugin order, needs, Vite hooks of your own, shipping a plugin, and the build errors.
Build contextEverything build(b) can ask for: route config keys, macros, markup formats, transforms, page modules and more.
Server hooksdefineServer, the request stages in order with their contracts, shared hooks, and an app’s own HTML pass.
Contexts and factsapp and ctx, signing and capabilities, per-visitor reads, and the request facts.
Browser hooksdefineClient, boot, and every runtime hook with its event and timing.
HelpersRegion values, late regions, route config for a programmatic router, component CSS, and plugin/utils.
Plugin testsstartPlugin, inRequest and the route config helpers, for unit tests without a server.
APIThe generated reference for /plugin, /plugin/client, /plugin/css, /plugin/utils and /plugin/testing.

Integrations

  • Regions: a server half can make the same region values an app renders with <Region> (Helpers). That’s how federation turns another app’s page into a region on yours.
  • @ogygia/postrender is the fullest example of the server stages: it answers in page, stores in response, and implements invalidate.
  • @ogygia/devtools uses handle to wrap the whole request, b.events() and b.measure() for its data, and a browser half that subscribes to most runtime hooks.
  • @ogygia/tsdown-plugin-svelte builds a plugin package that ships Svelte components.
  • Packaging covers shipping marked components and route tables in an npm package, which a plugin may also do.

Moved sections

These sections used to be on this page under other addresses: