@multitenant/next-app
v0.5.1
Published
Next.js **App Router** integration for `@multitenant/core`.
Readme
@multitenant/next-app
Next.js App Router integration for @multitenant/core.
It provides:
createTenantMiddleware(registry, options)– Next.jsmiddleware.tsfactorycreateTenantMiddlewareFromConfig(config, options)– from@multitenant/next-app/autowhen you already have a loadedTenantsConfigobjectcreateNodeTenantMiddlewareFromProjectRoot(options?)– from@multitenant/next-app/auto-node(Node only): loads./tenants.config.jsonfromprocess.cwd()via@multitenant/configgetTenantFromHeaders(headers, registry, options?)– readResolvedTenantin route handlers / server componentsrequireTenant(headers, registry, options?)– same but throws if missing
Runtime: auto-node uses fs and must not run on Edge. Use auto with a static import or bundled JSON if middleware must run on Edge.
Install
npm install @multitenant/next-app @multitenant/coreUsage
middleware.ts
import type { EnvironmentName } from '@multitenant/core';
import { createTenantMiddleware } from '@multitenant/next-app';
import tenantsConfig from './tenants.config.json';
import { createTenantRegistry } from '@multitenant/core';
const registry = createTenantRegistry(tenantsConfig as any);
const env = (process.env.TENANT_ENV ?? 'local') as EnvironmentName;
export const middleware = createTenantMiddleware(registry, {
environment: env,
});Missing tenant behavior
createTenantMiddleware resolves the tenant from the incoming request Host header (it also respects x-forwarded-host).
If the current host doesn't match any tenant domain (common when you run next dev directly on localhost instead of via multitenant dev), tenant resolution will fail.
By default (onMissingTenant: 'passthrough') the middleware will not throw; it will NextResponse.next() (no tenant headers are added). Your app/server code must still handle getTenantFromHeaders(...) === null if you don’t route through multitenant dev.
If you want a hard failure (or logs), set onMissingTenant: 'throw' (or 'warn').
In route handlers / server components
import { headers } from 'next/headers';
import { getTenantFromHeaders } from '@multitenant/next-app';
import { tenantRegistry } from './tenant-registry';
export async function GET() {
const h = headers();
const tenant = getTenantFromHeaders(h, tenantRegistry, { environment: 'local' });
// ...
}Full App Router checklist: next-app-router.md.
Open source
MIT licensed — github.com/klypalskyi/multitenant · Issues · npm
