Skip to Content
Octopus Energy Packages

Storyblok A/B Testing

Export reference for Storyblok Experiments middleware

import {
  createStoryblokExperimentsMiddleware,
  createStoryblokExperimentsSource,
  getCanonicalSlug,
} from "@octopus-energy/flags/storyblok";

This entry point requires the @storyblok/experiments peer dependency. For setup and middleware composition, use the Storyblok guides.

createStoryblokExperimentsSource(options)

Returns ExperimentsSource, an async function that fetches the running experiments from Storyblok's v2/cdn/experiments endpoint.

The source throws for non-successful responses. It always requests the current published version rather than retaining an in-memory payload.

Every original_slug in the returned payload is the original story's full slug (for example blog/test), including on the control variant. Storyblok reports only the story's own slug, so the folder prefix(es) are rebuilt from the story mapping's variant_slug.

createStoryblokExperimentsSource

Prop

Type

CreateStoryblokExperimentsSourceOptions

Prop

Type

createStoryblokExperimentsMiddleware(options)

Returns StoryblokExperimentsMiddleware, which accepts a NextRequest and resolves to StoryblokMiddlewareResult.

StoryblokMiddlewareResult extends MiddlewareResult: requestHeaders are the complete headers for downstream middleware, and response(response?) materialises identity metadata and a possible assignment rewrite on the final response. The middleware fails open when experiments, matching, or identity is unavailable.

The required routing configuration names the internal route that renders each assignment: routing.rewritePathname receives every assignment, control included, and typically points at a dedicated experiments route or a page that embeds the experimented story. routing.storyblokSlug is optional and defaults to the slug derived from the request pathname. Visitors who cannot be assigned are served the public page.

createStoryblokExperimentsMiddleware

Prop

Type

CreateStoryblokExperimentsMiddlewareOptions

Prop

Type

StoryblokExperimentsRouting

Prop

Type

StoryblokExperimentRewriteContext

Prop

Type

StoryblokMiddlewareResult

Prop

Type

getCanonicalSlug(slug, experiments)

Returns the slug whose public URL is canonical for a rendered Storyblok story: the original story's slug for a variant story slug, and slug unchanged otherwise. Use it for <link rel="canonical">, because variants are served at the original URL and direct variant URLs redirect there.

getCanonicalSlug

Prop

Type

On this page