title: "@thorprovider/runtime" purpose: "Multitenant storefront runtime: tenant resolution, theming, spec-driven rendering and commerce data for Next.js hosts" lastReviewed: "2026-10-02"
@thorprovider/runtime
Multitenant storefront runtime for Thor Commerce. It converts a serializable
json-render spec into real Next.js pages, resolves the tenant by hostname,
applies per-tenant theming as CSS variables, and feeds data through loaders
from the @thorprovider/types canon.
It is the package every Thor Commerce storefront mounts: projects/electrostore
uses it directly, and every storefront generated by @thorprovider/storefront
imports from it.
This README is organised in two parts:
- Getting started — for consumers who want to mount the runtime in a Next.js
host and render a storefront. Start here.
- Development — for maintainers working on the package itself. See
Development.
Getting started
Prerequisites
| Requirement |
Supported |
| Next.js |
^16.0.0 (peer dependency) |
| React / React DOM |
^19.0.0 (peer dependencies) |
| Data provider |
@thorprovider/adapters (bundled as a dependency; mock works offline) |
Install
yarn add @thorprovider/runtime
The package ships layered subpath exports (see
Subpath entry points). Import from the layer you need —
never from the root in app code.
The four layers
+------------------------------------------------------------+
| your Next.js host (app/) |
+------------------------------------------------------------+
| |
v v
+-------------+ theme vars +--------------+
| host-glue | ------------> | render-core |
| (Next/React)| | (Next/React) |
+-------------+ +--------------+
| |
v v
+-------------+ +--------------+
| contracts | | commerce |
| (pure Zod) | | (server TS) |
+-------------+ +--------------+
^ ^
| @thorprovider/types canon |
+------------------------------+
| Layer |
Subpaths |
Bound to |
Role |
host-glue |
/host-glue, /host-glue/providers, /host-glue/tenant-resolver |
Next.js + React |
Mount the runtime in a host: resolve the tenant by hostname, provide the renderer context, SEO helpers |
render-core |
/render-core, /render-core/app, /render-core/theme, /render-core/registry |
Next.js + React |
Turn a spec into routes: catalog of blocks, component registry, theming, createStorefrontApp |
contracts |
/contracts |
none (pure Zod/TS) |
Shared contracts: theme tokens, spec shapes, tenant config/registry, resolution, env |
commerce |
/commerce |
server-side TS |
Data bridge over @thorprovider/adapters: catalog reads, cart/checkout service, canon-to-blocks transforms |
Quick start — multitenant host
Three pieces, following the canonical projects/electrostore mount.
1. Root layout — resolve the tenant, derive theme CSS, wrap in the provider
(app/layout.tsx):
import { resolveTenantFromRequest } from '@thorprovider/runtime/host-glue/tenant-resolver'
import { StorefrontProvider } from '@thorprovider/runtime/host-glue/providers'
import { themeTokensToVars, themeVarsToCss } from '@thorprovider/runtime/render-core/theme'
import { tenantRegistry } from '@/lib/tenant'
import { registry } from '@/lib/registry'
export async function generateMetadata() {
const resolution = await resolveTenantFromRequest(tenantRegistry)
return resolution.status === 'found'
? { title: resolution.tenant.seo.title, description: resolution.tenant.seo.description }
: {}
}
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const resolution = await resolveTenantFromRequest(tenantRegistry)
if (resolution.status === 'not-found') {
return <h1 className="p-8 text-2xl font-semibold">Store not found</h1>
}
const themeCss = themeVarsToCss(themeTokensToVars(resolution.tenant.theme))
return (
<body>
<StorefrontProvider themeCss={themeCss} registry={registry}>
{children}
</StorefrontProvider>
</body>
)
}
2. Runtime module — build the app from a spec and loaders
(lib/<name>/runtime.ts):
import { createStorefrontApp } from '@thorprovider/runtime/render-core/app'
import { getCatalog, getDataSource, getProductByHandle } from '@thorprovider/runtime/commerce'
import { spec } from './spec'
const loaders = {
catalog: async () => ({ catalogProducts: (await getCatalog()).products }),
product: async (params) => {
if (getDataSource() === 'mock') return { selectedProduct: mockProduct(params.id) }
return { selectedProduct: await getProductByHandle(params.id) }
},
}
export const app = createStorefrontApp({ spec, loaders })
3. Catch-all route — the app drives the page (app/[[...slug]]/page.tsx):
import { notFound } from 'next/navigation'
import { app } from '@/lib/runtime'
import { PageRenderer } from '@json-render/next'
export const { generateMetadata, generateStaticParams } = app
export default async function Page({ params }: { params: Promise<{ slug?: string[] }> }) {
const data = await app.getPageData({ params })
if (!data) return notFound()
return <PageRenderer {...data} />
}
Quick start — single-tenant site
Generated storefronts skip tenant resolution and theme from the default tokens
(packages/storefront template):
import { DEFAULT_THEME_TOKENS } from '@thorprovider/runtime/contracts'
import { themeVarsToCss, themeTokensToVars } from '@thorprovider/runtime/render-core/theme'
const themeCss = themeVarsToCss(themeTokensToVars(DEFAULT_THEME_TOKENS))
// then the same <StorefrontProvider themeCss={themeCss} registry={registry}> mount
Data source configuration
/commerce resolves its provider from the environment
(THOR_DATA_SOURCE=mock is the default and needs nothing):
| Variable |
Used for |
THOR_DATA_SOURCE |
mock (default, offline demo data) or medusa |
MEDUSA_BASE_URL |
Medusa backend URL (required in medusa mode) |
MEDUSA_PUBLISHABLE_KEY |
Medusa publishable key (required in medusa mode) |
MEDUSA_REGION_ID |
Region pricing and region-scoped queries |
MEDUSA_CURRENCY_CODE |
Optional currency override |
MEDUSA_SALES_CHANNEL_ID |
Channel scoping (preferred; read by the adapter) |
NEXT_PUBLIC_SALES_CHANNEL_ID |
Legacy fallback for channel scoping |
When the live provider fails, the bridge falls back to mock data and reports it
via console.warn and the source field — it never fails the render silently.
Subpath entry points
| Entry point |
Exports |
@thorprovider/runtime/host-glue |
StorefrontProvider, StorefrontLayout, resolveTenantFromRequest, hostnameFromRequestHeaders, requireTenant, tenantMetadata |
@thorprovider/runtime/host-glue/providers |
StorefrontProvider |
@thorprovider/runtime/host-glue/tenant-resolver |
resolveTenantFromRequest, hostnameFromRequestHeaders |
@thorprovider/runtime/render-core |
createStorefrontApp, catalog, registry, handlers, validateSpecForCatalog, theme utils |
@thorprovider/runtime/render-core/app |
createStorefrontApp |
@thorprovider/runtime/render-core/theme |
themeTokensToVars, themeVarsToCss, cssVarsToStyle, accentColorToOklch |
@thorprovider/runtime/render-core/registry |
registry, handlers |
@thorprovider/runtime/contracts |
Zod schemas and types: theme tokens, spec shapes, tenant config/registry, DEFAULT_THEME_TOKENS, env |
@thorprovider/runtime/commerce |
getCatalog, getProductByHandle, cart/checkout service, transforms, getDataSource |
Documentation
- Getting started — host mounting walkthrough
- host-glue — tenant resolution and providers
- render-core — spec-driven rendering
- contracts — Zod contracts
- commerce — data bridge and cart service
Development
yarn workspace @thorprovider/runtime build # tsup
yarn workspace @thorprovider/runtime dev # tsup --watch
yarn workspace @thorprovider/runtime test # vitest run
yarn workspace @thorprovider/runtime type-check # tsc --noEmit
Layer boundaries
The layers are separated by an invariant that agents and reviewers enforce:
host-glue and render-core are Next/React-bound (.tsx, import
next/*, react, @json-render/next).
contracts is pure Zod/TypeScript — no React, no Next.js.
commerce is JSX-free and Next-free server TypeScript. Its only React
touch is cache() from react in the bridge for per-request memoization.
Regenerating AGENTS.md
packages/runtime/AGENTS.md and the root AGENTS.md are generated:
yarn generate:agents-index
CI fails if they are out of sync with the generator.
Maintenance
- Docs here and in
docs/ describe the exported surface of each layer. When a
subpath export changes, update the affected doc in the same commit.
- The behavioral contract for this package lives in
openspec/specs/storefronts-runtime/spec.md (Spanish). These docs derive
from it; the spec wins on conflict.
- Publishing follows the repo changesets flow — see
PUBLISHING_GUIDE.md at
the repo root.