Rift Logo RiftJS

Context

The `Context` object is the foundational state container that powers all page generation, controller execution, collection resolution, and rendering behavior within Rift SSG. It is the central interface that ties together project-wide data, localized configuration, collection access, and page scheduling.

What is Context?

The `Context` object is an encapsulated representation of the current state during a build step. It is passed into every user-facing function in the Rift pipeline, including:

  • params(context) in controller files
  • permalink(context) in routing logic
  • collection(context) in collection API loaders
  • render(context, data) in the rendering stage

Structure and Responsibilities

Internally, the context object includes:

  • A reference to the active Project
  • A shared Map of collections (loaded and grouped by name)
  • A data dictionary to store scoped shared information between calls
  • An optional parent context, which supports scoped child contexts
  • An array of parameters collected via context.param(...)

Context Lifecycle

Understanding how context lives and mutates during different build phases is key to using it effectively.

1. During params(context)

In this phase, the context is shared and mutable. You typically use this to scan collections, derive which pages should exist, and use context.param() to register page variants.

2. During permalink(context)

For each set of parameters declared earlier, a new context is created using .makeChildContext(). This ensures clean page-by-page logic, and allows you to derive a URL (permalink) from that page's localized state.

3. During render(context, data)

At this phase, the context is read-only from the user’s perspective. It contains everything required to render the page: the params, locale, associated layout, and any project-global metadata.

Localization Awareness

One of Rift’s core features is built-in i18n, and the context object plays a central role in this. During all phases, context.locale tells you which locale is currently being processed — for example, "en" or "fr".

Collections Access

The context object gives access to all declared collections via context.collections, which is typically a Map<string, Collection>. These are automatically loaded during the initialization step of the build.

Scheduling Pages

The most important context method during params(context) is:

  • context.param(paramsObject) — This tells Rift: “I want to generate a page for this set of parameters.”

Data Sharing

Because context includes a mutable data object, you can store scoped variables inside it during params() execution and use them across multiple passes.

Developer Tips

  • Always filter collections using context.locale inside controllers to avoid cross-language leaks.
  • Treat context as read-write during params(), and read-only during render().