Skip to content

Pages with parameters in their address

Most pages in Content have an address you can read in full, such as /about-us. A page whose address has a route parameter is different: one page answers every value of the route parameter. /people/{slug} is reached at /people/ada, /people/grace and any other value, and the route parameter’s value is handed to your renderer.

Content never interprets the value. It does not know whether ada is a real person, and it answers every value that fits the pattern with the page. Your code decides what the page shows for a value, and what happens when a value names nothing. This guide covers what Content delivers, then a typed layer in @ebitex/content-sdk that loads the data once for every section on the page, and finally how a server answers a real 404.

In Composer, when it is turned on for your organization, adding a page offers an address choice of Fixed or Parameter. A parameter page is an ordinary page in every other way: it has a presentation, a publish state, access rules and a place in the tree, and it can have children (/people/{slug}/projects). It has a parameter name (such as slug) instead of a slug, an optional preview value that Composer fills the address with to frame the page (it is never published), and an optional context tag.

The context tag is a label naming what the value stands for, such as people.person. Content stores it and delivers it with the page, and never interprets it. Your loader names the same tag, which is how the SDK knows a loader belongs to a page. An editor guide is on the help page Pages whose address has a parameter.

GET /path resolves the address as usual, and adds a route member when the page was reached through a parameter:

{
"kind": "presentation",
"nodeId": "…",
"path": "/people/ada",
"route": {
"pattern": "/people/{slug}",
"params": { "slug": "ada" },
"context": "people.person"
},
"presentation": { "…": "…" }
}
  • path is the address that was asked for, and route.pattern is the page’s address with its parameters in braces, both in your site’s URL space (so both carry a locale prefix when the site uses one).
  • route.params holds each value, decoded. route.context is the tag, and is left out when the page has none. A page at a fixed address has no route member, and neither does any response from a server that does not know parameters, so always check it before use.
  • A fixed address always wins. If /people/ada is a published page of its own, it answers that address, and every other value reaches the parameter page. A redirect left behind by a rename wins too. Between two parameter pages, the one with the fixed word furthest to the left wins, so /people/{slug}/projects comes before /people/{slug}/{tab}.
  • A value is one whole segment, 1 to 200 characters after decoding, with no /. An address no pattern fits is 404 path_not_found, and so is any address that contains a brace: GET /path/people/{slug} is refused rather than answered as if {slug} were a value.
  • A renamed or moved parameter page leaves a redirect, and the redirect fills in the same value: asking for the old /people/ada gives kind: "redirect" and a targetPath of the new address with ada in it.
  • The response is cached once per page, not once per value, and the value and path are added to each request’s own copy. Every request therefore gets its own path and params, and a caller that tries a thousand values does not fill the cache.

A parameter page has no one address to point at, so it is left out of anything that lists addresses:

  • GET /nodes leaves the page and everything beneath it out of the tree, and a from that names one is an unknown anchor.
  • GET /sitemap leaves them out. The values that would fill the pattern live in your own data, so listing those pages is up to you.
  • A link field to a parameter page delivers url: null, resolved: true and a route of { pattern, parameters }. Fill the pattern with fillRoute below. A link to it inside rich text does not resolve.
  • Reference paths never list a pattern as an address.

resolveLocation carries route through. In React, useRoute() gives a renderer the pattern, the values and the tag, and it never throws: on a page at a fixed address it returns pattern: null and no values.

const { pattern, params, context, notFound } = useRoute()

notFound() is how a presentation says that the value names nothing. The enclosing <Experience> then draws its notFound prop in place of the page, and calls onNotFound once, which is where a server-rendered host sets its response status. The mark belongs to the address, so it clears by itself when the address changes.

fillRoute(pattern, params) builds an address back from a pattern, percent-encoding each value. It throws on a missing or empty one, because an address with a hole in it is never what a caller meant. The helpers need no React or client, so they work on a server and in a script.

A typed route and one loader for every section

Section titled “A typed route and one loader for every section”

A presentation receives only its own template, settings and component, so a section cannot be handed the person that the whole page is about. Without help, each section on /people/{slug} would fetch the person itself. The route layer fixes that: the page owns the data, the sections read it, and TypeScript checks the parameters.

Declare the route once, and bind a loader to the page’s context tag:

route-data.tsx
// The pattern is written by hand, once, and must match the address the site uses. `params` is
// typed from it: `{ slug: string }`.
export const personRoute = defineRoute('/people/{slug}')
// Links to a person: `fill` takes exactly the parameters the pattern names and percent-encodes them.
export const adaLink: string = personRoute.fill({ slug: 'ada' }) // '/people/ada'
export const sameLink: string = fillRoute('/people/{slug}', { slug: 'ada' })
// The loader, bound to the route context tag the page's parameter node carries. It runs once per
// parameter set, however many sections read it. Return `NotFound` when the value names nothing:
// Content answers every value that fits the pattern, so only you know.
export const personData = defineRouteData({
context: 'people.person',
load: async ({ slug }: { slug: string }, { signal }): Promise<Person | NotFound> => {
const response = await fetch(`/api/people/${encodeURIComponent(slug)}`, { signal })
return response.ok ? ((await response.json()) as Person) : NotFound
},
})
  • defineRoute reads the pattern from the string literal, so params is { slug: string }, and fill refuses a parameter the pattern does not have. It is written by hand and must match the address the site uses. In development, useRoute() on it warns once when the delivered page names its parameters differently.
  • defineRouteData ties a tag to a loader. The loader receives the parameters and { locale, signal }, and returns the data or NotFound. Pass signal to fetch: it is aborted when the page moves to another value or unmounts.

Pass the loaders to <Experience> and read them from any section:

route-data.tsx
// Two sections of the page. Neither fetches: both read the one load.
const Heading: PresentationRenderer = () => {
const person = useRouteData(personData)
if (person.status === 'loading') return <h1>Loading…</h1>
if (person.status === 'notFound') return null // the page is already drawing the site's not-found
if (person.status === 'error') return null // the provider's error panel replaces the page
return <h1>{person.value.name}</h1>
}
const Role: PresentationRenderer = () => {
const person = useRouteData(personData)
const { params } = personRoute.useRoute() // typed: params.slug is a string
return <p data-slug={params.slug}>{person.status === 'ready' ? person.value.role : ''}</p>
}
route-data.tsx
export function App() {
return (
<ContentProvider client={content} renderers={{ heading: Heading, role: Role }}>
<Experience
path={pathname}
routeData={[personData]}
notFound={<h1>Not found</h1>}
onNotFound={() => navigate('/404')}
/>
</ContentProvider>
)
}
  • One load, however many sections. The loader runs once per set of parameters and every section that calls useRouteData shares its result. Rebuilding the routeData array on every render is fine.
  • Loading, not-found and errors are values. useRouteData returns status: 'loading' | 'ready' | 'notFound' | 'error', with no Suspense. A loader that returns NotFound makes the page draw the site’s not-found and call onNotFound. A loader that throws goes to the provider’s error panel, never to not-found.
  • The tag is the contract. A loader runs only on a page whose route.context is the tag it names. In development, a loader whose tag differs from the delivered one, or a page with no tag, throws naming both tags, and mounts nothing. In production it mounts no loader for that definition and useRouteData reports notFound. A page with no route (an ordinary page) mounts no loader.
  • One tag takes one loader. Two definitions that name the same tag throw. If a page needs two things, return both from the one loader. Pass a loader only on the pages whose tag it names.

Content answers 200 for every value that fits, so only your code can say the value is wrong, and a server should turn that into a real 404 before it sends anything. loadRouteData runs the same loaders without React:

route-data.tsx
// On a server: resolve the page, load the data, answer a real 404 before rendering anything, and
// hand the values to the browser so it does not load them again.
export async function handle(url: URL): Promise<{ status: number; initialRouteData?: unknown; result?: ExperienceResult }> {
const result = await content.resolveLocation(url)
if (result.kind === 'notFound') return { status: 404 }
if (result.kind === 'redirect') return { status: 302 }
const data = await loadRouteData([personData], result.route, { locale: result.locale })
if (data.notFound) return { status: 404 }
// Render with <Experience result={result} routeData={[personData]} initialRouteData={data} />,
// and embed `data` (it is plain JSON) for the browser's own <Experience initialRouteData>.
return { status: 200, initialRouteData: data, result }
}

loadRouteData returns { values, missing, notFound }. When notFound is true, answer 404. Otherwise render with routeData and initialRouteData={data}, and embed data (it is plain JSON) in the page so the browser’s own <Experience> starts with the values and loads nothing. A loader’s own errors reject the promise, so you can tell a failing backend from a missing value. The server-rendered guide shows the surrounding page.

A site that uses the .NET client reads the same members: PathResult.Presentation.Route has Pattern, Params and Context, and a LinkValue to a parameter page has a Route with Pattern and Parameters. The host answers its own 404 when a value names nothing.

  • The typed route layer reads the pattern from a string literal you write. Types generated from your delivered schema, and a Template that declares which route context it needs, are not available yet.
  • A link field cannot carry values of its own: a link to a parameter page delivers the pattern and you fill it.
  • GET /nodes and GET /sitemap have no member listing parameter pages.
  • A page with a parameter cannot be a redirect, and a redirect cannot point at one.
  • A published parameter page keeps resolving when the feature is turned off for the organization: the switch controls authoring only.