Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@contentful/live-preview

Package Overview
Dependencies
Maintainers
162
Versions
218
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contentful/live-preview

Preview SDK for both the field tagging connection + live content updates

  • 1.7.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
53K
increased by2.78%
Maintainers
162
Weekly downloads
 
Created
Source

@contentful/live-preview

Warning: This package is currently in an ALPHA state (i.e., not suitable for production use and subject to breaking changes).

Preview SDK for both the field tagging connection + live content updates by Contentful.

It uses Typescript, React and is bundled using Vite.

Getting started

Requirements

  • Node.js: >=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

Documentation

Initializing the SDK

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();

Field Tagging

To tag fields you need to add the live preview data-attributes 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

Live Updates from the editor to your applications are out of the box only supported for React.js at the moment.

import { useContentfulLiveUpdates } from "@contentful/live-preview/react";

// ...
const updated = useContentfulLiveUpdates(originalData, locale);
// ...
Integrating with Gatsby

To use the Contentful Live Preview SDK with Gatsby, you can start with the gatsby starter contentful homepage

  1. Add the @contentful/live-preview package to your Gatsby project by running one of the following commands:
yarn add @contentful/live-preview

or

npm install @contentful/live-preview
  1. In your gatsby-browser.js file, import the live preview styles and initialize the SDK:
import '@contentful/live-preview/dist/style.css';
import { ContentfulLivePreview } from '@contentful/live-preview';

ContentfulLivePreview.init();
  1. In order to tag fields and use live updates, you need to add the contentful_id property to the GraphQL schema. For example, to extend the HomepageHero interface:
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
}
  1. Update the corresponding component to load the contentful_id property:
export const query = graphql`
  fragment HomepageHeroContent on HomepageHero {
    __typename
    id
    contentful_id # add this property
    heading
    text
  }
`;
  1. Add tagging and live updates to your component:
export default function Hero({ contentful_id, ...props }) {
  // 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"
        {...ContentfulLivePreview.getProps({
          entryId: contentful_id,
          fieldId: 'text',
          locale,
        })}>
        {data.text}
      </Text>
    </Section>
  );
}
  1. In Contentful, define the preview environment and configure the preview URL for your Gatsby site. Once you open an entry with a configured preview URL, you can use the Live Preview and all its features.

That's it! You should now be able to use the Contentful Live Preview SDK with Gatsby.

Code of Conduct

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.

License

The live preview package is open source software licensed as MIT.

FAQs

Package last updated on 31 Mar 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc