Try “optimization”, “doctor” or “page body”.

    v1.2.2
    Docs menu
    Docs › Grow

    Optimization

    Contentful Personalization per component, resolved on the server so visitors get their variant in the first HTML.

    Contentful Optimization (the Personalization product) is optional. When you add it, each optimized component fetches its own experiences and resolves the visitor’s variant on the server. The variant is in the first HTML. Visitors don’t see the baseline flash, and there’s no extra request.

    Turn it on

    1. Install the Contentful Personalization app on the space, from the Marketplace.

    2. Add Optimization to the project. Either choose it in create-aces-app, or run:

      pnpm aces optimization add                       # asks which components
      pnpm aces optimization add --components hero,lockup --yes

      It installs @contentful/optimization-nextjs and the preview panel, writes the implementation files and sets features.optimization. For each component you pick, it adds an nt_experiences field to the content type. It uses the site’s own space and environment, so you don’t need extra keys.

    3. Create audiences and experiences in Contentful and attach them to entries of optimized components.

    4. Publish both the experience and the entry it’s attached to. The live site reads the Delivery API.

    Optimize more components later

    pnpm aces component add-optimization Hero Banner
    pnpm aces component remove-optimization Banner      # keeps the field in Contentful

    These commands edit the config and the component’s code. The query lines and imports they add are marked aces:optimization, and removing Optimization restores the original files exactly. aces doctor checks that the code and the config agree.

    What the CLI writes

    An optimized component’s query selects its experiences next to its own fields:

    // src/libs/cf/lib/cf-hero/services.ts
    export const HeroQuery = gql`
      query ($id: String!, $preview: Boolean!, $locale: String) {
        hero(id: $id, preview: $preview, locale: $locale) {
          ntExperiencesCollection(limit: 5) { items { ...NtExperience } } # aces:optimization
          ...HeroFields
        }
      }
      ${NtExperienceFragment} # aces:optimization
    `;

    Its server component wraps the render. The SDK picks the visitor’s variant from that data. A variant renders through its own component by entry ID, so a variant can even be a different content type.

    // src/libs/cf/lib/cf-hero/index.tsx
    return (
      <OptimizedBlock data={data} id={id} lang={lang} preview={preview}>
        <CfHero headline={data.headline} image={data.image} id={id} lang={lang} />
      </OptimizedBlock>
    );

    client.tsx gets the same treatment with OptimizedBlockClient, for Draft Mode.

    Nested components

    Button and Hyperlink render inside other components, so they’re marked nested: true. Optimize them like any other component:

    pnpm aces component add-optimization Button Hyperlink

    Their shared fragment then selects the experiences with each variant’s fields inline. Every query that uses the fragment carries the variants, including blocks and navigation. CfButton, CfTextLink and the navigation lists swap in the variant with useOptimizedNested during render, with no extra request.

    Nested components are limited to 2 experiences × 3 variants. Contentful caps GraphQL query complexity at 11,000, and the header query multiplies the limits.

    Configuration

    features: {
      optimization: { enabled: true, consent: "granted", previewPanel: true },
    },
    components: {
      Hero: { /* … */ optimization: true },
    },
    Setting
    consent: "granted" Personalizes and tracks immediately
    consent: "required" Waits until the site sets the aces-optimization-consent=granted cookie (server) or calls setConsent(true) (client)
    previewPanel Shows the Optimization preview panel in Draft Mode. When it’s off, a Reset visitor button appears instead.

    Previewing

    In Live Preview, the Optimization panel switches audiences and variants, and Live Preview keeps working on the variant shown. The panel’s Reset Profile button starts a new visitor. It clears the profile, stored state and cookies, and removes utm_* from the URL first. Profile-based audiences, such as a UTM campaign or page views, then start over. Outside Draft Mode, use a private window.

    Troubleshooting

    • The audience is active but visitors get the baseline. A newly published experience, or a new Personalization app install, takes about a minute to reach the Experience API. Until then it returns experiences: []. Wait and reload; the site doesn’t need changes.
    • “Experience API request … may not be retried” with a 404. The browser sent a visitor ID from another space. The ctfl-opt-aid cookie is shared by every site on a host, for example two dev servers on localhost.
      • The Optimization proxy checks an unknown ID once and drops a foreign one. Check that src/proxy.ts still wraps the app with withOptimization.
      • To keep two local builds apart, open one on 127.0.0.1.
    • A component’s query fails after optimizing it. Its content type is missing the nt_experiences field. Run pnpm aces component add-optimization <name> without --no-model, or check with aces doctor.
    • The preview panel doesn’t appear. The Personalization app isn’t installed in this environment, so the panel skips itself and logs a warning. Install the app; aces doctor reports it as a failure when components are optimized.

    Something wrong or missing? Open an issue.