@rsc-kit/mcp
Version:
An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.
321 lines (244 loc) • 12.5 kB
Markdown
# Domains and subdomains
> A host as a route segment — admin.example.com reaches app/admin, a tenant's host binds [domain] — with nothing to rewrite.
A subdomain is a folder. Name the site's own host once, and
`admin.example.com` is served by `app/admin/`, the same pages as
`example.com/admin`:
```ts title="vite.config.ts"
rscKit({ hosts: ['example.com'] })
```
```text
src/app/
page.tsx example.com/
admin/
page.tsx admin.example.com/ and example.com/admin
[domain]/
settings/
page.tsx acme.example.com/settings, acme.com/settings
```
There is no middleware to write and no rewrite. The router matches the host
itself, so typed links, stored pages and the build all see it.
## Naming the site's host
The router has to know which host is the site. It learns it from
`rscKit({ hosts })` or from the root layout's `metadataBase`. One is enough.
When the address is in the environment, as a Laravel app's is in `APP_URL`,
read it in the Vite config:
```ts title="vite.config.ts"
import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
plugins: [rscKit({ hosts: [new URL(env.APP_URL).host] })],
}
})
```
The build reads it, so the address of the machine running it is the one named:
`example.test` under Herd, the real domain in production.
`hosts` also takes names that are the site rather than a tenant: a staging or
internal name, or a second brand domain:
```ts title="vite.config.ts"
rscKit({ hosts: ['example.com', 'app.internal', 'example.co.uk'] })
```
A literal `metadataBase` works too, but a computed one isn't read for routing.
It's meant for metadata: the base a relative Open Graph image or canonical URL
is made absolute against.
If you name neither, no host is the site's. `merchant.example.com` is then a
foreign host, no folder matches it, and it is served by path: the home page,
with nothing said.
## Serving a subdomain from a folder
Add a folder named for the subdomain:
```text
src/app/
merchant/
page.tsx merchant.example.com/
orders/
page.tsx merchant.example.com/orders
```
A request from a host that isn't the site's own is matched with the host in
front of the path:
| Request | Matched as | File |
| --- | --- | --- |
| `example.com/admin` | `/admin` | `app/admin/page.tsx` |
| `admin.example.com/` | `/admin` | `app/admin/page.tsx`, the same file |
| `acme.example.com/settings` | `/acme/settings` | `app/[domain]/settings/page.tsx`, `domain: "acme"` |
| `acme.com/settings` | `/acme.com/settings` | the same file, `domain: "acme.com"` |
- A subdomain of the site adds its label. Any other host adds the whole host.
- The site's own hosts add nothing, so the apex keeps routing by path. They
are the one in `metadataBase`, `www.` of it, and any named in `hosts`.
- `localhost` and an IP address are always the site's own.
- Listing a subdomain in `hosts` makes it the site, routed by path, rather than
a tenant. That's the "app on `app.example.com`, marketing on the apex" split.
The visitor's URL doesn't change. `acme.example.com/settings` stays in the
address bar, and a link to `/billing` on that page goes to
`acme.example.com/billing`. Only the match changed.
### A subdomain with no folder
A subdomain of the site with no folder of its name is served by path, so
`merchant.example.com/` with no `app/merchant/` serves the home page. That
keeps a preview deployment under the production domain working. The dev server
says so once per host, naming the folder to add:
```
[rsc-kit] merchant.example.test has no app/merchant/ - served by path, as the site's own. Add app/merchant/ to route the subdomain there.
```
Host routing only applies when a route could answer it: a `[domain]` (or
`[host]`) folder at the top of `app/`, or a folder named for the host. Any
other parameter at the top of `app/`, such as `[collection]` or `[slug]`, is a
path segment. An app with no tenant tree routes every host by path, so a proxy
that forwards to the app by an internal name isn't read as a tenant called
`internal`.
## Serving tenants from `[domain]`
A `[domain]` folder at the top of `app/` receives the tenant's host. Check that
the tenant exists in `middleware.ts`. It runs before anything is sent, on every
path, and its `notFound()` is a real 404. Its argument is the route's params:
```ts title="src/app/[domain]/middleware.ts"
import { notFound } from '@rsc-kit/core/not-found';
import { tenantByDomain } from '@/lib/tenants';
export default async function knownTenant({ domain }) {
if (!(await tenantByDomain(domain))) notFound(); // "acme" or "acme.com", as stored
}
```
A page reads the tenant from `params`:
```tsx title="src/app/[domain]/settings/page.tsx"
import { tenantByDomain } from '@/lib/tenants';
export default async function SettingsPage({ params }) {
const { domain } = await params;
const tenant = await tenantByDomain(domain);
return <h1>{tenant!.name} settings</h1>;
}
```
The layout gets the same params, as a promise. A route that lists no URLs is
stored as one shell for every tenant, so put the part that reads the tenant
under `<Suspense>`. Awaited above every boundary, there's no value to store the
shell with, and the build says so and names the layout:
```tsx title="src/app/[domain]/layout.tsx"
import { Suspense } from 'react';
import { tenantByDomain } from '@/lib/tenants';
async function TenantName({ params }) {
const { domain } = await params;
return (await tenantByDomain(domain))!.name;
}
export default function TenantLayout({ params, children }) {
return (
<>
<header>
<Suspense fallback={null}>
<TenantName params={params} />
</Suspense>
</header>
{children}
</>
);
}
```
`[domain]` is an ordinary dynamic segment otherwise:
- `params.domain` is in every page and layout below it, and `error.tsx` and
`loading.tsx` work where you put them.
- A layout is rendered again when one of its own values changes.
`acme.example.com/settings` to `globex.example.com/settings` renders the
tenant layout for globex, and the root layout above it stays mounted.
- A folder named for a host, such as `app/admin/`, wins over `[domain]`, the
way a static segment wins over a parameter anywhere else.
One difference: a `[domain]` at the top of `app/` binds **only from a host**,
never from a path. `example.com/nope` is a 404, not a tenant called `nope`, and
`acme.example.com/` can't be reached as `example.com/acme`.
## Linking to another host
A typed `href` is a folder path, so `/merchant/orders` links to the site by
path. Both addresses work. When the page is meant to be seen on its subdomain,
link with `hostUrl`:
```tsx title="src/app/page.tsx"
import Link from '@rsc-kit/core/Link';
import { hostUrl } from '@rsc-kit/core/routes';
<Link href={hostUrl('merchant', '/orders')}>Orders</Link> // //merchant.example.com/orders
<Link href={hostUrl(tenant.slug, '/settings')}>Settings</Link> // //testbrand.example.com/settings
<Link href={hostUrl('acme.com', '/settings')}>Settings</Link> // //acme.com/settings
<Link href={hostUrl('/pricing')}>Pricing</Link> // //example.com/pricing, from a subdomain
```
- The first argument autocompletes to the top-level folders of `app/`, and
takes any other string too, such as a tenant's subdomain read from the
database. A name with a dot is a whole domain, used as written.
- The path is typed by what that host answers. For a folder it is the pages
under it, with the folder taken off: `hostUrl('merchant', '/orders')`, not
`'/merchant/orders'`. For any other name it is the tenant tree's paths.
- With one argument it is the site itself: the link back from a subdomain,
where a plain `/pricing` would stay on the subdomain.
It works in `visit()` and `redirect()` too:
```ts
redirect(hostUrl(tenant.slug, '/welcome'));
```
The site's host is the first one `hosts` names (or a literal `metadataBase`),
port included. So `hosts: [new URL(env.APP_URL).host]` gives
`merchant.example.test` under Herd and `merchant.example.com` in production.
With no host named, a folder's URL is its path, `/merchant/orders`.
The URL is the same string on the server and in the browser, so the link
hydrates as it was rendered. On the host it already names, `Link` navigates
without a reload. To another host it is a full page load, as any link to
another origin is.
## Keeping a folder on its subdomain only
When a folder is only ever meant to be seen on its subdomain, such as a
merchant dashboard or an admin, name it in `subdomains`. Its subdomain becomes
the page's only URL:
```ts title="vite.config.ts"
rscKit({ hosts: [new URL(env.APP_URL).host], subdomains: ['merchant'] })
```
- `example.com/merchant/orders` answers 301 to `merchant.example.com/orders`,
with the query kept.
- `<Link href="/merchant/orders">`, `visit('/merchant/orders')` and
`redirect('/merchant/orders')` go to `merchant.example.com/orders`. The typed
href stays the folder path, and the host is added for you, with no `hostUrl`.
- On the subdomain, a root path that isn't under the folder is the site's:
`<Link href="/pricing">` goes to `example.com/pricing`.
- The build's route table lists the folder, so a backend that names routes puts
them on the subdomain. Laravel's `route('merchant.orders')` is
`https://merchant.example.com/orders`.
The redirect and the links only apply on the site's own host. On `localhost`,
an IP address, a preview or a platform's own name (`<app>.homeport.run`), the
path form still renders and links keep it, since there is no subdomain of
those to go to.
`example.com/merchant` is then no longer a page of the site: it is the
subdomain's home page. Put a landing page about merchants in a folder of its
own, such as `app/sell/`. `subdomains` needs the site's host (`hosts`) and a
folder of that name, or dev and the build stop and say which is missing.
Two things the build enforces:
- `edge.json` lists none of the folder's pages. An edge answers by path and
would skip the 301.
- A `backendPaths` entry under the folder stops the build, because visitors
there send `/auth`, never `/merchant/auth`.
## Building a page per tenant
List the tenants in `generateStaticParams` on the tenant route. Each listed
host is rendered at build and stored, one file per host:
```ts title="src/app/[domain]/page.tsx"
export async function generateStaticParams() {
const tenants = await db.tenant.findMany({ select: { domain: true } });
return tenants.map((t) => ({ domain: t.domain }));
}
```
A host added later falls through to the plain tree or, if the page reads the
request, renders on demand. A tenant's page that reads `cookies()` or awaits
`connection()` is dynamic for that tenant and stored for none, like any page.
## Trying it locally
`localhost` and an IP address are always the site's own, so a dev server
opened on one routes by path as usual. A local name such as `example.test`
under Herd or Valet is a host like any other: `merchant.example.test` reaches
`app/merchant/` only when `example.test` is named in `hosts`. Reading
`APP_URL`, as above, does that. A `metadataBase` fixed to the production host
names only that one.
To try a tenant without DNS, send the host the router will see in production:
```bash
curl -H 'X-Forwarded-Host: acme.example.com' http://localhost:3000/
```
Or point `acme.example.com` at `127.0.0.1` in `/etc/hosts` and open it on the
dev server's port.
## Behind a proxy
The host is read from `X-Forwarded-Host` first, then `Host`. A load balancer
that terminates TLS and forwards to the app by an internal name still routes by
the name the visitor typed.
## Static exports
An export is served by a file server, which sees no host. Host routing is a
server feature, so an exported site is the site's own on every host it is
served from.
## Coming from Next
Next routes a subdomain with a `middleware.ts` that rewrites
`acme.example.com/settings` to `/acme/settings`. Delete that rewrite, and the
`[domain]` folder works as it did: the segment binds the same value. Next's
`[domain]` folder also matches any path once the rewrite is in place, which is
why Next apps tuck the tenant tree under a route group. Here it binds only from
a host, so it doesn't need one. Next's `rewrite()` for anything else isn't
here: a host maps to a tree by file, not by code.