project/medusa-foundation-audit
Resources app — References architecture
Shipping something new? Add it to the docs homepage's What's New list in
www/apps/book/components/Homepage/WhatsNewSection/data.ts. Seewww/apps/book/CLAUDE.mdfor the entry format and rules.
This documents how the API references (/references/...) are generated and
served. Non-reference pages in this app are ordinary MDX under app/; this file
is only about references.
TL;DR
References are generated from TypeScript source into a JSON doc-model (one
page.json per page under references/), stored in R2, fetched at request
time, and rendered with React components. There is no MDX in the references
pipeline anymore — neither as a generated artifact nor at serve time. Internal
links are resolved to their final slugs at generation time, so there is no
runtime link-fixing.
Data model: DocPage / DocBlock
Each reference page serializes to a DocPage: { slug, title, frontmatter, toc,
blocks }, where blocks is an ordered list of typed DocBlocks (markdown,
heading, typeList, codeTabs, workflowDiagram, note, sourceCodeLink,
linkList, table, badges, workflowEvents). Type/link data is structured
JSON; links are already final site URLs.
The contract is defined twice (separate yarn workspace roots — keep in sync):
- Generator side:
www/utils/packages/types/lib/index.d.ts(DocPage,DocBlock, ...) - Site side:
www/packages/types/src/references.ts(reusesWorkflow+FrontMatter)
Generation (in www/utils)
Run from www/utils/packages/typedoc-generate-references:
yarn start generate all --merge (or yarn generate:references from www/utils).
Two phases, both TypeDoc:
- Generate — per reference, convert TS source to a TypeDoc reflection JSON
in
www/utils/generated/typedoc-json-output/<name>.json. Enriched by the converter pluginstypedoc-plugin-custom(DML, internal-resolve, namespaces, events) andtypedoc-plugin-workflows(workflow/step/hook parsing). These are unchanged by the JSON migration. - Merge — read all reflection JSON back in (
entryPointStrategy: "merge") and render the doc-model intowww/apps/resources/references/(page.json).
The renderer is the json theme in
www/utils/packages/typedoc-plugin-medusa-theme:
src/json-theme.ts—JsonTheme extends MarkdownTheme. It reusesMarkdownTheme's URL/slug/mapping logic, the registered Handlebars helpers, andreflection-formatter(theTypeListdata). It only overrides output (.json),render(returnsJSON.stringify(DocPage)), andgetRelativeUrl(returns each target's final slug — this is what pre-resolves links).src/utils/build-doc-page.ts— builds aDocPagefrom a reflection (title, frontmatter, prose, TypeList for params/returns/properties, linkList for namespaces/modules, DML entities, workflow dispatch).src/utils/build-workflow-blocks.ts— workflow/step pages (diagram, input, output, hooks, emitted events).src/utils/parse-code-tabs.ts— splits helper-produced MDX fragments into structured blocks (<CodeTabs>,<TypeList>,:::note, headings, ...).src/utils/resolve-page-slug.ts— builds the page-URL -> final-slug map.
Important: MarkdownTheme and the Handlebars .hbs templates still exist in
that package — they are the generation-engine internals JsonTheme extends, not
MDX output. Do not delete them.
Merge options: src/constants/merger-json-options.ts (theme json, out =
references/, entry _index.json); it extends the shared merger-options.ts
(the regex-keyed formatting map, allowedProjectDocuments, etc.).
Build-time prep (in this app, yarn prep)
scripts/generate-files-map.mjs— scansreferences/forpage.jsonand keys each entry by the DocPage's baked-inslug(so slug-overridden pages resolve with a direct lookup); also scansapp/pages. Output:generated/files-map.mjs.scripts/generate-slug-changes.mjs— app pages only (references no longer need slug-changes; their slug is baked in).
Serving
app/references/[...slug]/page.tsx— renderscomponents/ReferenceJSON(client) and reads page metadata from the DocPage.components/ReferenceJSON— SWR-fetches/api/references/<slug>and renders it withReferenceContentfromdocs-ui.app/api/references/[...slug]/route.ts— resolves slug ->page.jsonviafiles-map, loads it (R2 bindingREFERENCES_R2_BUCKET-> public URL -> local fs), and returns the DocPage JSON as-is. Noserialize(), no link-fixer plugins. Cached (unstable_cache+Cache-Control+ OpenNext R2 incremental cache).docs-uiReferenceContent(www/packages/docs-ui/src/components/ReferenceContent) maps eachDocBlockto a component (TypeList,CodeTabs/CodeBlock,WorkflowDiagram,Note,SourceCodeLink,MarkdownContent, headings, linkList,WorkflowEvents).
Markdown output (md-content, for LLMs / copy-as-markdown)
app/md-content/[[...slug]]/route.ts serves plain Markdown. For page.json
files it uses docPageToMarkdown (www/packages/docs-utils/src/doc-page-to-markdown.ts),
the doc-model analogue of getCleanMd; app pages still use getCleanMd.
R2 storage / CI
- Keys:
resources/references/<relPath>/page.jsonin bucketdocs-references, read via theREFERENCES_R2_BUCKETbinding (seewrangler.jsonc). scripts/upload-references-to-r2.mjs— full or selective (--upload/--remove) upload ofreferences/page.json..github/workflows/sync-resources-references-to-r2.yml— syncs changedreferences/**page.jsonto R2 on push todevelop..github/workflows/generate-public-references.yml— regenerates references and opens the automated PR (commitsreferences/**).
Gotchas
- Links in the doc-model are final site slugs (
/references/...);md-contentabsolutizes them withNEXT_PUBLIC_BASE_URL. react-queryreference rendering exists in the theme but is not exercised — no reference in the current set producesUseMutationResult/UseQueryOptionsWrappertypes.- Generating the module
*-modelsreferences needseslint-plugin-markdowninstalled in the generation environment (used by the DML/eslint converter plugin); the merge step itself does not.