Controllers in Rift define how pages are generated from dynamic data. They let you control what pages are built, what data is passed into them, and how URLs are structured. Controllers are TypeScript files placed under src/controllers/, and can be referenced from front matter using controller: somefile.controller.ts.
This function is responsible for seeding the generator with parameters for each page to be created. Use ctx.param(...) to define as many entries as needed.
export async function params(ctx) {
ctx.param({ slug: 'hello-world' });
ctx.param({ slug: 'goodbye-world' });
ctx.param({ slug: 'draft-post' });
}
This function receives each param and locale and must return a string path representing the final URL for that page. You have full control over the permalink structure.
export function permalink(ctx) {
return `/${ctx.locale}/blog/${ctx.params.slug}`;
}
This will generate URLs like /en/blog/hello-world or /fr/blog/bonjour-le-monde based on locale and slug.
The real power of controllers comes from combining both functions to produce multiple localized or data-driven pages.
export async function params(ctx) {
const posts = ctx.collections.posts.filter(p => p.locale === ctx.locale);
for (const post of posts) {
ctx.param({ slug: post.slug });
}
}
export function permalink(ctx) {
return `/${ctx.locale}/blog/${ctx.params.slug}`;
}
Rift includes a pagination helper to split arrays into paginated chunks. Use it in params() when rendering lists of data across multiple pages.
import { pagination } from '@riftjs/utils';
export async function params(ctx) {
const allPosts = ctx.collections.posts.filter(p => p.locale === ctx.locale);
const pages = pagination(allPosts, 5);
for (const page of pages) {
ctx.param({ page });
}
}
See Pagination to learn more about page chunking and navigation helpers.