evaluate

Learn how to evaluate a CQL query on the server.

This function evaluates a CQL query against the current visitor’s context on the server.

Call it from a loader, passing the loader’s context as the scope option, to personalize or segment the response before it renders.

Signature

This function has the following signature:

function evaluate<T extends JsonValue>(query: string, options: EvaluationOptions<T>): Promise<T>;

Example

Evaluate a query in a loader:

app/routes/_index.tsx
import {evaluate} from '@croct/plug-hydrogen/server';
const country = await evaluate<string | null>("location's country", {  scope: context,});

Parameters

The following list describes the supported parameters:

query
string

The CQL query to evaluate.

options
object

The evaluation options.

scope
CroctContext

The Hydrogen load context for the current request.

context(optional)
EvaluationContext

The evaluation context to attach to the query. Use it to pass custom attributes that personalize or segment the result. These values are accessible in the context variable.

fallback(optional)
T

The value to return when the evaluation fails, such as on a network error or timeout.