Preview Plugin

Previewing Experiences before they are published.

The Preview Plugin allows you to assign your profile to specific audiences, experiences, and variants within preview and development contexts.

Add plugin to the plugins prop of the NinetailedProvider and provide an array of all Ninetailed Experience and Ninetailed Audience entries to the plugin. Ensure that you are supplying both all published and all draft entries of each to the plugin. Additionally, you should ensure that you are not instantiating the plugin in production contexts.

Installation

Add the dependency:

npm install @ninetailed/experience.js-plugin-preview

Then, add the plugin to the Ninetailed instance:

import { NinetailedPreviewPlugin } from '@ninetailed/experience.js-plugin-preview'
<NinetailedProvider
  //...
  plugins={[
    ...(process.env.NODE_ENV !== 'production' // Example instantiation criteria
      ? [
          new NinetailedPreviewPlugin({
          // Required: Experiences from your CMS
          experiences: yourExperiences || [],
          // Required: Audiences from your CMS
          audiences: yourAudiences || [],
          // Optional: Callback to handle user forwarding to the experience entry in your CMS
          onOpenExperienceEditor: (experience) => { },
            // Optional: Determine the visibility of the Preview Plugin
            ui: { opener: { hide: false } },
          })
        ] : [])
  ]}
>
  // ...
</NinetailedProvider>

Conditional Instantiation

The Preview Plugin is designed to be used within preview and development contexts. This plugin does not automatically turn off in production contexts; you are responsible for instantiating the plugin only when it is required.

The above code sample shows conditionally including the plugin only when process.env.NODE_ENV !== 'production', but you are free to use whatever condition best identifies your non-production environments. For example, you might choose to instantiate the plugin based on whether the current visitor has turned on preview or draft mode and use this condition instead or in conjunction.

Supplying Preview Content

The Preview Plugin expects a specific format for the provided experiences and audiences. Use the appropriate Utility Library to map these to the required format, depending on your content source.

When fetching your Experiences and Audiences from Contentful, we provide a utility package (@ninetailed/experience.js-utils-contentful) which handles the transformation.

import { ExperienceMapper, ExperienceEntryLike, AudienceMapper, AudienceEntryLike } from 'experience.js-utils-contentful';
import { yourContentfulPreviewClient } from 'lib/api'

export const getAllExperiences = async () => {
  const entries = await yourContentfulPreviewClient.getEntries({
    content_type: 'nt_experience',
    include: 1,
  });
  return (entries.items as ExperienceEntryLike[])
    .filter(ExperienceMapper.isExperienceEntry)
    .map(ExperienceMapper.mapExperience);
};

export const getAllAudiences = async () => {
  const entries = await yourContentfulPreviewClient.getEntries({
    content_type: 'nt_audiences',
  });
  return (entries.items as AudienceEntryLike[])
    .filter(AudienceMapper.isAudienceEntry)
    .map(AudienceMapper.mapAudience);
};

Last updated