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.
<NinetailedProvider//...plugins={[newNinetailedPreviewPlugin({// 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 CMSonOpenExperienceEditor: (experience) => {},// Optional: Callback to handle user forwarding to the audience entry in your CMSonOpenAudienceEditor: (audience) => {},// Optional: Determine the visibility of the Preview Plugin ui: { opener: { hide:false } }, }) ]}> // ...</NinetailedProvider>
gatsby-config.(js|ts)
import {audienceQuery, audienceMapper, experienceQuery, experienceMapper } from'@/lib/yourFunctions'plugins: [// ... { resolve:`@ninetailed/experience.js-gatsby`, options: { clientId:NINETAILED_CLIENT_ID, environment:NINETAILED_ENVIRONMENT, ninetailedPlugins: [ { resolve:`@ninetailed/experience.js-plugin-preview`, name:"@ninetailed/experience.js-plugin-preview", options: {// Options specific to Gatsby - optional customOptions: {// Query all audiences audienceQuery,// Mapper function for audiences audienceMapper,// Query all experiences experienceQuery,// Mapper function for experiences experienceMapper, },// Callback to handle user forwarding to the experience entry in your CMS - optionalonOpenExperienceEditor: (experience) => { },// Determine the visibility of the Preview Plugin - optional ui: { opener: { hide:false } }, }, }, ], }, },];
Define the functions for retrieving and mapping Gatsby data so they can be used in gatsby-config.js. These example queries are compatible with gatsby-source-contentful; adjust your queries based on the content source you are using.
import { Ninetailed } from'@ninetailed/experience.js';import { NinetailedPreviewPlugin } from'@ninetailed/experience.js-plugin-preview'exportconstninetailed=newNinetailed( { clientId:// Your client ID environment: // Your Ninetailed environment }, { plugins: [newNinetailedPreviewPlugin({// 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 CMSonOpenExperienceEditor: (experience) => {},// Optional: Callback to handle user forwarding to the audience entry in your CMSonOpenAudienceEditor: (audience) => {},// Optional: Determine the visibility of the Preview Plugin ui: { opener: { hide:false } } }) ] });
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.
One way of doing this is through a simple instantiation toggle based on some runtime argument. The code sample below 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.
constpreview=process.env.NODE_ENV!=='production'
<NinetailedProvider ... // Other provider propsplugins={[...(preview? [newNinetailedPreviewPlugin({ experiences: audiences: pageProps.ninetailed?.preview.allAudiences || [], }), ]: []),...// Other plugins ]}> ... // Children of provider</NinetailedProvider>
However, this simple toggle will bundle the preview plugin in production settings. A more robust way would be to dynamically import the preview module only when it is required. The below Next.js code sample shows returning a <NinetailedProvider> component with the Preview Plugin only when preview data is supplied, leveraging next/dynamic to exclude the depedency from the bundle.
lib/getNtProvider.tsx
importtype { NinetailedProviderProps } from'@ninetailed/experience.js-next';importtype { PropsWithChildren } from'react';import dynamic from'next/dynamic';import { NinetailedProvider } from'@ninetailed/experience.js-next';/** * Dynamic import of the preview plugin ensures it it not part of a production bundle */exportfunctiongetNinetailedProvider(enablePreview:boolean) {constproviderProps:NinetailedProviderProps= { clientId:process.env.NEXT_PUBLIC_NINETAILED_CLIENT_ID||'', environment:process.env.NEXT_PUBLIC_NINETAILED_ENVIRONMENT||'main', plugins: [// Any plugins that should always be loaded ],// Any other provider config };if (!enablePreview) {returnfunctionStandardNinetailedProvider({ children, }:PropsWithChildren) {return ( <NinetailedProvider {...providerProps}>{children}</NinetailedProvider> ); }; }returndynamic(() =>import('@ninetailed/experience.js-plugin-preview').then((mod) => {returnfunctionNinetailedProviderWithPreviewPlugin({ children, }:PropsWithChildren) {providerProps.plugins = [newmod.NinetailedPreviewPlugin({ audiences:...// your audiences experiences: ...// your experiences...// other config }),...(providerProps.plugins || []), ];return ( <NinetailedProvider {...providerProps}>{children}</NinetailedProvider> ); }; }) );}
Supplying Preview Content
The Preview Plugin expects a specific format for the provided experiences and audiences. Use the appropriate Utility Library for your content source to map these to the required format.