Live preview - Live updates
Live preview - Live updates
Overview
Live updates is an advanced live preview feature that allows you to preview your changes without clicking the “Refresh preview” button. Whenever content is updated, the changes are immediately displayed in the live preview pane.
Setup
Implementation with Vanilla JS and React
- Initialize the live preview SDK with the live preview provider.
Vanilla JS
React
- Import the
useContentfulLiveUpdateshook and pass the original and full (untransformed) API response data to it. If you’re using inspector mode with Content Source Maps, make sure to pass the encoded result touseContentfulLiveUpdates. The hook then returns the live updated data in real time. See an example in the code snippet below:
sys.id and __typename in your query.Vanilla JS
React
Implementation with Next.js app router
- Create a Next.js client component where you initialize the
ContentfulLivePreviewProviderfrom the SDK.
Once the provider is set up in the client component, you can wrap your server component app with it:
For a full example visit the examples directory on Github.
- The
useContentfulLiveUpdateshook needs to be called in a client component:
I don’t want to use any client components
For customers who prefer not to use client components in Next.js, we recommend implementing a lightweight JavaScript script. This script is designed to trigger a revalidation process via a specified endpoint whenever changes are saved in Contentful, and then it automatically reloads the iframe. Simply place the script in the public folder:
The revalidation endpoint located in app/revalidate/route.ts triggers the revalidation of a specific path based on a query string parameter (pathname) from the request URL:
This ensures that the page doesn’t reload unnecessarily when updates are made to fields not currently displayed. For a full example visit the examples directory on Github.
Provider configuration
The ContentfulLivePreviewProvider accepts parameters that allow you to customize your live preview SDK experience. The following options are available:
To optimize non-preview websites, it is advisable to disable live preview functionality by setting both enableInspectorMode and enableLiveUpdates to “false”. By doing so, any specific data related to live preview, such as data-attributes, is removed.
Known issues and limitations
- GraphQL filters (e.g. filters in the Array field type) or filtering using the select query parameter on the Content Preview API (CPA).
- We currently do not support multiple locales within a single GraphQL query or a single CPA response. Therefore, the
withAllLocaleschain modifier of the JS Client SDK is not supported. - Live updates work only with untransformed data. Ensure that the response from the CPA or GraphQL is provided to the hook in its full and original form, with any potential transformations applied afterwards. Exception: If you’re using inspector mode with Content Source Maps, pass the encoded result into
useContentfulLiveUpdates. - Contentful Functions that resolve data in the delivery context are not compatible with live updates.