Skip to content

Commit

Permalink
feat: update documentation + expose csm rest api function (#687)
Browse files Browse the repository at this point in the history
* docs: add csm readme

* feat: expose cpa response in lp sdk

* docs: examples explanation

* chore: update examples to latest sdk version

* feat: address pr concerns
  • Loading branch information
YvesRijckaert authored Apr 23, 2024
1 parent 7b4ebbd commit 39a2198
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 46 deletions.
33 changes: 33 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Example Projects Overview

This README provides a comprehensive overview of our various example projects to assist you in selecting the most appropriate example based on your specific requirements.

## next-app-router (recommended)

Client-Side Hydration: This setup integrates client components, facilitating the client-side hydration of HTML, CSS, and JavaScript. This feature enables the use of hooks for instant live updates directly in the browser.
Real-Time Live Updates: Features a live update system that allows fields to dynamically update as edits are made, providing immediate feedback without requiring a save action.

## next-14-app-router-ssr

Server-Side Rendering (SSR) Focus: This example exclusively utilizes server-side rendering, with minimal client-side interactions limited to loading the Live Preview SDK in a standalone script.
Conditional Revalidation: Configured to trigger a revalidation process through a designated endpoint whenever changes are saved in Contentful, refreshing the page to display new content after a brief delay of 5 seconds.

## next-13-app-router-ssr

Enhanced SSR with Client-Side Reloading: Operates similarly to the next-14-app-router-ssr but includes a script that forcefully reloads the iframe if an entry’s save event is detected in Contentful, effective after 5-seconds. This script supports both Next 13 and Next 14 versions.

## next-pages-router

Integration with Pages Router: Demonstrates how to utilize the `@contentful/live-preview` SDK within a Next.js Pages Router setup.

## Remix

Remix Framework Compatibility: Provides an example of how to integrate the `@contentful/live-preview` SDK with a Remix application.

## Vanilla JS

Offers a basic demonstration of employing the `@contentful/live-preview` SDK within a straightforward Vanilla JavaScript environment.

## Gatsby

Limited Gatsby Support: Currently, while Inspector Mode is operational in Gatsby, there is an issue with live updates. Due to Gatsby's unique processing of data from the Content Preview API into its GraphQL schema, live updates via the Live Preview SDK are not supported.
2 changes: 1 addition & 1 deletion examples/next-14-app-router-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": "relative-deps"
},
"dependencies": {
"@contentful/live-preview": "2.17.3",
"@contentful/live-preview": "3.1.0",
"next": "14.1.0",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/next-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"setup": "node ./lib/contentful/setup.js"
},
"dependencies": {
"@contentful/live-preview": "^2.17.3",
"@contentful/live-preview": "^3.1.0",
"@contentful/rich-text-react-renderer": "^15.19.4",
"next": "14.1.0",
"react": "^18",
Expand Down
2 changes: 1 addition & 1 deletion examples/next-pages-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@contentful/live-preview": "^2.15.2",
"@contentful/live-preview": "^3.1.0",
"@contentful/rich-text-react-renderer": "^15.19.3",
"contentful": "^10.6.21",
"next": "14.1.0",
Expand Down
2 changes: 0 additions & 2 deletions examples/remix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

This is an example project that demonstrates how to use the `@contentful/live-preview` SDK with a Remix application. The SDK provides live preview functionality for content changes and the inspector mode for your Contentful space.

The `@contentful/live-preview` SDK comes with its own CSS file, which needs to be included in your project. Due to how Remix treats static assets, you will need a build step to bundle and move the third-party CSS into your public directory. We have used esbuild for this.

## 1. Installation

Install the dependencies:
Expand Down
96 changes: 95 additions & 1 deletion packages/content-source-maps/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,97 @@
> ⚠️ **This feature is currently in alpha for selected customers.**
# Content Source Maps

> The base for generating Content Source Maps with Contentful
Automatically add inspector mode to visual fields such as short text, rich text and assets, eliminating the time and effort required for manually inserting data attributes.

## Installation

Install the Live Preview SDK:

```bash
npm install @contentful/live-preview
```

## How it works

The Live Preview SDK transforms the Content Source Maps coming from either the GraphQL API or Content Preview API (REST) into hidden metadata. This process employs steganography to conceal metadata within invisible Unicode characters, containing information to activate inspector mode. These invisible Unicode characters will not alter the visual presentation of your content.

### GraphQL

#### 1. Setting Up GraphQL Queries

Enable Content Source Maps in your GraphQL queries as follows:

```graphql
query @contentSourceMaps {
postCollection(preview: true) {
items {
title
}
}
}
```

The GraphQL API will now return the data along with the Content Source Maps in the `extensions` field.

#### 2. Processing GraphQL Response

Then, pass the data to the provided function `encodeGraphQLResponse` to encode the response:

```jsx
import { encodeGraphQLResponse } from '@contentful/live-preview';

const dataWithAutoTagging = encodeGraphQLResponse(data);
```

When rendering the encoded data in your website, inspector mode will activate automatically.

### REST (Content Preview API)

#### 1. Enable Content Source Maps for the CPA

To enable Content Source Maps using the [Contentful Client SDK](https://github.com/contentful/contentful.js), simply integrate the `alpha_withContentSourceMaps` chain modifier as shown below:

```jsx
import { getEntries } from 'contentful';

const entries = await client.alpha_withContentSourceMaps.getEntries();
```

The CPA will include Content Source Maps within the `sys` object of the response.

#### 2. Processing CPA Response

Then, pass the data to the provided function `encodeCPAResponse` to encode the response:

```jsx
import { encodeCPAResponse } from '@contentful/live-preview';

const dataWithAutoTagging = encodeCPAResponse(data);
```

When rendering the encoded data in your website, inspector mode will activate automatically.

## Troubleshooting / Tips

- 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 `splitEncoding` function provided by the Live Preview SDK to retrieve the content and remove any hidden metadata.

```jsx
import { splitEncoding } from '@contentful/live-preview';

const { cleaned, encoded } = splitEncoding(text);
```

- Images will get automatically tagged if you provide an alt attribute with the asset title or description.

- To stop using manual tags while using Content Source Maps:

```jsx
<ContentfulLivePreviewProvider experimental={{ ignoreManuallyTaggedElements: true }} />
```

## 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.
- Hidden strings will not be generated for fields that end with numeric values.
2 changes: 1 addition & 1 deletion packages/content-source-maps/src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function decode(text: string): SourceMapMetadata | undefined {
return vercelStegaDecode(text);
}

export function split(text: string): {
export function splitEncoding(text: string): {
/** The original string with encoded substring removed */
cleaned: string;
/** The encoded substring from the original string */
Expand Down
2 changes: 1 addition & 1 deletion packages/live-preview-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"homepage": "https://github.com/contentful/live-preview#readme",
"dependencies": {
"@contentful/content-source-maps": "0.2.0-alpha.13",
"@contentful/content-source-maps": "0.3.0",
"@contentful/rich-text-types": "^16.2.0",
"flatted": "^3.2.9",
"graphql-tag": "^2.12.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/live-preview-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeGraphQLResponse, split } from '@contentful/content-source-maps';
import { encodeGraphQLResponse, encodeCPAResponse } from '@contentful/content-source-maps';
import { type DocumentNode } from 'graphql';

import { version } from '../package.json';
Expand Down Expand Up @@ -343,4 +343,4 @@ export class ContentfulLivePreview {
export { LIVE_PREVIEW_EDITOR_SOURCE, LIVE_PREVIEW_SDK_SOURCE } from './constants.js';

export * from './messages.js';
export { encodeGraphQLResponse, split };
export { encodeGraphQLResponse, encodeCPAResponse };
39 changes: 3 additions & 36 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@
"@types/conventional-commits-parser" "^5.0.0"
chalk "^5.3.0"

"@contentful/content-source-maps@0.2.0-alpha.13":
version "0.2.0-alpha.13"
resolved "https://registry.yarnpkg.com/@contentful/content-source-maps/-/content-source-maps-0.2.0-alpha.13.tgz#43f27367c316069fbf30eb7bf22c5e898d7f924d"
integrity sha512-zlwdaA4TFgCIUJjWNtVH4V5MJ3nMQo/XiFGgPbMELz8c3xnoXDaPu9Z8tB0JPCndqSbBcS+ejCVwT2r5lEB5rA==
dependencies:
"@vercel/stega" "^0.1.0"
json-pointer "^0.6.2"

"@contentful/rich-text-types@^16.0.2", "@contentful/rich-text-types@^16.2.0", "@contentful/rich-text-types@^16.3.0":
version "16.3.5"
resolved "https://registry.yarnpkg.com/@contentful/rich-text-types/-/rich-text-types-16.3.5.tgz#927433a84416101ba3dbf19bd0924e755f5e6d65"
Expand Down Expand Up @@ -6943,16 +6935,7 @@ string-argv@0.3.2, string-argv@~0.3.1:
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.2.tgz#2b6d0ef24b656274d957d54e0a4bbf6153dc02b6"
integrity sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -7066,14 +7049,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -7816,7 +7792,7 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -7834,15 +7810,6 @@ wrap-ansi@^6.0.1:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit 39a2198

Please sign in to comment.