@multitenant/next-pages
v0.4.2
Published
Next.js **Pages Router** integration for `@multitenant/core`.
Readme
@multitenant/next-pages
Next.js Pages Router integration for @multitenant/core.
It provides:
withTenantGSSP(gssp, { registry, environment? })– wrapsgetServerSidePropsand injectstenantinto propswithTenantApi(handler, { registry, environment? })– wraps API route handlers and attachesreq.tenant. If the host does not resolve, responds 404 with JSON{ error: string, code: 'MULTITENANT_TENANT_NOT_FOUND' }(same stable code asTenantNotFoundErrorin@multitenant/core).
Install
npm install @multitenant/next-pages @multitenant/coreUsage
pages/index.tsx
import React from 'react';
import type { GetServerSideProps } from 'next';
import { withTenantGSSP } from '@multitenant/next-pages';
import { tenantRegistry } from '../tenant-registry';
import type { ResolvedTenant } from '@multitenant/core';
interface Props {
tenant: ResolvedTenant;
}
export const getServerSideProps: GetServerSideProps<Props> = withTenantGSSP(
async ({ tenant }) => {
return { props: { tenant } };
},
{ registry: tenantRegistry, environment: 'local' },
);
export default function Page({ tenant }: Props) {
return <div>Tenant: {tenant.tenantKey}</div>;
}API routes
// pages/api/example.ts
import type { NextApiResponse } from 'next';
import { withTenantApi, type NextApiRequestWithTenant } from '@multitenant/next-pages';
import { tenantRegistry } from '../../tenant-registry';
export default withTenantApi(
(req: NextApiRequestWithTenant, res: NextApiResponse) => {
res.json({ tenant: req.tenant.tenantKey });
},
{ registry: tenantRegistry, environment: 'local' },
);Open source
MIT licensed — github.com/klypalskyi/multitenant · Issues · npm
