Was this page helpful?

Content preview with Content Source Maps and Vercel

Overview

Content Source Maps is a feature that automatically tags text fields, Rich Text fields, and assets. When integrated with Vercel Content Links (formerly known as Vercel Visual Editing), it allows you to open a relevant Contentful entry by clicking a button in a field in your Vercel preview. This is particularly helpful when authors share the preview URL with colleagues, as it enables them to access the correct Contentful entry with a single click.

Vercel and CSM overview

Content Source Maps are only available on our Premium plan. Vercel Content Links are only available on Vercel Pro and Enterprise plans.
Content Source Maps can also be used within Contentful live preview to simplify Inspector mode setup.

How it works

When enabled, our APIs return source maps for certain visual fields that have links to the correct field in Contentful. The Live Preview SDK then transforms these Content Source Maps to hidden metadata into the results of your queries.

This integration employs a method known as steganography, which conceals hidden information within other data. Specifically, this approach embeds the metadata into invisible Unicode characters incorporated into the existing text strings.

The use of these "enhanced strings" on your web pages allows Vercel preview deployments to detect and decipher the concealed metadata in the final HTML. This enables the display of interactive links that guide users directly to the relevant field in Contentful.

Supported field types

Content Source Maps support the following field types:

  • Short text (Symbol) and Long Text: We only support fields with the Appearance setting Single line and Multiple line. We purposely don’t support URL, Dropdown, Radio, and Slug.
Radio and Dropdowns are not supported since they are primarily used for non-visual text purposes, such as CSS styling properties (e.g. a #F4F4F4 color code in a dropdown for the button color).
We are still working on a solution to also support Markdown.
  • Rich Text, including embedded references and assets.
  • Lists of Short text (Symbol).
  • Title and Desciprtion fields in an asset - Use one of them as the alt attribute for the image.
Custom widgets of supported field types can also be tagged with Content Source Maps.

We don’t support URL or ISO Date values (even in the supported field types), because adding hidden metadata would cause issues.

Implementation

Content Source Maps can be set up according to one of the following options:

GraphQL

  1. Install the latest version of the Live Preview SDK.
npm install @contentful/live-preview
  1. Use the query level directive in your GraphQL queries.
Content Source Maps will only be generated for preview content.

Content Source Maps return from our GraphQL API under the “extensions”:

query @contentSourceMaps { //Add the query level directive
      postCollection(preview: true) { //And preview: true
         items { 
            title
         } 
    }
}
   // This will return:
   {
     "data": ...
     "extensions": ... //Content Source Maps will be here
}
  1. Use the encodeGraphQLResponse function from the Live Preview SDK by passing it the GraphQL Response with Content Source Maps. It will return with your content that includes the hidden metadata to enable Vercel Content Links.
import { encodeGraphQLResponse } from "@contentful/live-preview";
const dataWithAutoTagging = encodeGraphQLResponse(data);

Content Preview API (CPA)

Enable Content Source Maps using the Contentful Client SDK, by setting includeContentSourceMaps in the client to true:

export const clientPreview = createClient({
space: process.env.CONTENTFUL_SPACE_ID!,
          accessToken: process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN!,
     host: "preview.contentful.com",
       includeContentSourceMaps: true
});

Without Contentful.js SDK

Directly access the Contentful CPA without using the Contentful.js SDK.

Please be aware that without the Contentful Client SDK, certain protections, such as automatically requesting the sys.id, are not enforced. To ensure Content Source Maps function properly, the complete sys object needs to be retrieved. Therefore, using a select operator to exclude this from the response would cause errors.

  1. Add &includeContentSourceMaps=true to the URL:
fetch("https://preview.contentful.com/spaces/:spaceId/environments/:e nvId/entries&includeContentSourceMaps=true",
    {
      method: "GET",
      headers: {
        Authorization: "Bearer YOUR_ACCESS_TOKEN",
        Content-Type: "application/json",
      },
} )
  1. Use the encodeCPAResponse function from the Live Preview SDK by passing it the CPA Response with Content Source Maps. It will return with your content that includes the hidden metadata to enable Vercel Content Links.
   import { encodeCPAResponse } from "@contentful/live-preview";
   const dataWithAutoTagging = encodeCPAResponse(data);

Troubleshooting and tips

We've taken a cautious approach in determining which fields to generate Content Source Maps for, ensuring that it won't disrupt your website. The remaining potential issues might include the following:

  • Under certain circumstances, such as when applying letter-spacing in CSS, fields may display styles that weren't intended. In these cases, you can utilize the functions provided by the Live Preview SDK to retrieve the content and remove any hidden metadata.
import { splitEncoding } from "@contentful/live-preview";
const { cleaned, encoded } = splitEncoding(text);

Known issues and limitations

  • Markdown support is currently in development.
  • Adding hidden metadata to content can result in problems, e.g. when being used for CSS values, for dates or URL content. You can remove the hidden strings using the splitEncoding function from the Live Preview SDK.
  • Encoding is skipped on the following formats:
    • Any date format that does not use English letters (e.g. 4/30/24);
    • ISO dates (e.g. 2024-04-30T12:34:59Z);
    • URLs.
  • We intentionally avoid tagging certain widgets, such as dropdowns, because they are primarily used for non-visual text purposes, such as CSS styling properties (e.g. #f4f4f4 color code). Adding hidden strings to these fields could potentially cause issues.