How to use Structured Text fields with Next.js
Play video »
Sorry, no results found for "".
Rich text in DatoCMS is stored in Structured Text fields, which lets us use it in many different contexts, from HTML in the browser to speech fulfillments in voice interfaces, if that's what you want.
There's a lot to be said about Structured Text and the extensibility of it, but for now let's just say that it returns content in a particular JSON format called dast
which will resemble this example:
To make it easy to convert this format in HTML inside your Next.js projects, we released a package called react-datocms
that exposes a <StructuredText />
component that does all the tedious work for you.
To take advantage of it, install the react-datocms
package if you haven't already:
yarn add react-datocms
Then, inside your page, make a GraphQL query to fetch a Structured Text field, and feed the result to the data
prop of a <StructuredText />
component:
1import { request } from "../lib/datocms";2import { StructuredText } from "react-datocms";3
4const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {5 allBlogPosts(first: $limit) {6 id7 title8 content {9 value10 }11 }12}`;13
14export async function getStaticProps() {15 const data = await request({16 query: HOMEPAGE_QUERY,17 variables: { limit: 10 }18 });19
20 return {21 props: {22 data23 }24 };25}26
27export default function Home({ data }) {28 return (29 <div>30 {data.allBlogPosts.map(blogPost => (31 <article key={blogPost.id}>32 <h6>{blogPost.title}</h6>33 <StructuredText data={blogPost.content} />34 </article>35 ))}36 </div>37 );38}
Other than "regular" formatting nodes (paragraphs, lists, etc.), Structured Text documents can contain three special types of node:
itemLink
nodes are just like regular HTML hyperlinks, but point to other records instead of URLs;
inlineItem
nodes lets you directly embed a reference to a record in-between regular text;
block
nodes lets you embed a DatoCMS block record in-between regular paragraphs;
If a Structured Text document contains one of these nodes, then we need to change the GraphQL query, so that we also fetch all the records and blocks it references. As an example, if the field can link to other Blog posts, and can embed blocks of type "Image block", then the query should change like this:
1const HOMEPAGE_QUERY = `query HomePage($limit: IntType) {2 allBlogPosts(first: $limit) {3 id4 title5 content {6 value7 blocks {8 __typename9 ... on ImageBlockRecord {10 id11 image { url alt }12 }13 }14 links {15 __typename16 ... on BlogPostRecord {17 id18 slug19 title20 }21 }22 }23 }24}`;
We also need to tell <StructuredText />
how you want such nodes to be rendered:
1return (2 <StructuredText3 data={blogPost.content}4 renderInlineRecord={({ record }) => {5 switch (record.__typename) {6 case "BlogPostRecord":7 return <a href={`/blog/${record.slug}`}>{record.title}</a>;8 default:9 return null;10 }11 }}12 renderLinkToRecord={({ record, children }) => {13 switch (record.__typename) {14 case "BlogPostRecord":15 return <a href={`/blog/${record.slug}`}>{children}</a>;16 default:17 return null;18 }19 }}20 renderBlock={({ record }) => {21 switch (record.__typename) {22 case "ImageBlockRecord":23 return <img src={record.image.url} alt={record.image.alt} />;24 default:25 return null;26 }27 }}28 />29);
To see structured text in action with Next.js, check out this tutorial: