---
name: nuustudio-addon
description: Build, extend or debug a NuuStudio add-on (a plugin for the NuuStudio page builder on EmDash, built with @nuustudio/sdk). Use when someone asks to make a NuuStudio plugin or add-on, add a custom module / widget / element to the NuuStudio builder, add NuuStudio settings, data sources, display-condition rules, dynamic content, members-style page access or starter layouts, or to fix an add-on that doesn't show up in the builder.
---

# Building a NuuStudio add-on

A NuuStudio add-on is an ordinary **EmDash plugin** that also registers builder parts with NuuStudio through **`@nuustudio/sdk`**. The CMS does what it does well (routes, storage, permissions, hooks, its own MCP tools); NuuStudio picks up the builder parts (modules, settings form, data, rules, dynamic content, page access, layouts, admin pages).

Add-ons import **only** `@nuustudio/sdk` (and `emdash` in the plugin entry), never NuuStudio's own files. NuuStudio lends its helpers at runtime through the **kit**.

Full contract: [REFERENCE.md](REFERENCE.md). Working examples in the NuuStudio repo: `packages/nuustudio-events` (small), `packages/nuustudio-members` (everything), `packages/nuustudio-grammar` (browser-only text checker).

## Workflow

1. **Scaffold** (don't hand-write the boilerplate):
   ```bash
   npx @nuustudio/sdk create <id> --label "<Name>" --package <npm-name> [--dir <path>]
   ```
   `<id>` is lowercase-with-hyphens, unique, and becomes the EmDash plugin id. Inside the NuuStudio monorepo use `--dir packages/<id>` (the SDK is then linked as `workspace:*`).
2. **Design the add-on in `src/addon.ts`**: modules, settings, and any of `ruleTypes`, `dynamicSources`, `pageAccess`, `layouts`, `adminPages`, `privateCookies`. This file ships to the browser: no secrets, no `emdash` import, no Node APIs.
3. **Server parts in `src/server.ts`**: `dataProviders` (load data for modules) and `viewer` (who is visiting). Secrets and network calls go here.
4. **Plugin entry `src/index.ts`**: `emdashAddon(addon, { package, server, plugin: { routes, storage, hooks, capabilities, admin } })`. Export the descriptor factory, `createPlugin` and `default`.
5. **Builder entry `src/admin.ts`**: `registerAddon(addon)` (plus `{ client: { textCheckers } }` for a checker), and `export const pages = { … }` for admin pages.
6. **Install in the site**: add the dependency, then the plugin next to `nuuStudio()` in `astro.config.mjs`. Restart (`astro.config` and plugin server entries aren't hot-reloaded).
7. **Verify** (see the checklist below). Don't claim it works before you've checked.

## Rules that break things when ignored

- **Names.** Module names, rule type ids, dynamic source ids and layout ids must equal the add-on id or start with `<id>-`. Built-in modules never contain a hyphen, so this can't clash. `defineAddon` / `registerAddon` throw on mistakes.
- **`render(settings, ctx)` returns an HTML string** and runs on the server *and* in the builder canvas: no DOM, no `window`, no async. Escape every value with `esc()`, pass URLs through `safeUrl()`. Read provider data with `addonData(ctx)` (undefined while loading) and the visitor with `addonViewer(ctx, id)` (undefined in the builder).
- **`runtime` is a JavaScript function *body* string**, run once per module instance with `el` (module root) and `Dash`. ES2018, no imports, no template literals. Call your own routes with `Dash.addon("<id>").post("route", body)` (never hard-code `/_emdash/api/plugins/…`). Stop intervals when `Dash.alive && !Dash.alive(el, el.firstElementChild)` (the builder re-rendered the module).
- **Settings:** declare `settings.fields`; `type: "secret"` values are encrypted by EmDash and only a hint (`••••1234`) ever reaches the browser. Data providers and the viewer get decrypted values in `ctx.settings`.
- **Field groups.** Keys must be unique per module. `tab` is `content`, `design` or `advanced`. Use `design: { heading: " .sel", text: " .sel", button: " .sel" }` to get NuuStudio's own typography / button groups on your elements instead of building them; `design.text: false` drops the default text group.
- **Need NuuStudio's helpers** (text style groups, button groups, the Contact Form field builder, form engine, icons, responsive CSS props, rule matching)? Make `modules` a function: `modules: (kit) => [ … ]` and use `kit.fields.*`, `kit.forms.*`, `kit.html.*`, `kit.css.*`, `kit.rules.text`. Don't copy NuuStudio code.
- **Server-only code never goes in `addon.ts`.** If the builder bundle pulls in `emdash`, `node:*` or a secret, it's in the wrong file.
- **Your own routes / storage / hooks** belong in `plugin` of `emdashAddon` (exactly as with EmDash's `definePlugin`). Routes live at `/_emdash/api/plugins/<id>/<route>`. Declared `storage` is what Settings → Add-ons → Uninstall deletes, with your `plugin:uninstall` hook run first.
- **Turned off ≠ removed.** When the plugin is turned off (Settings → Add-ons or EmDash's Plugins screen) NuuStudio ignores the add-on everywhere; its elements stay on pages and come back when it's turned on. Don't add your own on/off flag.

## Verify

```bash
pnpm typecheck && pnpm build          # in the add-on
```
Then, with the site running (restart it after changing `astro.config.mjs` or the add-on's `src/index.ts`):

1. NuuStudio → Settings → **Add-ons** lists it as **On** and not "Check install" (that means only the server or only the builder registered it: missing `adminEntry` / `admin.ts`, or the plugin isn't in `astro.config.mjs`).
2. The module is in the builder's Insert panel, renders on the canvas, and its settings show.
3. A published page using it renders on the live site (view the HTML; check data providers by changing a setting).
4. Your routes answer at `/_emdash/api/plugins/<id>/…`; with the plugin turned off they answer 404 and the module disappears from pages.
5. The browser console and the dev server log show no errors.

## Common problems

| Symptom | Cause |
| --- | --- |
| "Check install" in Settings → Add-ons | Registered on one side only: add the plugin to `astro.config.mjs`, keep `adminEntry` pointing at `src/admin.ts`, restart |
| Module missing from the Insert panel | `modules` is a function but threw (see the console), `hidden: true`, or the add-on is turned off |
| `Missing add-on module "x"` on the canvas | Module renamed, or the add-on isn't installed / registered |
| Data never arrives | Provider name in `data()` doesn't match `server.dataProviders`, or the provider threw (dev server log) |
| Runtime doesn't run | It isn't a function *body* string, uses syntax newer than ES2018, or throws (console shows `[Dash] <module>`) |
| Secret shows as empty | Read it on the server (`ctx.settings`), never in `addon.ts` |
