minshop
AGENTS.md163 starsChanged 4 months agoMITView on GitHub
A guide for contributors and automated tooling making changes to minshop: a small, full-Cloudflare ecommerce store (Astro 7 SSR on Workers + D1 + R2 + Stripe / Lightning). Read this before editing. It's the map, the rules, the recipes, and the traps. npm run verify is the single signal that a change is sound: it runs unit tests, full Astro diagnostics, the production build, the clean-room D1 integration, and the MCP typecheck/deployment dry run. If it's green, the change…
- Reads credentials
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
The file
We split this AGENTS.md into the rules it gives an agent
9 rules. About 1 is common, 3 are distinctive, and 5 are very specific to this project.
≈1
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
Run it after every meaningful edit, not just at the end.
- Very specific: line 31
Prefer it over npx wrangler d1 execute --local for reads and fixture setup: that spawns a process each time (~1.2s) where this is an HTTP …
- Distinctive: line 50
Use the narrowest customization surface that fits the change:
- Common: line 72
Never put real credentials, customer data, payment data, personal paths, or local environment files in the repository.
- Very specific: line 75
Before handing off a customized shop, run npm run verify and review git diff --check plus git status --short.
- Very specific: line 148
NOT EXISTS / ALTER TABLE ADD COLUMN.
- Very specific: line 182
A page layout preset: add ONE entry to PAGE_LAYOUTS in features/pages/layouts.ts.
- Distinctive: line 256
Don't install npm packages without asking.
- Very specific: line 258
After non-trivial UI changes, screenshot to verify (see README → Frontend Testing).
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md — working on minshop
A guide for contributors and automated tooling making changes to minshop: a small, full-Cloudflare ecommerce store (Astro 7 SSR on Workers + D1 + R2 + Stripe / Lightning). Read this before editing. It's the map, the rules, the recipes, and the traps.
The loop (run this constantly)
nvm use 22 # REQUIRED — the supported toolchain runs on Node 22
npm run verify # complete storefront + D1 + MCP green/red gatenpm run verify is the single signal that a change is sound: it runs unit tests,
full Astro diagnostics, the production build, the clean-room D1 integration, and
the MCP typecheck/deployment dry run. If it's green, the change holds together.
Run it after every meaningful edit, not just at the end.
Other commands: npm run dev (astro dev), npm run preview (wrangler dev =
production mode, for testing middleware/auth), npm run db:migrate (local D1),
npm test.
Inspecting local data: use Local Explorer, not the CLI
Any local dev server (npm run dev, npm run preview) serves Cloudflare's
Local Explorer — a UI at /cdn-cgi/explorer and a self-describing REST API at
/cdn-cgi/explorer/api covering D1, KV, R2, Durable Objects and Workflows.
It reads the same local state the app does, with no setup.
Prefer it over npx wrangler d1 execute --local for reads and fixture setup:
that spawns a process each time (~1.2s) where this is an HTTP call (~0.02s) —
roughly 70x, which is the difference between checking state freely and avoiding
it. GET /cdn-cgi/explorer/api returns the OpenAPI spec, so the endpoints are
discoverable rather than memorized. The D1 database id is the binding name:
curl -s localhost:4321/cdn-cgi/explorer/api/d1/database/DB/raw \
-H 'content-type: application/json' \
-d '{"sql":"SELECT status, COUNT(*) FROM orders GROUP BY status"}'Local only, and unauthenticated on the dev origin — it is never exposed on a
deployed Worker, but do not bind a dev server to a public interface. Anything
touching --remote still goes through wrangler. The API reports itself as
version 0.0.1, so use it for working loops, not as the basis of a test gate.
Customizing a cloned shop
Use the narrowest customization surface that fits the change:
- Admin → Settings for runtime settings: store identity, time zone, storefront toggles, payment rails, email, bot protection, and search. These values live in D1 and apply without a deploy.
src/store.config.tsfor versioned, build-time settings: currency, shipping zones and rates, image dimensions, order numbering, and template-only features. Override only changed keys; never customizesrc/config.tsdefaults directly.src/themes/<theme>/tokens.cssfor brand colors, fonts, surfaces, and corner radii — each theme owns its@themeblock.src/styles/overrides.cssis a normally-empty override layer applied after the theme's tokens. Keep structural styles insrc/styles/base.cssand avoid restyling individual components when a shared token will do.public/favicon.svgfor the browser icon. Products, categories, images, and other catalog content belong in the admin, not source files.wrangler.jsoncfor optional infrastructure bindings such as image optimization, semantic search, and email delivery. Keep optional bindings disabled unless the corresponding feature is configured.
Provider credentials belong in Admin → Settings, where the secret vault stores them encrypted. Deployment-only secrets belong in the platform secret store. Never put real credentials, customer data, payment data, personal paths, or local environment files in the repository.
Before handing off a customized shop, run npm run verify and review
git diff --check plus git status --short. Add a new numbered migration for
schema changes; never rewrite a migration that may already have run.
Architecture in one screen
Feature folders (vertical slices). Each owns its data access, types, and components. Deleting a folder still builds.
src/
config.ts SCHEMA + DEFAULTS (upstream-owned). getConfig() = source of truth.
store.config.ts build-time shop overrides, deep-merged on top
styles/overrides.css post-theme override layer (normally empty; theme tokens live in themes/<theme>/tokens.css)
middleware.ts admin auth gate (fail-closed)
env.d.ts Cloudflare.Env binding/secret types
layouts/ Layout.astro (storefront), AdminLayout.astro
features/
products/ db · form · image · ProductForm.astro · sort · search · stock · slug
orders/ db · number · reservations (atomic checkout stock holds)
payments/ provider (port) · stripe · opennode · lightning-provider · index (factory)
lightning/ backend (port) · phoenixd · lnbits · index · rate · pending
shipping/ calculator (zones + ShippingCalculator port)
storage/ provider (port) · r2 · index (factory)
email/ provider (port) · resend · cloudflare · index (factory) · orderConfirmation
auth/ access (CF Access JWT) · session (admin login cookie) · turnstile · Turnstile.astro
catalog/ serialize · http (public API shapes for /api/products)
cart/ categories/ customers/ media/ pages/
pages/
index, products/[slug], categories/[slug], pages/[slug], search, cart, checkout
product/[slug], category/[slug] — 301s to the plural URLs; keep indefinitely
pay/[publicId] (Lightning invoice page), order/[token] (confirmation)
admin/ (CRUD UI, login, logout)
api/ (cart, webhook, admin/*; checkout — form OR JSON {items} → checkout_url;
products, products/[slug] — public machine-readable catalog)
images/[...key] (serves R2), sitemap.xml, robots.txtPorts & adapters (the seams). Routes depend on interfaces, never on a vendor. To swap/add a provider, write one adapter file + wire the factory:
| Port | Where | Factory | Adapters |
PaymentProvider | payments/provider.ts | payments/index.ts | stripe, opennode, lightning |
LightningBackend | payments/lightning/backend.ts | payments/lightning/index.ts | phoenixd, lnbits |
ShippingCalculator | shipping/calculator.ts | (config-rates) | carrier rates (future) |
StorageProvider | storage/provider.ts | storage/index.ts | r2 |
EmailProvider | email/provider.ts | email/index.ts | resend, cloudflare |
SearchProvider | search/provider.ts | search/index.ts | fts, vector (Workers AI + Vectorize) |
Bindings. Access Cloudflare bindings via import { env } from 'cloudflare:workers'
(typed by Cloudflare.Env in env.d.ts). Never Astro.locals.runtime.env
(removed in Astro v6). D1 = env.DB, R2 = env.BUCKET.
Invariants — do not break these
- Storefront is near-zero client JS. Server-render everything; progressive enhancement only. A little JS is allowed where it earns it (cart drawer, /pay polling, Turnstile) — never required for the page to work.
- Orders are paid-only. The
orderstable holds settled orders. In-flight Lightning invoices live inpending_paymentsuntil settled — never write an unpaid order. - Config has two layers. Operational settings live in the D1 settings table
and are managed through the admin. Data-coupled defaults live in
config.tsand are overridden by clones instore.config.ts. Read effective values through the existing settings/config helpers. Currency remains build-time and store-wide. - Provider-agnostic core.
checkout.ts/webhook.ts/ shipping never import a vendor SDK directly — only the ports. Vendor code stays in adapters. - Admin is fail-closed. In production, if nothing is configured the admin is
blocked. Don't add an open admin path. Don't make
/admina "secret" path (security-through-obscurity) — the auth gate is the protection. - Migrations are additive. Numbered files in
migrations/,CREATE TABLE IF NOT EXISTS/ALTER TABLE ADD COLUMN. Never edit an applied migration; neverDROPdestructively. - Money is integer minor units (cents) end to end; format only at the edge
via
formatPrice(). UsetoMajorUnits()/minorUnitsPerMajor()for currency math (handles JPY/BHD, not just 2-decimal). - Tests stay pure. A
*.test.tsmust NOT importcloudflare:workers(vitest can't load it). Keep DB/env logic out of unit-tested modules — passdband secrets as params (seelightning/rate.ts,auth/session.ts). - Public routes are plural for browsable collections —
/products/<slug>,/categories/<slug>— matching the admin (/admin/products), the JSON API (/api/products/<slug>), and the feature directories. Token-addressed single resources stay singular:/order/<token>,/pay/<publicId>. Retiring a public URL means leaving a 301 stub behind permanently; these paths appear in sitemaps,llms.txt, and search indexes on stores you don't control. - Media is the only owner of stored files. Products, pages, and the logo
record USAGE by
image_key; removing an image from any of them deletes the association only. Only/api/admin/media/:iddeletes an R2 object, and it refuses while anything still references the row. Deletion and every association write are guarded in a single SQL statement so the two cannot race; never re-introduce a check-then-write. - Page bodies are Markdown with
html: false. Raw HTML is escaped, not parsed, so a page body can never inject script — including from a compromised admin session. The storefront and the admin preview call the SAME server-siderenderMarkdown(); do not add a client-side parser.
Recipes — how to add X
- A product field: new migration (
ALTER TABLE products ADD COLUMN …) → updateProduct/AdminProduct+ queries infeatures/products/db.ts→ add toProductForm.astro+parseProductForminfeatures/products/form.ts. - A content page: nothing to add — merchants create them at
/admin/pages. Published pages appear at/pages/<slug>, in the footer, sitemap, andllms.txtautomatically. - A page layout preset: add ONE entry to
PAGE_LAYOUTSinfeatures/pages/layouts.ts. The editor dropdown, validation, the storefront, and the admin preview all derive from it, andmeasure/titleAlignare emitted as CSS custom properties, so no stylesheet edit is needed. A preset that needs more than those two axes can also target[data-page-layout="<key>"]inglobal.css. Unknown/removed presets fall back tostandardat render time, so old rows never break. - A config setting: build-time → add to the
SiteConfiginterface ANDdefaultConfig()inconfig.ts; read viagetConfig(); document the override instore.config.example.ts(per-env overrides can read an env var, seeTIME_ZONE). Runtime (dashboard) → add aSettingKey+StoreSettingsfield infeatures/settings/db.tsand a form in/admin/settings. - A payment provider: implement
PaymentProviderin a newfeatures/payments/<name>.ts; add a case togetPaymentProvider()inpayments/index.ts; add its key as aSecretNameinfeatures/secrets/store.ts(stored encrypted in D1 via the admin vault — provider keys are NOT env vars) and aSecretFieldin the settings Payments card. - A Lightning backend: implement
LightningBackendinfeatures/payments/lightning/<name>.ts; add a case togetLightningBackend(). - A shipping zone/rate: edit
shipping.zonesinconfig.tsdefault (orstore.config.ts). Pure logic lives inshipping/calculator.ts— unit-test it. - A migration:
npx wrangler d1 migrations create minshop-db <name>→ edit →npm run db:migrate(local) →npm run db:migrate:remote(prod, before deploy). - An admin page:
src/pages/admin/<x>.astrousingAdminLayout(add a nav entry there). Mutations go through/api/admin/*(covered by the auth gate). - A storefront feature behind a flag:
config.features.<x>toggle; gate the nav link inLayout.astroand the route. - Customer auth:
features/auth/customer.tsis the magic-link adapter (no passwords) — reusestoken.ts(signed HMAC) + theEmailProvider. Pages live underpages/account/. Swap to OAuth by replacing that module. Orders are keyed by email, so "my orders" =listOrdersByEmail(no per-user join needed). - A search backend: implement
SearchProviderinfeatures/search/<name>.ts; add a branch togetSearchProvider(). Semantic (vector) keeps the index in sync viaindexProduct/unindexProductcalled from the admin product routes.
Gotchas — preflight checklist
These cost afternoons. Most are in the README "Gotchas" section too.
- Node ≥ 22.12 (
nvm use 22). Use Node 22 (the tested/supported release line). - Bare
</<=in an Astro{expression}parses as a tag open → a misleading "Unable to assign attributes when using <> Fragment shorthand" error. Fix: flip operands (0 >= x), use!==, or compute the boolean in frontmatter. (>is fine; only<bites.) - No
main/assetsinwrangler.jsonc— the adapter supplies the worker entry; settingmainbreaks a clean build. - CSRF on POST: Astro rejects cross-origin form POSTs (403). Browsers send
Originautomatically;curlneeds-H "Origin: http://localhost:4321"+-H "Content-Type: application/x-www-form-urlencoded". - Admin auth only enforces in production mode.
astro devbypasses the gate (so you can't lock yourself out). Test login/middleware withnpm run preview(wrangler dev). On a fresh store the password is set via the/admin/setupwizard (hashed in D1) — there is noADMIN_PASSWORDenv var; until one is set,/admin/setupis open (bootstrap), then the gate locks. - Lightning settlement = re-poll the node. The webhook is an untrusted nudge;
authority is
backend.getIncoming(). Don't trust a webhook to mark paid. - Webhooks need
constructEventAsync(Stripe) — the sync verifier uses Node crypto, absent on Workers. Web Crypto everywhere (seeauth/access.ts). - FTS5
MATCHthrows on raw input — sanitize to alphanumeric prefix tokens (features/products/search.ts). wrangler d1 exportfails with FTS5 virtual tables — dropproducts_fts, export, recreate (re-run migration0003).- The
mcp/Worker has its OWNpackage.json+node_modules. Never addagents/@modelcontextprotocol/sdkto the ROOT package.json — the Agents SDK pulls in workerd/miniflare deps that silently break the storefront's Astro build ("require_dist is not a function" in the CF Vite plugin). Install MCP deps inmcp/only. Build it independently withnpm run mcp:check(npm run verifyalso includes it).
Conventions
- Comments explain why, match surrounding density. No attribution in commits.
- Colocate tests:
foo.ts+foo.test.ts. Test pure logic; integration (D1/R2) is verified againstwrangler dev. - Don't install npm packages without asking. Don't run deploy/publish commands unless explicitly told.
- After non-trivial UI changes, screenshot to verify (see README → Frontend
Testing). Screenshots to
/tmp, deleted after.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

