Selegic CRM Docs
ServerMiddleware & Lifecycle

Hook lifecycle

How lifecycle hooks are executed inside middleware.

The lifecycleMiddleware provides the global hook execution context for every request. It runs two phases:

  • runBefore(c) — executed before the route handler (can mutate request-scoped state).
  • runAfter(c) — executed after the route handler (used for telemetry, notifications, cleanup).

Hooks are defined via the lib/hooks registry and should be:

  • idempotent (for safety on retries)
  • fast (timeout-protected)
  • non-throwing where possible (use logging or return recoverable errors)

Example hooks: request timing, feature-flag checks, request auditing.