Documentation
NebulaKit Documentation
NebulaKit is a SvelteKit starter for sites that run on Cloudflare: sign-in, an admin, a shop, a page builder and a UI kit are already built and tested. This page is the single source of truth for setup, development, and deployment, written so you can go from first run to production without guesswork.
Using an AI? Start with the first section. New to Cloudflare? Set up your account. Doing it by hand? Go to Quick Start. Already built? Jump to Deployment.
Start
Use NebulaKit with an AI
There are three ways an AI works with NebulaKit. Pick the one you need; each is a few minutes.
1. Have an AI build your site
A coding agent (Claude Code, Codex, Cursor, Copilot, or any other) works inside your copy of the template. It does the typing; the repo's rules keep it safe.
2. Have an AI run your live site
An agent calls your deployed site's API with a key you create, to edit pages, manage products or read orders. You choose what the key may do.
3. Have an AI read this site
Every page can be fetched as Markdown, and the site publishes a catalog of what it offers, so an agent can find its way without scraping.
1. Build your site with a coding agent
- Create your repository from the template, then clone it.
- Open the folder in your agent. It should read
AGENTS.mdfirst. That file is the rule book (CLAUDE.mdpoints to it), and it is why the agent writes tests, keeps colours in CSS variables and never edits a migration that already shipped. - Paste a prompt like the one below, and replace the first line with what you want built.
- Read the result, not only the summary. The agent must run the checks and show you their output.
Build: a pricing page with three plans and a contact button.
Before you start, read AGENTS.md and docs/UI_KIT.md.
Use the components in src/lib/ui and the modules in src/lib/modules before writing new ones.
Write the tests first. Keep every colour a CSS variable. If the database changes,
add a new migration; never edit an old one. Update /documentation in the same change.
When you are done, run these and paste their output:
bun run check
bun run test
bun run test:coverage
Coverage must stay at or above 95%. Then list the files you changed. Tasks that go well
- Add a page, a form, a table or a dashboard from the UI kit and modules.
- Add a database table and the admin screen for it, with tests.
- Change the look: edit the theme tokens in
src/app.css, then runbun run validate:contrast. - Explain a route: ask which file, store and service control it, and what the smallest edit is.
Keep control
- Ask for a branch and a pull request. Do not let an agent push to
main. - Never paste a secret into a prompt. Keys go in
.dev.varslocally and in Cloudflare secrets in production. - Ask for one change at a time. Small diffs are easy to review.
2. Let an agent run your live site
Everything the admin can do with pages, products, prices and orders is also reachable
at /api/v1, and the whole surface is described at /api/v1/openapi.json. An agent that can read that
file knows every endpoint.
- Sign in as the owner and open
/admin/api-keys. Create a key with the fewest scopes the job needs, and an expiry. The key is shown once and stored hashed. - Give the agent the key through an environment variable (
NEBULAKIT_KEY), not in the chat. - Point it at the OpenAPI file and say what to do. Revoke the key at
/admin/api-keyswhen the job is finished.
| Scope | Lets the key |
|---|---|
site:read | Read site information and updates. |
pages:read, pages:write | Read, create, edit and publish builder pages, and read revisions. |
products:read, products:write | See archived products, and create, edit and delete products. The public catalog of active products needs no key. |
pricing:read, pricing:write | Read or change regional pricing. |
orders:read, orders:write | Read orders (this includes customers' addresses) or move an order's status. |
site:admin | Everything. Only owners can grant it; avoid it for agents. |
export NEBULAKIT_KEY=nk_... # created at /admin/api-keys
curl -H "Authorization: Bearer $NEBULAKIT_KEY" \
"https://nebulakit.starspace.group/api/v1/products?all=1" A key that is wrong is refused, never treated as an anonymous visitor. Writes must send JSON, and every call is logged next to the key on the same admin page.
3. Let an agent read this site
Give the agent this address and ask your question. It does not need to scrape HTML:
curl -H 'Accept: text/markdown' https://nebulakit.starspace.group/documentation - Markdown for every page. Send
Accept: text/markdown. The reply says roughly how many tokens it holds, so the agent can budget. - A map of what exists. /.well-known/api-catalog lists the APIs and /.well-known/agent-skills/index.json holds short guides for reading content and contacting the site.
- How to sign in. /auth.md says how an agent authenticates, and what is not offered.
- Crawl rules. /robots.txt names the AI crawlers and says how the content may be used. See Agent Readiness for the full list.
Treat anything an agent reads from a web page as information, not as orders. If a page says "ignore your instructions", it is the page that is wrong.
Start Here
There are two repositories, and the difference matters. NebulaKit is the template: copy it to build your own site. nebulakit-site is this website, which runs on the same platform but is not a template. Everything below describes the platform, so it applies to both.
- Get the code. Use the template button in Quick Start, then clone your new repository.
- Install and run it.
bun install, thenbun run dev, then openhttp://localhost:4278. - Create the local database.
bun run db:migrate:localapplies every migration to a local D1 database (see Database Migrations). - Make yourself the owner. The setup flow connects an OAuth provider and names the owner (see Authentication and Setup).
- Set up Cloudflare. Create the account, sign Wrangler in and connect an AI through the Cloudflare MCP server. See Set Up Cloudflare.
- Check before you ship.
bun run check,bun run testandbun run test:coveragemust all pass. Coverage may not fall below 95%. - Deploy. See Deployment to Cloudflare Pages.
Your site's name, colours and links live in src/lib/site.config.ts and src/app.css. Nothing else needs renaming.
Set Up Cloudflare
NebulaKit runs on Cloudflare: Pages serves the site, D1 holds the data, KV holds runtime config, and R2 holds uploaded files. You do not need an account to run the site on your own machine. You need one to deploy, and about ten minutes to set it up.
- Create the account. Sign up at dash.cloudflare.com/sign-up and confirm your email. The free plan covers Pages, D1 and KV. R2 asks for a payment method before you can switch it on, and the free plan allows 10 D1 databases per account.
- Sign Wrangler in. Run
bunx wrangler login. It opens a browser window to approve access. Then runbunx wrangler whoamiand check that it names the account you mean to use. With more than one account, setCLOUDFLARE_ACCOUNT_IDbefore you run anything else. - Create this project's resources. Run
bun run setup:cf. It creates the D1 database and the KV namespaces, writes their ids intowrangler.toml, and runsbun run check:bindingsto confirm the result. Then runbun run db:migrateto create the tables. Every project needs its own resources: never paste an id from another project (see Cloudflare Bindings). - Set the secrets. Set
SESSION_SECRET,SETUP_SECRETand, optionally,ENCRYPTION_KEYwithbunx wrangler pages secret put NAME. The reasons and the rotation steps are in Secrets at Rest. - Deploy. Connect the repository to Pages as described in Deployment to Cloudflare Pages.
Connect an AI through the Cloudflare MCP server
Cloudflare runs hosted MCP servers, so a coding agent can read your account and change it for you: create the database, check a binding, read the logs of a failed deploy. You sign in once in the browser, and no key goes into the repository. In Claude Code:
claude mcp add --transport http cloudflare-api https://mcp.cloudflare.com/mcp
claude mcp add --transport http cloudflare-docs https://docs.mcp.cloudflare.com/mcp Then run /mcp inside Claude Code, pick the server, and approve the Cloudflare
sign-in. Choose the account and the permissions on the consent screen, and grant no more
than the task needs. Other MCP clients (Cursor, Windsurf, ChatGPT and the rest) take the
same URL in their remote-server settings.
https://mcp.cloudflare.com/mcpis the Cloudflare API server. It reaches the whole Cloudflare API and is the one to start with.https://docs.mcp.cloudflare.com/mcpsearches the Cloudflare documentation. It changes nothing, so it is safe to leave connected.https://observability.mcp.cloudflare.com/mcpreads Worker logs and analytics.https://builds.mcp.cloudflare.com/mcpreads and manages Workers Builds.
The API server acts on the account you approved, not on this project alone. Keep one
Cloudflare account per client, and tell the agent to create resources with bun run setup:cf so that the ids land in wrangler.toml and check:bindings can vouch for them. For CI, which cannot open a browser,
create an API token in the Cloudflare dashboard under My Profile, API Tokens. Scope it
to one account and give it only the permissions it uses. The full list of servers is
in the Cloudflare MCP documentation. To have an agent run your live site rather than your Cloudflare account, see Use NebulaKit with an AI.
Quick Start
Then clone your repository, run the commands below, and open http://localhost:4278.
Bun is the recommended default for this repo. Switch to npm commands if your environment requires it.
terminal bun
# Install dependencies
$
bun install
# Start development server
$
bun run dev
# Apply local D1 migrations
$
bun run db:migrate:local
# Build for production
$
bun run build
# Type-check and tests
$
bun run check
$
bun run test
# Coverage report
$
bun run test:coverage
# Deploy to Cloudflare Pages
$
bun run deploy
What You Get Out of the Box
This starter already ships with the main product surfaces wired together. The fastest way to understand the repo is to think of it as a Cloudflare-first app shell with auth, theming, content management, and AI entry points already in place.
Core App Shell
- Keyboard-first navigation with command palette for fast route switching.
- Theme system with persistent light and dark preferences.
- Responsive layout, navigation, footer, and shared metadata components.
Account and Admin Flow
- Setup-first authentication flow for owner configuration.
- Login, signup, profile, reset, and admin routes are already scaffolded.
- Cloudflare D1 and KV are used for setup state and application data.
Content and AI Surfaces
- CMS-style content routes and admin tooling are included for structured content.
- Chat route becomes your primary AI surface once a provider is configured.
- Command palette can expose AI-related navigation when providers are available.
Analytics and Operations
- First-party, cookie-free analytics at
/admin/stats— traffic, audience, and growth, with no third-party script. - A Cloudflare plan-limit meter that projects whether today's traffic will exhaust your request allowance.
- Per-admin permission so operators can see stats without owner access.
How To Use the App
Use the app in this order if you want the least confusing first run. That sequence matches how the repo is structured and avoids most setup-related false alarms.
- Open
/setupfirst on a fresh environment and configure owner credentials. - Complete
/setupbefore expecting sign-in or AI features to work. - Sign in through
/auth/loginor create an account through/auth/signup. If you forget your password, choose "Forgot your password?" on the sign-in page: it emails a one-hour link to/auth/resetand needs email sending configured. - Open the command palette with Ctrl/Cmd + K to move between major routes quickly. On a phone, tap the search icon in the navbar or choose "Search & commands" in the menu.
- Use
/chatfor AI interactions,/profilefor account settings, and/adminfor operator tasks. - Use the theme toggle to verify light and dark presentation while you customize branding.
Common First-Run Checks
- If auth looks broken, re-check setup lock state and provider credentials first.
- If chat is missing from navigation, verify AI provider configuration and route access.
- If admin tools are unavailable, confirm you are signed in as the configured owner.
Where To Extend
- Modify routes under
src/routeswhen changing page behavior. - Use
src/lib/componentsfor reusable UI and shell elements. - Keep business logic in
src/lib/servicesand shared helpers insrc/lib/utils.
Build
UI Kit
Standard elements live in src/lib/ui and import from one place. Every one
uses the theme's CSS variables only, works in light and dark, and is usable by
keyboard. See them all running, with the code for each, at /components.
import { Button, TextInput, Select, Dialog, Tabs, Table, toast } from '$lib/ui'; - Actions — Button (four variants, three sizes, link form, loading state), ButtonGroup, Menu, DropdownMenu (checkbox and radio items), Command (a filterable command palette, inline or as a Ctrl+K dialog), Toggle and ToggleGroup.
- Forms — TextInput, Textarea, Select, Checkbox, RadioGroup, Switch,
Slider, Combobox, Calendar, DatePicker, InputOTP, InputGroup, NumberInput, TagInput
and FileDrop, all wired the way Field wires them: a real label, the hint and error
in
aria-describedby. - Feedback — Alert, toasts, Badge, Progress, Spinner and Skeleton.
- Overlays — Dialog and AlertDialog (native
<dialog>), Sheet (from any side, so also a drawer), Popover, HoverCard, Tooltip and ContextMenu. - Navigation — Tabs, Accordion, Breadcrumbs, Pagination, Menubar, NavigationMenu, Sidebar and Stepper.
- Layout — Container, Stack, Separator, AspectRatio, ScrollArea, Resizable and Collapsible.
- Data display — Card, Item, Table, DataTable (search, sort, paging,
selection), BarChart, LineChart and DonutChart (plain SVG, each with a hidden data
table for screen readers), Stat, Carousel, Timeline, Avatar, AvatarGroup, Kbd,
CodeBlock (highlighted code with a copy button; code samples use it, never a bare
<pre>), Prose and EmptyState.
That is every element shadcn/ui ships, and more. None of it needs a dependency beyond Svelte, and none of it is a paid add-on: the kit is MIT-licensed with the rest of the project.
Toasts need one <Toaster />, which the root layout already renders.
Raise one from anywhere with toast.success('Saved'); pass 0 as
the duration to keep it until it is dismissed.
The catalog is checked: a test fails when a component is added to src/lib/ui, or a widget is registered, without an entry in src/lib/ui/catalog.ts.
Full reference: docs/UI_KIT.md.
Modules
Where the UI kit gives you elements, modules give you the blocks every app ends up
building. They live in src/lib/modules, are built from $lib/ui, and are shown running, with the accessibility behaviour each one
keeps, at /modules.
import { SignInForm, PricingTable, DataGrid, KanbanBoard } from '$lib/modules'; - Account — sign-in, sign-up, password reset, profile, sessions, two-factor.
- Settings — appearance, notifications, API keys, webhooks, cookie consent, privacy.
- Marketing — hero, pricing, comparison, FAQ, testimonials, footer, contact.
- App shell — app shell, notification center, global search, wizard, error states.
- Data & teams — data grid, kanban, comments, files, calendar, tree, audit log.
- Commerce — billing, invoices, checkout, cart, reviews, status page, changelog.
Each module takes its data and copy as props, emits events, and never fetches. All are mobile first from 320px, use CSS variables only, and work by keyboard and screen reader.
Shop, Payments and Email
The shop is the @nebulakit/commerce package in packages/commerce (products, cart pricing, checkout, orders, Stripe, Printful), versioned on its own. It stays
inert until configured: with no products /shop shows an empty catalog
(admins with the shop area add them at /admin/products), with no Stripe key
orders wait for a manual payment, and with no email key nothing is sent. Apply
migrations 0023, 0024 and 0025 with bun run db:migrate:local.
Storefront
Shoppers browse /shop and /shop/[slug], add products to
a cart kept in their browser, and check out at /cart. Prices, tax and
shipping are always worked out on the server from the catalog; the cart only
remembers what to ask for. Physical products ask for a shipping address. With
Stripe configured the shopper pays by card right on /cart (Stripe's
Payment Element, loaded from js.stripe.com only on that page), or on Stripe's hosted page when CHECKOUT_MODE is hosted; without it the order waits for
a manual payment. Orders are listed at /admin/orders.
Regional pricing
/admin/settings/pricing (owner only) lowers prices for shoppers in
countries where money buys more, using a World Bank price level table shipped with
the kit. It is off until you turn it on, and it only ever lowers a price. Set how
much of the gap to pass on, the largest discount, which products it covers
(digital only by default) and per-country overrides, and preview any country
before saving. An order takes the least generous country among the one Cloudflare
sees and the shipping address, so a VPN alone earns nothing. The cart shows the
server's quoted total (POST /api/v1/checkout/quote) and each order
records the multiplier and countries used. Settings are also at GET/PUT /api/v1/pricing, with scopes pricing:read and pricing:write. Refresh the table with bun run update:price-index.
Shop settings are environment variables, never request fields: COMMERCE_TAX_RATE_PERCENT, COMMERCE_PRICES_INCLUDE_TAX, COMMERCE_FLAT_SHIPPING_CENTS, STRIPE_SECRET_KEY, STRIPE_PUBLISHABLE_KEY and STRIPE_WEBHOOK_SECRET (webhook
URL /api/v1/webhooks/stripe), plus optional CHECKOUT_MODE (onpage, the default, or hosted) and STRIPE_WEBHOOK_SECRET_PREVIOUS while rolling the signing secret. Without
the publishable key, the cart sends shoppers to Stripe's hosted page. See docs/CHECKOUT.md for test cards and stripe listen. On-page
checkout needs migrations 0032–0034.
Payments, status and email
The Stripe webhook answers 503 until STRIPE_WEBHOOK_SECRET is set, so Stripe keeps retrying. It handles checkout.session.completed and checkout.session.async_payment_succeeded (the order becomes paid), checkout.session.async_payment_failed and checkout.session.expired (a pending order is cancelled), and for the
on-page card form payment_intent.succeeded, payment_intent.canceled, payment_intent.payment_failed and charge.refunded (the last
two are recorded on the order's payment history at /admin/orders and
never change its status). Only this signed webhook marks an order paid, never the
shopper's return to the cart. A payment that is smaller than the order total, or in
another currency, leaves the order pending for a person to check. Each event is
applied once per event id; a paid order never goes back to unpaid. POST /api/v1/checkout/cancel lets a shopper back out of an unpaid order: Stripe
closes the payment first, then the order is cancelled.
After paying, the shopper returns to /cart?order=…, which asks GET /api/v1/orders/[id]/status every 1.5 seconds for 20 seconds. That public
endpoint returns the status word only, never an email, address or total, and the page says
plainly when payment is still being confirmed.
Email is optional. Set RESEND_API_KEY and EMAIL_FROM (and EMAIL_REPLY_TO if you want one). A receipt is sent when an order becomes
paid and a shipping notice when it is marked shipped, each at most once per order: the email_log table refuses a second send. Without a key nothing is sent and
the log records what was skipped. Email colours come from emailTheme in src/lib/site.config.ts.
Google Listings
A shop is eligible for free listings and Shopping ads in Google Merchant Center
without hand-made spreadsheets. Apply migration 0035 with bun run db:migrate:local. Each product has an optional Google listing section in /admin/products (brand, GTIN, MPN, condition, category, sale price and window, extra images, color, size,
material, gender, age group, item group, weight and size, custom labels). The rules live
in one pure module, packages/commerce/src/google-listing.ts.
The feed
/feeds/google-products.xml (RSS 2.0 with the g: namespace)
and /feeds/google-products.tsv list active products. Auctions, products
marked "keep out of the Google feed", and products with an error are left out, so one
bad product cannot cost the account its standing. Prices are the catalog price in the
product's own currency, for example 12.99 USD; regional pricing is never
applied to what Google sees. Paste the XML address into Merchant Center as a scheduled
fetch. The feed is read from the Cloudflare cache for 15 minutes and pages through D1,
so a 2000 product catalog costs one pass of reads, not one per visit.
Feed health
/admin/products/google shows the feed address, when it was last fetched, how many products are valid, left out or carry warnings, and the first problems with a link to fix each. It also holds the ship-to countries, delivery times, return policy and default brand that the feed and each product page's structured data use. A section left blank is not claimed anywhere.
Product page markup
Every /shop/[slug] page emits one JSON-LD Product with an Offer (price, currency, availability, condition, seller, shipping details and
return policy when set), inside the page's single Organization and WebSite graph. The price
in the markup is the price on the page, the price in the cart and the price in the feed.
A sale price is charged at checkout while its window is open.
Merchant API sync (optional, off)
Turn on Merchant API sync in /admin/products/google (owner only) to send changes every ten minutes through the google-merchant-sync scheduled job (/api/cron/google-merchant-sync). The service-account key is stored encrypted in KV, never in D1. Setup, disapproval
reasons and what could not be verified: docs/GOOGLE_LISTINGS.md.
Auctions
Sell one unit of a product to the highest bidder. Create an auction from a product in /admin/auctions (the shop permission). Shoppers bid at /auctions and /auctions/[slug]. A won auction becomes an
ordinary order, so payment, fulfilment and the orders API work as they do for any
other sale.
Setup options
- Starting price, and an optional hidden reserve.
- Buy it now, offered until the first bid (or until the reserve is met).
- Bid increment, or the tiered default.
- Extend by N seconds, so nobody wins with a last-moment bid.
- Winner must pay within N hours. Use 0 for manual payment.
Proxy bidding
- A bidder enters the most they will pay.
- The board shows only the price needed to stay in front.
- An equal maximum loses: the earlier bid wins a tie.
- The winner pays the price on the board, never their maximum.
Closing and paying
- The
settle-auctionsjob runs every 5 minutes. It closes ended auctions and creates one pending order per winner. - The winner pays on the auction page. Without Stripe keys the order waits for manual payment.
- Unpaid winners get one reminder, then the sale lapses and can be relisted.
Bidders must be signed in and see an alias such as “Bidder 7F3A”, never a name or a
maximum. Emails (won, outbid, pay soon, win cancelled) send once per event when RESEND_API_KEY and EMAIL_FROM are set.
Drag and Drop
The widget board is a working feature of this site, not a demo you read about. See the components page for the widgets running on a live board.
Using it in your app
<WidgetBoard> takes a layout and a list of columns, and reports a
new layout through on:change. It stores nothing itself, so where a layout
is saved stays your decision.
<WidgetBoard bind:widgets {columns} on:change={(e) => save(e.detail.widgets)} /> Registering a widget takes three edits and none of them is the board: an entry in src/lib/widgets/manifest.ts, a line in src/lib/widgets/index.ts, and the component itself. Six standard widgets
ship — stat, clock, checklist, meter, notes and links — all running at /components. The two actions underneath the board — use:draggable and use:dropzone — work on any markup, so a sortable list or a nav reorder
needs no board at all.
Keyboard control
Every pointer gesture has a keyboard equivalent, announced through a live region. Focus a drag handle, then:
- Space or Enter — pick the widget up, and put it down again.
- Arrow up and down — move it within its column.
- Arrow left and right — move it to the column either side.
- Escape — cancel, returning it to where it started.
On a touchscreen, hold a handle briefly before dragging — a swipe stays a swipe, so the page still scrolls. Dragging near the top or bottom edge scrolls the page with you.
The rule that will bite you
A widget's stored state must be inert. A value that changes on a timer — a price, a
clock, a connection count — goes out through the widget's live event and
comes back in through the board's live prop; it must never be written
into widget.title, which is persisted. Ignoring this is how a dashboard
rewrites its whole layout every thirty seconds and burns a day's storage quota from
one open tab.
Full reference, including the reorder contract and the component checklist: docs/WIDGET_BOARD.md.
Pages and the Site API
Two packages live in packages/, each versioned on its own: @nebulakit/builder (drag-and-drop pages stored as data) and @nebulakit/security (API keys). Security updates are forced to every site;
the builder is opt-in.
Pages
Build pages at /admin/pages: drag blocks from the palette onto the
page or into a Section or Column, reorder and nest them, edit each block's fields,
hide a block on phones, tablets or desktops, preview at each width, and publish.
Mouse, touch and keyboard all work: every drag has Add, Move up/down, Move in and
Move out buttons, plus Alt+Arrow keys, and Undo and Redo (Ctrl/⌘+Z) cover every
edit. A check list flags images without a description, skipped heading levels and
vague link text. Published pages are served at /p/[slug] and listed
in the sitemap; the site's own routes always win over a page. Every save that
changes a page keeps a revision (the newest 50), listed under History in the
editor and at /api/v1/pages/[slug]/revisions; restoring one is itself
saved, so it can be undone. A page is a JSON document (format version 2: a tree of
blocks), never code, and it is rendered without raw HTML.
API keys
The owner manages access at /admin/api-keys: create a key with scopes
and an expiry, see when it was last used, revoke it in one click, and read the
call log. A key is shown once and stored hashed.
The site API
Everything is reachable under /api/v1, described at /api/v1/openapi.json. Send keys as Authorization: Bearer nk_…. Published pages are public to read;
writes need a key with the right scope or an admin session that sends JSON.
ORCHESTRATOR_KEY_SHA256 connects the Ammoura control room. Apply
migrations 0020–0022 with bun run db:migrate:local.
The Badge
The badge page hands out "Proudly built with NebulaKit" in six forms — Markdown for a README, HTML, a web component, and React, Svelte and Vue components — in three wordings and on either a dark or a light ground. Pick, preview, copy.
An app generated from the template already wears it: the footer carries BuiltWithNebulaKit.svelte, and showBuiltWithBadge: false in src/lib/site.config.ts removes it. NebulaKit is MIT-licensed, so the badge
is a courtesy rather than a condition.
Endpoints
/badge.svg?variant=proudly|built|powered&theme=dark|light is the
image a README embeds. /badge.js defines <nebulakit-badge>, which renders in a shadow root and follows the
reader's own colour scheme. /badge-mark.svg?size=64 is the mark alone, for
the cases the pill does not cover.
All three are served with Access-Control-Allow-Origin: *, because the
badge is embedded cross-origin by definition. None of them loads a font, a stylesheet
or a tracker: the mark is drawn inline and every colour is a literal, so the badge
renders on a page that has never heard of this site. An unknown variant or theme renders the default badge rather than a 400 — a broken image in someone
else's README tells them nothing.
Working With AI in This Repo
Treat AI as a fast pair programmer, not as a source of truth. It is useful here because the repo already includes app structure, tests, and strong conventions, which gives the assistant real context to work against. The quick version, with a prompt to paste, is at the top of this page: Use NebulaKit with an AI.
Good Prompts
- Point the assistant at a concrete file, route, failing test, or command.
- Ask it to write or update tests first when changing behavior.
- Ask for narrow fixes instead of broad rewrites unless you want architectural change.
Good Validation Habits
- Have the assistant explain which route, store, or service controls the behavior.
- Require executable validation after changes, not only a diff summary.
- Always finish by running check, tests, and coverage.
What AI Is Best At Here
- Tracing a route from UI to service layer and identifying the smallest edit surface.
- Adding tests around setup, auth, chat, or command palette behavior.
- Summarizing repo conventions such as Cloudflare bindings, migrations, and theme rules.
If you are using an AI coding agent, keep requests concrete: mention the page or failing test, state the desired behavior, and ask for the smallest validating change that solves it.
Core Commands
These scripts are defined in package.json and are the canonical local workflow.
Development
npm run devruns on host 0.0.0.0, port 4278.npm run previewpreviews the production build on port 4278.npm run checkruns Svelte sync plus svelte-check.
Testing
npm run testruns Vitest in CI mode.npm run test:watchruns Vitest in watch mode.npm run test:e2eruns Playwright tests.npm run test:allruns unit tests, then E2E tests.
Deploy and Validation
npm run deploybuilds then deploys .svelte-kit/cloudflare.npm run validate:contrastchecks theme contrast.npm run validate:allruns check + test + contrast validation.
Checks and Tooling
npm run check:bindingsandsetup:cfcheck and create the Cloudflare resources.npm run check:watchkeeps svelte-check running.npm run build:dafnycompiles the Dafny proof cores to JavaScript.npm run dev:tunnelstarts the dev server behind a public tunnel.npm run og:generaterebuilds the social-share image.npm run seo:externalruns the external SEO checks.npm run test:ui,test:e2e:uiandtest:e2e:headedopen the test runners with a window.npm run palette:scanaudits colour use against the theme tokens.npm run seed:demofills a local database with demo content.npm run capture:built-withandnpm run capture:admintake the marketing screenshots.
Testing and Quality Gates
NebulaKit follows Test-Driven Development. Write failing tests first, then implementation, then refactor.
# Run all tests
npm run test
# Run tests in watch mode
npm run test:watch
# Check coverage
npm run test:coverage
# Run E2E tests
npm run test:e2e
# Run all tests (unit + E2E)
npm run test:all Coverage guidance in repository docs is mixed: several files reference 90 percent, while AI assistant workflow instructions enforce 95 percent as a stricter floor. For new work, target 95 percent or higher to satisfy both interpretations safely.
Project Structure
NebulaKit/
├── .github/ # Copilot and workflow instructions
├── src/
│ ├── lib/
│ │ ├── components/ # Reusable UI components
│ │ ├── services/ # Business logic
│ │ ├── stores/ # Svelte stores
│ │ ├── types/ # Shared type definitions
│ │ └── utils/ # Helpers
│ ├── routes/ # SvelteKit routes
│ │ ├── api/ # API endpoints
│ │ ├── auth/ # Authentication pages
│ │ ├── chat/ # Chat UI
│ │ ├── setup/ # First-time setup flow
│ │ └── documentation/ # This page
│ ├── app.css # Global styles & theme
│ └── app.html # HTML template
├── tests/ # unit/integration/e2e tests
├── migrations/ # Immutable D1 migration files
├── docs/ # Extended project docs
└── wrangler.toml # Cloudflare bindings/config /media/<key> streams uploaded files from the R2 bucket, with byte
ranges so video plays on iOS. /privacy and /terms ship as template
text. Replace both with your own before you launch.
Run
Cloudflare Bindings
NebulaKit is configured for Cloudflare Pages with these bindings in wrangler.toml:
DBas D1 database binding (database name: nebulakit-site-db).KVas KV namespace for runtime config and flags.BUCKETas R2 bucket binding.- Queue producer binding is documented but commented out by default.
Set app secrets in Cloudflare dashboard or Wrangler secrets for production. Avoid committing raw secrets to source control.
Database Migrations
Migrations are ordered SQL files under migrations/ and tracked by D1. Never
edit or delete existing migration files once committed to main.
# Apply pending migrations to local D1
npm run db:migrate:local
# Apply pending migrations to remote D1
npm run db:migrate
# List migration status
npm run db:migrate:list When schema changes are needed, create a new file with the next sequence number (for
example, 0006_add_feature_flag.sql) and use ALTER TABLE or new CREATE statements. npm run check:migrations (also a CI step) fails on a bad file name, a duplicate
or skipped number, or an edit to a migration already on the base branch.
Deployment to Cloudflare Pages
- Push your repository to GitHub.
- In Cloudflare dashboard, open Pages and connect the repository.
- Use build command
npm run build. - Use output directory
.svelte-kit/cloudflare. - Add D1, KV, and R2 bindings to the Pages project settings.
- Add required environment variables and secrets.
- Deploy and verify auth, setup flow, and database connectivity.
The local deploy script already uses wrangler pages deploy .svelte-kit/cloudflare.
Scheduled Jobs
Cloudflare Pages cannot run Cron Triggers, so a standalone Worker in cron-worker/ wakes on a schedule and POSTs to the app's /api/cron/* routes with CRON_SECRET, retrying three times
with exponential backoff. The app claims each job once per scheduled minute, so a
retry or overlapping delivery never runs it twice, and records every run.
/admin/scheduled-jobs lists each job with its last run, status and next
expected run, and has a Run now button for the owner.
cd cron-worker
# set TARGET_URL in wrangler.toml to your site's origin
bunx wrangler deploy
bunx wrangler secret put CRON_SECRET # same value as on the Pages project It needs migration 0019_scheduled_job_runs.sql. Adding a job and the
details are in docs/SCHEDULED_JOBS.md.
SEO, Redirects and Lighthouse
Structured data
SharingMeta emits one JSON-LD @graph per page:
Organization, WebSite, WebPage, plus a BreadcrumbList, an ItemList or an Article
when the page passes them in its structured prop. Ids are built from site.url, and a noindex page emits none. Notes in docs/STRUCTURED_DATA.md.
Redirects
Admins manage old-to-new addresses at /admin/redirects (migration 0017_redirects.sql): pick 301, 302, 307 or 308 and see how many times
each one was used. A redirect is looked up only when the site would otherwise
answer "not found", so it never hides a live page and costs healthy pages nothing.
The new address is a path on this site or an https address on an allowed host: the
site's own, plus any you list in REDIRECT_ALLOWED_HOSTS (comma-separated).
Loops and off-site targets are refused when you save, and again when a visitor is redirected.
Canonical host
The session cookie belongs to one host, so a visitor signed in on the bare domain
would look signed out on www. The site sends www. + the
host in site.url to that host (301 for reads, 308 for anything else), except
webhook and cron paths. It never redirects on localhost, *.pages.dev,
previews or tunnels. Every host that is not the one in site.url is
served with X-Robots-Tag: noindex, so a preview never competes with
production in search.
Security contact
/.well-known/security.txt (RFC 9116) tells researchers where to send
a vulnerability report. Set SECURITY_CONTACT to an email address or
an https URL (comma-separated for several) and the file is served, with an Expires date recomputed on every request, 180 days ahead. Until then the
path answers 404.
Lighthouse, two ways
The scores on the home page come from bun run lighthouse, which runs
Lighthouse locally and commits the reports. /admin/lighthouse is the
live counterpart: it audits the public pages in the sitemap through Google's
PageSpeed Insights API (migration 0018_lighthouse_audits.sql), lists
them worst first with the audits that failed, and has a Run now button. PageSpeed can only reach a public URL, so site.url has to be
real. PAGESPEED_API_KEY is optional and only raises the rate limit. Notes
in docs/LIGHTHOUSE_MONITORING.md.
Agent Readiness
This site publishes a machine-readable discovery layer so search crawlers and AI agents can find it, read it efficiently, and understand how to interact with it. Everything below is live and needs no configuration.
- /robots.txt — crawl rules, explicit entries for AI crawlers (GPTBot, ClaudeBot, PerplexityBot and others), and Content Signals declaring how the content may be used.
- /sitemap.xml — every public page plus all published CMS content, regenerated on request so newly published items appear immediately.
- Landing pages under
/who,/where,/what,/whenand/why— hand-written SEO pages fromsrc/lib/landing/content/. This site has not published any yet, so those routes return 404 and the sitemap lists none. Start one withbun run landing new <family> <slug>and check it withbun run landing report; seedocs/SEO_LANDING_PAGES.md. - /.well-known/api-catalog — an RFC 9727 catalog of this deployment's APIs.
- /.well-known/agent-skills/index.json — short guides teaching an agent how to read content and contact the site, each with a SHA-256 digest.
- /auth.md — how agents authenticate (and what is not offered).
- /api/health — service health, used as the catalog's status link.
Reading pages as Markdown
Any page can be fetched as Markdown instead of HTML by sending an Accept: text/markdown header. Browsers are unaffected — HTML remains the
default. Responses include an x-markdown-tokens estimate so an agent can budget
context before reading.
curl -H 'Accept: text/markdown' https://nebulakit.starspace.group/ In-browser tools (WebMCP)
When opened by a WebMCP-capable agent, this site registers tools for searching content, listing pages, reading a page as Markdown, navigating, and switching theme. They are read-and-navigate only, restricted to this site's own origin, and run with the visitor's existing permissions.
Content usage policy
The shipped default is fully permissive — search=yes, ai-input=yes, ai-train=yes — which suits an open template. If you build a site with proprietary content, change CONTENT_SIGNAL in src/lib/agent-discovery.ts before launching;
every robots.txt group picks the change up automatically.
DNS-based discovery (DNS-AID) is the one piece that must be added by hand, since DNS
records live with your provider rather than in this repo. See docs/AGENT_READINESS.md for the exact records and the DNSSEC requirement.
Troubleshooting
- If setup API reports KV unavailable, create KV namespaces and update wrangler.toml binding IDs.
- If login fails after setup, confirm OAuth callback URL and ensure GitHub credentials are valid.
- If migrations fail, run
npm run db:migrate:listand check migration numbering. - If command palette entries are missing, verify AI provider status and authentication state.
Secure and administer
Authentication and Setup Flow
Sign-in is GitHub or Discord OAuth, or email and password, with a setup-first
workflow. The cookie holds an opaque session token; only its SHA-256 digest is stored
in D1, so a leaked table gives no usable cookie. Set SESSION_SECRET (it
signs the OAuth state cookie) and SETUP_SECRET (the bootstrap password
for /setup) as Worker secrets. The main routes are /setup, /auth/login, and /reset.
1. Configure
Open /setup and submit GitHub OAuth credentials plus admin GitHub username.
2. Lock Setup
After the admin logs in the first time, setup is locked to prevent accidental reconfiguration.
3. Reset When Needed
/reset clears setup-related KV keys and session cookie. Admins can disable
reset route access.
Secrets at Rest
AI provider keys and OAuth client secrets saved from the admin screens are encrypted
(AES-256-GCM, bound to their record) before they are written to KV, and the admin UI
only shows that a key is configured plus its last four characters. Set the ENCRYPTION_KEY secret (bun run generate:encryption-key) so
the key lives outside KV; without it the app generates one and keeps it in KV, which
encrypts the values but does not defend against someone who can read the whole
namespace. To rotate, move the old value to ENCRYPTION_KEY_PREVIOUS;
values are re-encrypted when read. Plaintext keys saved by earlier versions are
migrated on first read.
Security Hardening
Every response carries HSTS, X-Content-Type-Options: nosniff, a frame
policy and a referrer policy, and the Content-Security-Policy forbids object embeds, a foreign base and framing by other sites. Static files get the
same headers from _headers. Password sign-in is throttled per address and per email. After
signing in, ?next= returns you to a same-site path and never to another
host. The Markdown variant of a page is never cached, and /media answers
byte ranges so video plays in Safari. See docs/SECURITY.md.
Roles and Permissions
Access is a ladder, owner > superadmin > admin > user, plus a list of named areas each admin may open. One rule does the work: you can only act on someone strictly below you, and you can only hand out a role strictly below your own. An admin cannot promote, demote or delete a peer, a superadmin or the owner, and nobody can promote themselves.
The owner is configuration
The owner is set by GITHUB_OWNER_ID (or setup), never by the users.role column, so no request can assign or remove it. Superadmins
hold every area, including pii, and cannot be limited.
Areas for admins
cms, pages, users (view the list), users-manage (change roles and delete, below their own role), stats, pii, api-keys, contact, lighthouse and shop. Settings, auth keys, AI keys, the
analytics connection, setup and reset stay with the owner and superadmins. Only
the owner or a superadmin can change an admin's areas.
Managing access
On /admin/users choose Manage access on a row to set the
role and tick the areas. The sidebar only shows links the signed-in admin can use, but
every load and API also checks the permission itself. Role and areas are re-read from
the database on every request, so a change applies on the next click.
Upgrading
Migration 0015 adds users.role and user_permissions. Existing admins keep every area they could reach
before (and can_view_stats becomes the stats permission); nobody is promoted and owner-only areas are not handed out. is_admin and can_view_stats stay in sync. To add an area, add it to src/lib/access/permissions.ts, guard its routes with requirePermission, and give its nav entry a permission.
Audit Log
/admin/audit (owners and superadmins) lists who changed what: user role changes, deletes and merges, content publish and delete, settings and key changes, API key create and revoke. It records changes only, never page views, and never a secret value: a key or setting change notes that it changed, and any detail field named like a token, secret, password or key is redacted. Filter by actor and action, search, and open a row for its detail.
Rows are kept 365 days; a scheduled job prunes older ones (pruneAudit in src/lib/server/audit.ts, run daily as the prune-audit job).
Apply migration 0016 (shared with sign-in tracking) with bun run db:migrate:local. Details: docs/AUDIT_LOG.md.
Admin Time Zone
Every date and time in the admin area, and the days and hours on the Dashboard charts,
are shown in one zone. By default (auto) that is the browser's own zone,
read with Intl.DateTimeFormat().resolvedOptions().timeZone without asking, and
handed to the server in a tz cookie so server rendering matches. Each
user can set their own zone on their profile, which
wins over everything else. The owner can pin a fixed IANA zone on the admin dashboard; that beats the browser. With neither, the zone
is UTC. The zone is named in the sidebar and on the Dashboard header.
Counters are stored per UTC hour and per UTC day, so the Stats page re-buckets what it
can and says so: the 24-hour chart keeps UTC hours labelled in your zone, longer
windows are rebuilt from the hourly totals into days of your zone, and the breakdown
tables and the platform usage meter stay on UTC days. Apply migration 0030 and 0031 with bun run db:migrate:local. Public pages are not affected. Details: docs/ADMIN_TIMEZONE.md.
Sign-in Tracking
The owner can see who is signed in and where each sign-in came from at /admin/sign-ins, and sign any session out. Recording is off by default, because it is the one place the site stores an IP address; the rest of the site stays cookie-free and IP-free. The owner switches it on on that page, under a plain-language disclosure.
While on, each sign-in attempt, failures included, stores the method, whether it
worked, the IP address (shown masked unless the owner reveals personal data), a rough
device label such as “Chrome on macOS” (never the raw browser string), the country,
and when a live session was last used. Passwords and typed email addresses are never
stored, visitors who are not signing in are never recorded, and attempts are deleted
after 90 days. Recording can never block a sign-in. If you turn it on, say so in your
privacy policy. Details: docs/SIGN_IN_TRACKING.md.
Admin Analytics
/admin/stats is the built-in analytics surface: traffic over a 1, 7, 30, or
90-day window, views by route, the most-read CMS items, referrers, countries, an audience
breakdown, and user and content growth over time. It is first-party — there is no third-party
script, no account, and no API key to provision.
Everything collected is a daily aggregate counter in D1. There are no cookies, no identifiers, and no IP addresses anywhere in the feature. The User-Agent is read to classify the request and then discarded, so only coarse buckets (operating system, browser, device, language, viewport) are ever stored, and country comes from the Cloudflare edge rather than from an IP lookup. Because nothing per-visitor is retained, this needs no consent banner.
Turning It On
- Apply migrations
0007through0009, plus0012for the Top content table, withdb:migrate:local(ordb:migratefor remote). - Collection starts on the next request. Traffic and audience panels fill in as visits arrive.
- Country stays
(unknown)in local development — it is supplied by the Cloudflare edge. - Top content appears once a published CMS item is read. It is the only counter whose row count grows with your catalogue rather than with a fixed list, so on a large catalogue watch it — the retention cron is what keeps it in hand.
Granting Access
The owner always sees Stats. Any other admin needs the can_view_stats flag,
which defaults to off so existing admins do not gain access on upgrade:
UPDATE users SET can_view_stats = 1 WHERE email = 'someone@example.com'; The permission is re-read from the database on every request, so revoking it takes effect without waiting for a sign-out.
Retention
The counter tables grow one row per day per dimension unless pruned. Set CRON_SECRET and have any scheduler POST to the retention endpoint; rows
older than 400 days are removed.
curl -X POST https://your-app/api/cron/prune-view-stats \
-H "Authorization: Bearer $CRON_SECRET" Platform Usage Meter
The same page tracks billable Function invocations — a larger set than
page views, since bots, /api/* calls, 404s, and non-GET requests all count
against your plan. It projects whether today will exhaust the free 100,000-request daily
allowance before the UTC reset.
Treat it as an early warning and a floor, not a bill: the Cloudflare dashboard remains authoritative.
Reading Stats From a Dashboard
An external dashboard can read the same numbers as JSON. The owner issues a stats token under "Dashboard access" on /admin/analytics; it is shown once, and only its hash is stored. The
dashboard then sends it as a bearer token:
curl https://your-app/api/stats/summary?window=7 \
-H "Authorization: Bearer nks_…" The response is read-only and aggregate-only — exactly what /admin/stats shows. Rotate replaces the token; Revoke cuts the dashboard off on its next read.
A site that has never issued one answers 401 to every request.
Connecting Google Analytics
Optional, and off until you turn it on. The owner opens /admin/analytics, pastes either the GA4 Measurement ID (G-ABCD123456) or the whole gtag.js snippet — the ID is read out of it — and saves.
A toggle pauses the tag without discarding the ID, and Disconnect removes it entirely.
The tag loads on public pages only. /admin, /api, and /setup are excluded, so your own admin traffic stays out of the numbers,
and page views are sent on every client-side navigation rather than by gtag's automatic
tracking, which would count only the first page of a visit.
This is additive: the built-in stats keep working, and reports are read in Google's own console. Google Analytics sets cookies and collects per-visitor data, which is a different privacy posture from the built-in counters — review your privacy policy and add a consent banner where your jurisdiction requires one. Only the owner can change the connection.
More
References
Contributing
Use TDD, keep changes small and reviewable, and run tests plus checks before opening a pull request.
- Write tests before implementation changes.
- Run npm run check, npm run test, and npm run test:coverage.
- Prefer Cloudflare-native services and minimal external dependencies.
- Do not edit past migration files; create a new one instead.