Skip to content

How to add automatic Time To Read to Contentful and Gatsby

Published at:

This quick guide shows you how you can add automatic Time To Read amounts to your Contentful articles that are connected to Gatsby.

The TTR (Time To Read) calculation happens on build time, meaning that it has zero impact on your client-side performance!

As TTR generation happens on build time, you have to utilize the gatsby-node.js file for it. Add this to the gatsby-node.js file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Package below is required for calculating blog post length in words const { documentToPlainTextString } = require("@contentful/rich-text-plain-text-renderer") // Package below is required for counting the words by turning them into an array const words = require('lodash/words') // This inserts additional "Time To Read" field into article rich text query exports.onCreateNode = ({ node, actions }) => { const { createNodeField } = actions const timeToRead = (content) => { let TTR; // Change the WPM amount (240) below to higher number if your readers are faster // or lower if your readers are slower const avgWPM = 240; const wordCount = words(content).length; TTR = Math.round(wordCount / avgWPM); if (TTR === 0) { TTR = 1; } return TTR } // Change the "ContentfulBlogArticle" below to match your Content Type if (node.internal.type === 'ContentfulBlogArticle') { // Match the "bodyContent" below to match the name of your rich text field const data = JSON.parse(node.bodyContent.raw); const allText = documentToPlainTextString(data); createNodeField({ node, name: 'timeToRead', value: timeToRead(allText) }) } }

In the example above you need to change ContentfulBlogArticle to match the content type of your blog posts and bodyContent to match the name of your article's rich text field. Also if you don't have the@contentful/rich-text-plain-text-renderer and lodash/words packages installed for doing the word counting, do npm i @contentful/rich-text-plain-text-renderer lodash/words to install the necessary packages.

Now you can query the reading time like this (the number is the reading time in minutes):

Screenshot of a GraphQL query showing the time to read amount

And you would probably use it like this:

1 <p>Time to read: ${data.contentfulBlogArticle.fields.timeToRead} minutes</p>

So what does it do exactly? Basically the code adds a new node to be part of the GraphQL query. That node is "timeToRead", which is calculated this way: first rich-text-plain-text-renderer is used to turn the rich text into plain text. Then lodash/wordsis used to turn the plain text into an array of words. That array of words gets counted and then divided by the average words per minute reading ability of an average human. It is around 240, so if you want to go with the average, just keep it at 240. The end result of this is how many minutes it takes to read the whole rich text. The result gets rounded to nearest full digit, so if it happens to be rounded to a zero, we round it up to 1 instead. Now this minute amount is available to be queried via GraphQL.