Tutorial: Working with Rich Content

Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.

Some of the data that the JavaScript SDK returns, such as blog posts and database collection items, contains rich content. Rich content is enhanced text content that can include things like formatted text, link previews, images, and video.

The SDK returns rich content as complex objects. If you’re creating a Headless project with a custom frontend, it can be challenging to render the objects yourself. If you build your frontend using React, you can use the ricos-viewer package from Ricos.js to render rich content objects returned by the SDK. Ricos is an open source product designed for editing and rendering rich content objects. It supports Wix rich content objects.

This tutorial explains how to use Ricos to render the rich content objects returned by the SDK. It includes examples for both the Blog and Data modules.

This process has three steps:

  1. Retrieve your Headless project's rich content using the SDK.
  2. Create a React component that can render rich content objects.
  3. Add the component to your page’s code.

Important: The ricos-viewer package may be deprecated without notice. If you use the package in a production site, make sure to handle errors and include fallback functionality as needed.

Before you begin

Before getting started, you need:

  • A Wix site or Headless project with either the Wix Blog app installed, or with at least one database collection.
  • A blog post or collection item with rich content. To add rich content to a blog post, use the Blog Post Editor to add things like formatted text or images to a post. Use the CMS to add rich content to your collection.
  • Create an OAuth app for your Headless project. Retrieve your app’s client ID. You’ll need to use it in your code.

Step 1. Retrieve rich content data

The first step in rendering rich content is to retrieve it in your frontend code using the SDK. To do this:

  1. In the terminal, run the following commands to install the necessary SDK packages:

    • SDK: yarn add @wix/sdk
    • Blog (if you’re retrieving Blog content): yarn add @wix/blog
    • Data (if you’re retrieving CMS content): yarn add @wix/data
  2. Create a new file in your project called rich-content-api.js.

  3. At the top of your file, add the following import statements.

    Copy
    1
    import { createClient, OAuthStrategy } from '@wix/sdk';
    2
    //If you’re retrieving Blog content:
    3
    import { posts } from '@wix/blog';
    4
    //If you’re retrieving CMS content:
    5
    import { items } from '@wix/data';
  4. Create an SDK client by adding the following code to your code file. Fill in the value for your client ID. Adjust the value of modules if you are only working with Blog or CMS:

    Copy
    1
    const wixClient = createClient({
    2
    auth: OAuthStrategy({
    3
    clientId: '<your-client-id>'
    4
    }),
    5
    modules: { posts, items },
    6
    });
  5. Add a function to your file called getRichContent. This function retrieves the rich content data from your project.

    To retrieve collection items

    Use the SDK Data module’s queryDataItems function to retrieve your project’s CMS items. You can use the query builder to refine your results.

    Example code:

    Copy
    1
    export async function getRichContent() {
    2
    const queryResults = await wixClient.items.queryDataItems({'dataCollectionId':'MyCollectionName'}).find();
    3
    return queryResults.items[0].data.myRichContentField
    4
    }
    To retrieve blog posts

    Use the SDK Blog module’s queryPosts function to retrieve your project’s blog posts. You can use the query builder to refine your results.

    Example code:

    Copy
    1
    export async function getRichContent() {
    2
    const queryResults = await wixClient.posts.queryPosts({fieldsets: ["RICH_CONTENT"]}).find();
    3
    return queryResults.items[0]
    4
    }

Step 2. Create a rich content viewer component

The next step is to create a React component that uses the ricos-viewer package to display rich content objects. The viewer package doesn’t support all the Wix rich content types out of the box. However, Ricos provides plugins for them. Learn more about the plugins in the Ricos documentation.

The viewer generates a React class component with the rendered rich content. This means you can only work with this component in client code. If your project supports React Server Components, make sure to add ’use client’ at the top of your code file.

To create your component, do the following:

  1. Install the ricos-viewer package, its dependencies, and the plugins to support the different Wix rich content types. To do this, run the commands below.
    The commands install the plugins for all the rich content types. If there are some types that you aren’t using in your project, you can leave them out.

    Note: You must use the same version for ricos-viewer and the plugin packages. We recommend major version 9.

    Copy
    1
    yarn add ricos-viewer@9 classnames
    2
    yarn add wix-rich-content-plugin-code-block@9
    3
    yarn add wix-rich-content-plugin-audio@9
    4
    yarn add wix-rich-content-plugin-video@9
    5
    yarn add wix-rich-content-plugin-image@9
    6
    yarn add wix-rich-content-plugin-button@9
    7
    yarn add wix-rich-content-plugin-text-color@9
    8
    yarn add wix-rich-content-plugin-table@9
    9
    yarn add wix-rich-content-plugin-collapsible-list@9
    10
    yarn add wix-rich-content-plugin-divider@9
    11
    yarn add wix-rich-content-plugin-file-upload@9
    12
    yarn add wix-rich-content-plugin-giphy@9
    13
    yarn add wix-rich-content-plugin-hashtag@9
    14
    yarn add wix-rich-content-plugin-headings@9
    15
    yarn add wix-rich-content-plugin-html@9
    16
    yarn add wix-rich-content-plugin-link@9
    17
    yarn add wix-rich-content-plugin-link-preview@9
    18
    yarn add wix-rich-content-plugin-mentions@9
    19
    yarn add wix-rich-content-plugin-social-polls@9
    20
    yarn add wix-rich-content-plugin-spoiler@9
    21
    yarn add wix-rich-content-plugin-vertical-embed@9
  2. Create a new file in your project called RichContentViewer.jsx.

  3. Add ’use client’ to the top of the file if needed. See the introduction to this section for details.

  4. Import React, the viewer package, and all the plugins.

    Copy
    1
    import React from "react";
    2
    import { RicosViewer } from "ricos-viewer";
    3
    import { pluginVideo } from "wix-rich-content-plugin-video/viewer";
    4
    import { pluginAudio } from "wix-rich-content-plugin-audio/viewer";
    5
    import { pluginDivider } from "wix-rich-content-plugin-divider/viewer";
    6
    import { pluginHtml } from "wix-rich-content-plugin-html/viewer";
    7
    import { pluginLink } from "wix-rich-content-plugin-link/viewer";
    8
    import { pluginLinkPreview } from "wix-rich-content-plugin-link-preview/viewer";
    9
    import { pluginImage } from "wix-rich-content-plugin-image/viewer";
    10
    import { pluginTable } from "wix-rich-content-plugin-table/viewer";
    11
    import { pluginGallery } from "wix-rich-content-plugin-gallery/viewer";
    12
    import { pluginGiphy } from "wix-rich-content-plugin-giphy/viewer";
    13
    import { pluginActionButton } from "wix-rich-content-plugin-button/viewer";
    14
    import { pluginHashtag } from "wix-rich-content-plugin-hashtag/viewer";
    15
    import { pluginVerticalEmbed } from "wix-rich-content-plugin-vertical-embed/viewer";
    16
    import { pluginCodeBlock } from "wix-rich-content-plugin-code-block/viewer";
    17
    import { pluginMentions } from "wix-rich-content-plugin-mentions/viewer";
    18
    import { pluginFileUpload } from "wix-rich-content-plugin-file-upload/viewer";
    19
    import { pluginCollapsibleList } from "wix-rich-content-plugin-collapsible-list/viewer";
    20
    import { pluginPoll } from "wix-rich-content-plugin-social-polls/viewer";
    21
    import { pluginSpoiler } from "wix-rich-content-plugin-spoiler/viewer";
    22
    import { pluginHeadings } from "wix-rich-content-plugin-headings/viewer";
    23
    import {
    24
    pluginTextColor,
    25
    pluginTextHighlight,
    26
    } from "wix-rich-content-plugin-text-color/viewer";
  5. Create an array of initialized plugin objects. You can modify the way different rich content types are rendered by passing configuration objects to the plugin functions. For more information, see the Ricos documentation.

    Copy
    1
    const plugins = [
    2
    pluginVideo(),
    3
    pluginAudio(),
    4
    pluginDivider(),
    5
    pluginHtml(),
    6
    pluginLink(),
    7
    pluginLinkPreview(),
    8
    pluginImage(),
    9
    pluginTable(),
    10
    pluginGallery(),
    11
    pluginGiphy(),
    12
    pluginActionButton(),
    13
    pluginHashtag(),
    14
    pluginVerticalEmbed(),
    15
    pluginCodeBlock(),
    16
    pluginMentions(),
    17
    pluginFileUpload(),
    18
    pluginCollapsibleList(),
    19
    pluginPoll(),
    20
    pluginTextColor(),
    21
    pluginTextHighlight(),
    22
    pluginSpoiler(),
    23
    pluginHeadings(),
    24
    ];
  6. Create a React component called RichContentViewer that accepts content as a prop and returns a RicosViewer component with content and the plugins array from the previous step passed in as props. Export the component.

    Copy
    1
    const RichContentViewer = ({ content }) => {
    2
    return <RicosViewer content={content} plugins={plugins} />;
    3
    };
    4
    export default RichContentViewer;

Step 3. Render rich content on a site

The final step is to create a React component that uses the code from the previous sections to retrieve rich content and display it on a page. If you’re working in a framework like Next.js, this component would probably be a React Server Component defined in the code file for a particular page on your site.

To create this component, do the following:

  1. Add the following import statements to your file. Make sure to include the correct paths to your rich-content-api and RichContentViewer files.

    Copy
    1
    import React from "react";
    2
    import { toDraft } from "ricos-content/libs/toDraft";
    3
    import RichContentViewer from "./RichContentViewer";
    4
    import { getRichContent } from "./rich-content-api";

    toDraft() converts the rich content object received from the SDK into Draft.js format that the Ricos viewer uses.

  2. Create a component called RichContent that uses your getRichContent function to retrieve your content, converts to Draft.js format using toDraft(), and then passes the result to your RichContentViewer component.

    Copy
    1
    async function RichContent() {
    2
    const richContent = await getRichContent();
    3
    const draft = toDraft(richContent);
    4
    return (
    5
    <div>
    6
    <div>Your rich content:</div>
    7
    <RichContentViewer content={draft}/>
    8
    </div>
    9
    );
    10
    }
  3. Export another component called Page that includes your RichContent component. In a real site, this component would likely include other elements of your page.

    Copy
    1
    export default function Page() {
    2
    return (
    3
    <RichContent />
    4
    );
    5
    }

Example code

rich-content-api.js

Copy
1
import { createClient, OAuthStrategy } from '@wix/sdk';
2
import { items } from '@wix/data';
3
4
5
const wixClient = createClient({
6
auth: OAuthStrategy({
7
clientId: '<your-client-id>'
8
}),
9
modules: { items },
10
});
11
12
export async function getRichContent() {
13
const queryResults = await wixClient.items.queryDataItems({'dataCollectionId':'BlogPosts'}).find();
14
return queryResults.items[0].data.richcontent
15
}

RichContentViewer.jsx

Copy
1
"use client";
2
import React from "react";
3
import { RicosViewer } from "ricos-viewer";
4
import { pluginVideo } from "wix-rich-content-plugin-video/viewer";
5
import { pluginAudio } from "wix-rich-content-plugin-audio/viewer";
6
import { pluginDivider } from "wix-rich-content-plugin-divider/viewer";
7
import { pluginHtml } from "wix-rich-content-plugin-html/viewer";
8
import { pluginLink } from "wix-rich-content-plugin-link/viewer";
9
import { pluginLinkPreview } from "wix-rich-content-plugin-link-preview/viewer";
10
import { pluginImage } from "wix-rich-content-plugin-image/viewer";
11
import { pluginTable } from "wix-rich-content-plugin-table/viewer";
12
import { pluginGallery } from "wix-rich-content-plugin-gallery/viewer";
13
import { pluginGiphy } from "wix-rich-content-plugin-giphy/viewer";
14
import { pluginActionButton } from "wix-rich-content-plugin-button/viewer";
15
import { pluginHashtag } from "wix-rich-content-plugin-hashtag/viewer";
16
import { pluginVerticalEmbed } from "wix-rich-content-plugin-vertical-embed/viewer";
17
import { pluginCodeBlock } from "wix-rich-content-plugin-code-block/viewer";
18
import { pluginMentions } from "wix-rich-content-plugin-mentions/viewer";
19
import { pluginFileUpload } from "wix-rich-content-plugin-file-upload/viewer";
20
import { pluginCollapsibleList } from "wix-rich-content-plugin-collapsible-list/viewer";
21
import { pluginPoll } from "wix-rich-content-plugin-social-polls/viewer";
22
import { pluginSpoiler } from "wix-rich-content-plugin-spoiler/viewer";
23
import { pluginHeadings } from "wix-rich-content-plugin-headings/viewer";
24
import {
25
pluginTextColor,
26
pluginTextHighlight,
27
} from "wix-rich-content-plugin-text-color/viewer";
28
29
const plugins = [
30
pluginVideo(),
31
pluginAudio(),
32
pluginDivider(),
33
pluginHtml(),
34
pluginLink(),
35
pluginLinkPreview(),
36
pluginImage(),
37
pluginTable(),
38
pluginGallery(),
39
pluginGiphy(),
40
pluginActionButton(),
41
pluginHashtag(),
42
pluginVerticalEmbed(),
43
pluginCodeBlock(),
44
pluginMentions(),
45
pluginFileUpload(),
46
pluginCollapsibleList(),
47
pluginPoll(),
48
pluginTextColor(),
49
pluginTextHighlight(),
50
pluginSpoiler(),
51
pluginHeadings(),
52
];
53
54
const RichContentViewer = ({ content }) => {
55
return <RicosViewer content={content} plugins={plugins} />;
56
};
57
58
export default RichContentViewer;

page.jsx

Copy
1
import React from "react";
2
import RichContentViewer from "@/app/components/RichContentViewer/RichContentViewer";
3
import { toDraft } from "ricos-content/libs/toDraft";
4
import { getRichContent} from "@/app/model/blog/blog-api";
5
6
7
async function RichContent() {
8
const richContent = await getRichContent();
9
const draft = toDraft(richContent);
10
return (
11
<div>
12
<RichContentViewer content={draft}/>
13
</div>
14
);
15
}
16
17
export default function Page() {
18
return (
19
<RichContent />
20
);
21
}

You now have a basic app that can render rich content using React. For more information about working with the SDK, see the SDK Reference.

Was this helpful?
Yes
No