Gathering detailed insights and metrics for @contentful/content-source-maps
Gathering detailed insights and metrics for @contentful/content-source-maps
Gathering detailed insights and metrics for @contentful/content-source-maps
Gathering detailed insights and metrics for @contentful/content-source-maps
Preview SDK for both the field tagging connection + live content updates
npm install @contentful/content-source-maps
99.7
Supply Chain
98.6
Quality
93
Maintenance
100
Vulnerability
99.3
License
@contentful/live-preview@4.5.15
Published on 28 Nov 2024
@contentful/live-preview@4.5.14
Published on 04 Nov 2024
@contentful/live-preview@4.5.13
Published on 18 Oct 2024
@contentful/live-preview@4.5.12
Published on 18 Oct 2024
@contentful/content-source-maps@0.11.6
Published on 18 Oct 2024
@contentful/content-source-maps@0.11.5
Published on 14 Oct 2024
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
68 Stars
1,084 Commits
15 Forks
9 Watching
23 Branches
38 Contributors
Updated on 28 Nov 2024
TypeScript (99.24%)
JavaScript (0.76%)
Cumulative downloads
Total Downloads
Last day
-10.3%
38,416
Compared to previous day
Last week
-1.9%
214,361
Compared to previous week
Last month
15.6%
929,767
Compared to previous month
Last year
0%
4,173,044
Compared to previous year
2
2
Live preview SDK for both the inspector mode connection + live content updates by Contentful.
>=16.15.1
To install live preview simply run one of the following commands.
1yarn add @contentful/live-preview
or
1npm install @contentful/live-preview
To establish a communication between your preview frontend and Contentful, you simply need to initialize the live preview SDK. This can be done by executing the following command:
1import { ContentfulLivePreview } from '@contentful/live-preview'; 2 3... 4 5ContentfulLivePreview.init({ locale: 'en-US'});
The init command also accepts a configuration object that allows you to customize your live preview SDK experience. The following options are available:
1import { ContentfulLivePreview } from '@contentful/live-preview'; 2 3... 4 5ContentfulLivePreview.init({ 6 locale: 'set-your-locale-here', // This is required and allows you to set the locale once and have it reused throughout the preview 7 enableInspectorMode: false, // This allows you to toggle the inspector mode which is on by default 8 enableLiveUpdates: false, // This allows you to toggle the live updates which is on by default 9 debugMode: false, // This allows you to toggle the debug mode which is off by default 10 targetOrigin: 'https://app.contentful.com', // This allows you to configure the allowed host of the live preview (default: ['https://app.contentful.com', 'https://app.eu.contentful.com']) 11});
It is possible to override the locale you set in the init command for a more flexible workflow. If you need to override the locale you can do so either in the getProps command like below:
1ContentfulLivePreview.getProps({ entryId: id, fieldId: 'title', locale: 'fr' });
You can also override it when using our useContentfulLiveUpdates hook like below:
1import { useContentfulLiveUpdates } from '@contentful/live-preview/react'; 2 3// ... 4const updated = useContentfulLiveUpdates(originalData, { locale }); 5// ...
To use the inspector mode, you need to tag fields by adding the live preview data-attributes (data-contentful-entry-id
, data-contentful-asset-id
, data-contentful-field-id
) to the rendered HTML element output.
You can do this in React via our helper function.
1import { ContentfulLivePreview } from '@contentful/live-preview';
2...
3
4<h1 {...ContentfulLivePreview.getProps({ entryId: id, fieldId: 'title' })}>
5 {title}
6</h1>
Live updates allow you to make changes in your editor and see the updates in real time. The updates are only happening on the client-side and in the live preview environment of Contentful. Note: Currently transformations of Contentful response data are not supported. In order for live updates to work as intended, Contentful data must be kept in its original structure.
1import { useContentfulLiveUpdates } from '@contentful/live-preview/react'; 2 3// ... 4const updated = useContentfulLiveUpdates(originalData); 5// ...
For the best experience of live updates together with GraphQL, we recommend to provide your query information to useContentfulLiveUpdates
.
This will benefit the performance of updates and provides support for GraphQL features (e.g. alias
).
1import gql from 'graphql-tag'; 2 3const query = gql` 4 query posts { 5 postCollection(where: { slug: "${slug}" }, preview: true, limit: 1) { 6 items { 7 __typename 8 sys { 9 id 10 } 11 slug 12 title 13 content: description 14 } 15 } 16 } 17`; 18 19// ... 20const updated = useContentfulLiveUpdates(originalData, { query }); 21// ...
ContentfulLivePreview.openEntryInEditor({fieldId: string, entryId: string, locale: string})
Opens an entry in the Contentful live preview editor. This utility function allows for manual control over the editor opening process, providing flexibility for developers to integrate this action within custom UI components or events.
Usage:
1ContentfulLivePreview.openEntryInEditor({ 2 entryId: 'entryId', 3 fieldId: 'fieldId', 4 locale: 'en-US', 5});
targetOrigin
manually. (Init Configuration)You can find an example in the examples/vanilla-js folder.
Before you initialize live preview SDK you'll want to make whatever call (REST or GraphQL API) to get the entries and assets that make up the page being requested.
To use the Contentful Live Preview SDK with [Javascript], you can use the following steps to add it to an existing project.
1yarn add @contentful/live-preview
or
1npm install @contentful/live-preview
ContentfulLivePreview
class' init function.1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <title>Live Preview Example</title> 5 <script type="module"> 6 import { ContentfulLivePreview } from '@contentful/live-preview'; 7 8 ContentfulLivePreview.init({ locale: 'en-US' }); 9 </script> 10 </head> 11 <body></body> 12</html>
data-contentful-entry-id
and data-contentful-field-id
. If you want to override the global locale from the init function, you can set data-contentful-locale
.You can use the provided helper function getProps()
.
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <title>Live Preview Example</title> 5 <script type="module"> 6 import { ContentfulLivePreview } from '@contentful/live-preview'; 7 8 ContentfulLivePreview.init({ locale: 'en-US' }); 9 10 const heading = document.getElementById('demo'); 11 12 /* 13 * Example response 14 * 15 * const props = { 16 * 'data-contentful-field-id': 'fieldId', 17 * 'data-contentful-entry-id': 'entryId', 18 * 'data-contentful-locale': 'en-US', 19 * } 20 */ 21 const props = ContentfulLivePreview.getProps({ entryId: id, fieldId: title }); 22 23 for (const [key, value] of Object.entries(props)) { 24 // change from hyphen to camelCase 25 const formattedName = key.split('data-')[1].replace(/-([a-z])/g, function (m, w) { 26 return w.toUpperCase(); 27 }); 28 29 heading.dataset[formattedName] = value; 30 } 31 </script> 32 </head> 33 <body> 34 <h1 id="demo">Title</h1> 35 </body> 36</html>
4.To use the live updates feature you will have to subscribe for changes for each entry/asset, to get updates when a field is edited in Contentful.
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <title>Live Preview Example</title> 5 <script type="module"> 6 import { ContentfulLivePreview } from '@contentful/live-preview'; 7 8 const locale = 'en-US'; 9 10 ContentfulLivePreview.init({ locale }); 11 12 /** 13 * Subscribe to data changes from the Editor, returns a function to unsubscribe 14 * Will be called once initially for the restored data 15 */ 16 const unsubscribe = ContentfulLivePreview.subscribe({ 17 data, //the JSON response from the CPA for an entry/asset or an array of entries (or assets) 18 locale, 19 callback, //is a function to be called when the entry/asset is updated in Contentful to tell the frontend to update the preview. This callback is what makes the frontend update almost instantaneously when typing in a field in the editor. 20 }); 21 </script> 22 </head> 23 <body></body> 24</html>
That's it! You should now be able to use the Contentful Live Preview SDK with vanilla JS.
You can find an example for the NextJS Pages Router implementation in the examples/nextjs-graphql folder. If you are using the app router you can look at this example or for only serverside rendering this example instead.
To use the Contentful Live Preview SDK with Next.js, you can either use one of the Contentful starter templates, or do the following steps to add it to an existing project.
1yarn add @contentful/live-preview
or
1npm install @contentful/live-preview
ContentfulLivePreviewProvider
and add the stylesheet for field tagging inside _app.tsx
or _app.js
.
The ContentfulLivePreviewProvider
accepts the same arguments as the init function.1import { ContentfulLivePreviewProvider } from '@contentful/live-preview/react'; 2 3const CustomApp = ({ Component, pageProps }) => ( 4 <ContentfulLivePreviewProvider locale="en-US"> 5 <Component {...pageProps}> 6 </ContentfulLivePreviewProvider> 7)
This provides the possibility to only enable live updates and inspector mode inside draft mode:
1import { ContentfulLivePreviewProvider } from '@contentful/live-preview/react'; 2 3const CustomApp = ({ Component, pageProps }) => ( 4 <ContentfulLivePreviewProvider locale="en-US" enableInspectorMode={pageProps.draftMode} enableLiveUpdates={pageProps.draftMode}> 5 <Component {...pageProps} /> 6 </ContentfulLivePreviewProvider> 7)
1export default function BlogPost: ({ blogPost }) {
2 const inspectorProps = useContentfulInspectorMode()
3 // Live updates for this component
4 const data = useContentfulLiveUpdates(
5 blogPost
6 );
7
8 return (
9 <Section>
10 <Heading as="h1">{data.heading}</Heading>
11 {/* Text is tagged and can be clicked to open the editor */}
12 <Text
13 as="p"
14 {...inspectorProps({
15 entryId: data.sys.id,
16 fieldId: 'text',
17 })}>
18 {data.text}
19 </Text>
20 </Section>
21 );
22}
It doesn't matter if the data is loaded with getServerSideProps, getStaticProps or if you load it in any other way.
It's necessary that the provided information touseContentfulLiveUpdate
contains thesys.id
for identification and only non-transformed fields can be updated.
(For GraphQL also the__typename
needs to be provided)
Tip: If you want to tag multiple fields of an entry, you can also provide initial arguments to the hook:
1export default function BlogPost: ({ blogPost }) { 2 const inspectorProps = useContentfulInspectorMode({ entryId: data.sys.id }) 3 4 return ( 5 <Section> 6 <Heading as="h1" {...inspectorProps({ fieldId: 'heading' })}>{data.heading}</Heading> 7 <Text as="p" {...inspectorProps({ fieldId: 'text' })}> 8 {data.text} 9 </Text> 10 </Section> 11 ) 12}
We suggest using the draft mode and the Content Preview API for the best experience.
For a full guide checkout this free course
Due some security settings the draft mode is not always shared with the iframe.
You can find a workaround in our examples
That's it! You should now be able to use the Contentful live preview SDK with Next.js.
🚧 Please be aware that Gatsby support is currently limited. While Inspector Mode is operational, there is a known issue with live updates not functioning as expected.
gatsby-source-contentful
applies transformations to data received from Contentful, which affects the functionality of live updates. To ensure live updates operate properly, it's essential that Contentful data maintains its original structure. However, Inspector Mode remains fully compatible with Gatsby:
1export default function Hero({ data }) {
2 const inspectorProps = useContentfulInspectorMode();
3
4 return (
5 <Section>
6 {/* Text is tagged and can be clicked to open the editor */}
7 <Text
8 as="p"
9 {...inspectorProps({
10 entryId: data.contentful_id,
11 fieldId: 'text',
12 })}>
13 {data.text}
14 </Text>
15 </Section>
16 );
17}
For an example of inspector mode with Gatsby, you can checkout out the examples directory.
For further examples see the ./examples directory.
We want to provide a safe, inclusive, welcoming, and harassment-free space and experience for all participants, regardless of gender identity and expression, sexual orientation, disability, physical appearance, socioeconomic status, body size, ethnicity, nationality, level of experience, age, religion (or lack thereof), or other identity markers.
Read our full Code of Conduct.
The live preview package is open source software licensed as MIT.
No vulnerabilities found.
No security vulnerabilities found.