
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@contentful/live-preview
Advanced tools
Preview SDK for both the field tagging connection + live content updates
Preview SDK for both the field tagging connection + live content updates by Contentful.
It uses Typescript, React and is bundled using Vite.
>=16.15.1
To install live preview simply run one of the following commands.
yarn add @contentful/live-preview
or
npm 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:
import { ContentfulLivePreview } from '@contentful/live-preview';
...
ContentfulLivePreview.init();
The init command also accepts a configuration object that allows you to customize your live preview SDK experience. The following options are available:
import { ContentfulLivePreview } from '@contentful/live-preview';
...
ContentfulLivePreview.init({
enableInspectorMode: false, // This allows you to toggle the inspector mode which is on by default
enableLiveUpdates: false, // This allows you to toggle the live updates which is on by default
debugMode: false, // This allows you to toggle the debug mode which is off by default
});
To use the inspector mode, you need to tag fields by adding the live preview data-attributes (data-contentful-entry-id
, data-contentful-field-id
, data-contentful-locale
) to the rendered HTML element output.
You can do this in React via our helper function.
The necessary styles for the live edit tags can be found in the @contentful/live-preview/style.css
file.
import { ContentfulLivePreview } from '@contentful/live-preview';
import '@contentful/live-preview/style.css';
...
<h1 {...ContentfulLivePreview.getProps({ entryId: id, fieldId: 'title', locale })}>
{title}
</h1>
Live Updates from the editor to your applications are out of the box only supported for React.js at the moment. The updates are only happening on the client-side and in the Live preview environment of contentful.
import { useContentfulLiveUpdates } from '@contentful/live-preview/react';
// ...
const updated = useContentfulLiveUpdates(originalData, locale);
// ...
To use the Contentful LivePreivew 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.
yarn add @contentful/live-preview
or
npm 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.import '@contentful/live-preview/style.css';
import { ContentfulLivePreview } from '@contentful/live-preview/react';
const CustomApp = ({ Component, pageProps }) => (
<ContentfulLivePreviewProvider>
<Component {...pageProps}>
</ContentfulLivePreviewProvider>
)
This provides the posibility to only enable live updates and the inspector mode inside the preview mode:
import '@contentful/live-preview/style.css';
import { ContentfulLivePreview } from '@contentful/live-preview/react';
const CustomApp = ({ Component, pageProps }) => (
<ContentfulLivePreviewProvider enableInspectorMode={pageProps.previewActive} enableLiveUpdates={pageProps.previewActive}>
<Component {...pageProps}>
</ContentfulLivePreviewProvider>
)
export default function BlogPost: ({ blogPost }) {
const inspectorProps = useContentfulInspectorMode()
// Live updates for this component
const data = useContentfulLiveUpdates(
blogPost,
locale
);
return (
<Section>
<Heading as="h1">{data.heading}</Heading>
{/* Text is tagged and can be clicked to open the editor */}
<Text
as="p"
{...inspectorProps({
entryId: data.sys.id,
fieldId: 'text',
locale,
})}>
{data.text}
</Text>
</Section>
);
}
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 identifation 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:
export default function BlogPost: ({ blogPost }) {
const inspectorProps = useContentfulInspectorMode({ entryId: data.sys.id, locale })
return (
<Section>
<Heading as="h1" {...inspectorProps({ fieldId: 'heading' })}>{data.heading}</Heading>
<Text as="p" {...inspectorProps({ fieldId: 'text' })}>
{data.text}
</Text>
</Section>
)
We suggest using the preview mode and the Content Preview API for the best experience.
For a full guide checkout this free course
That's it! You should now be able to use the Contentful Live Preview SDK with Next.js.
To use the Contentful Live Preview SDK with Gatsby, you can start with the gatsby starter contentful homepage
yarn add @contentful/live-preview
or
npm install @contentful/live-preview
import '@contentful/live-preview/style.css';
import React from "react";
import { ContentfulLivePreview } from '@contentful/live-preview/react';
export const wrapRootElement = ({ element }) => (
<ContentfulLivePreviewProvider>{element}</ContentfulLivePreviewProvider>
)
interface HomepageHero implements Node & HomepageBlock {
id: ID!
contentful_id: String! # add this property
heading: String!
text: String
}
type ContentfulHomepageHero implements Node & HomepageHero & HomepageBlock @dontInfer {
id: ID!
contentful_id: String! # and also here
heading: String!
text: String
}
export const query = graphql`
fragment HomepageHeroContent on HomepageHero {
__typename
id
contentful_id # add this property
heading
text
}
`;
export default function Hero({ contentful_id, ...props }) {
const inspectorProps = useContentfulInspectorMode()
// Live updates for this component
const data = useContentfulLiveUpdates(
{
...props,
sys: { id: props.contentful_id },
},
locale
);
return (
<Section>
<Heading as="h1">{data.heading}</Heading>
{/* Text is tagged and can be clicked to open the editor */}
<Text
as="p"
{...inspectorProps({
entryId: contentful_id,
fieldId: 'text',
locale,
})}>
{data.text}
</Text>
</Section>
);
}
That's it! You should now be able to use the Contentful Live Preview SDK with Gatsby.
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.
FAQs
Preview SDK for both the field tagging connection + live content updates
The npm package @contentful/live-preview receives a total of 41,998 weekly downloads. As such, @contentful/live-preview popularity was classified as popular.
We found that @contentful/live-preview demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.