Contentful Algolia Nextjs 14 Datasync
Author Cloudapp
E.G.

Extended content sync from Contentful to Algolia with Next.js 14

April 15, 2024
Table of Contents

Let’s assume you plan to integrate an On-Site-Search on your website/blog and you don’t know what could be a viable solution for you in terms of easy implementation and at no cost.

Have you already heard about the headless CMS Contentful and the AI-Serach Algolia?

As already explained in my previous posts (Step-by-Step Guide for the whole project -> Next js Content Management, Quo Vadis ? ) there is a very generous free plan for Contentful and luckily for Algolia as well.

Algolia — Build Plan (Free)

  • Up to 10k search requests/month

  • 1m records included

  • AI features to test

Contentful — Free Plan

  • 5 users

  • 2 locales

  • 1M API calls/month

  • Community-based support

  • Structured content

  • Developer tools

  • 1 Intro Space

Next.js 14 — Route

  • Built-in API feature from Next.js

Let’s start with registering a new Contentful account (if not already done). Here are two posts with a step-by-step guide:

Contentful account registration

Nextjs 13.5.6 — Complete Example — Typescript / App Router / Contentful and a lot more

Additional information regarding GraphQL and Content Types

Next.js 14 — Complete Example — Typescript / Tailwindcss / Contentful — Part 2

Now we have to complete the registration of an Algolia account

Sign Up for an Algolia account

Where you can do a social login or via your work email. After you are logged in you have to create an application (Algolia creates a default application after the first login). First click on (1), so that you open the settings page and then on (2) to open the application overview page.

Settings-Algolia
Settings-Algolia

Now you can see the application overview page and there is one application without any index (1).

Applications-Algolia
Applications-Algolia

Let’s click on (2) to open the search overview page and then we click on the CTA “Create Index”. We will call it “example_dev”

Indices-Algolia
Indices-Algolia
Algolia Index creation
Algolia Index creation

Now let’s go back to the settings overview page (left bottom symbol -> as shown in the first Algolia screenshot), but now we click on “API Keys” in the left section “Team and Access”. We land on the API Keys overview page, where we need three values (“Application ID”, “Search-Only API Key” and ”Admin API Key”). The Search-Only API Key will be used in the next post, during the frontend implementation of Algolia. Now we need the Admin API Key because we have to write/push data to the previously created index “example_dev”.

API-Keys-Algolia
API-Keys-Algolia

We have both accounts (Contentful and Algolia) ready and have started creating a new Next.js API route for the sync process. If you want you can directly clone this repository

Here is the GitHub link -> GitHub - cloudapp-dev/nextjs14-SEO

which already includes all 6 previous steps (overview here) + the additional SEO topic regarding creating a complete Next.js 14 project from scratch.

We add the following env-variables to our .env.local file

You can use this command for the creation of a new API key

Endpoint creation in Next.js

For the new API endpoint, we create a new folder under /src/app/api called “algoliasync” with this route.ts file in it.

At the top, we import the needed packages

The second step is the security check with the API Key

Webhook creation in Contentful

The last step is setting up the new webhook within Contentful so that we push changes/deletions to the newly created index “example_dev”.

Contentful Webhooks
Contentful Webhooks
add Webhooks contentful
add Webhooks contentful

Here now are all the details for the webhook creation regarding the creation and updates of our data in Algolia

Nextjs-Algolia-Push-Webhooks
Nextjs-Algolia-Push-Webhooks

and here are the second webhooks for removing unpublished blog posts in Contentful from the Algolia index.

Algolia-Delete-unpublished-entries-Webhooks
Algolia-Delete-unpublished-entries-Webhooks

In this tutorial, I used the project, that I created in the previous steps as a foundation and then the Contentful—Algolia sync was built on top.

Cloudapp-dev and before you leave

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏 on our Medium Account

Related articles

Nextjs 14 Part4

Next.js 14 - Complete Example - Custom 404 Page / Loading UI / TailwindCss (ExtraColors) — Part 4

In Part 4 we are going to add a custom 404 page, a loading UI (SVG Spinner) and we add some extra colors to our tailwind.config.ts

Author Cloudapp
E.G.
March 23, 2024
Next.js 14 - typescript contentful tailwindcss - full example

Next.js 14 - Complete Example - Typescript / Tailwindcss / Contentful - Part 2

Let's dig deeper and build a nice UI on our Nextjs foundation with the help of Tailwindcss/Contentful

Author Cloudapp
E.G.
March 11, 2024
Next.js 14 - header footer contentful tailwindcss - full example - part3

Next.js 14 - Complete Example - Header / Footer / Tailwindcss / Contentful - Part 3

Now we have reached part 3 and we will add a header and a footer component, as well as the so-called draft/preview functionality offered by Contentful. Not to forget the nowadays mandatory dark mode toggle.

Author Cloudapp
E.G.
March 18, 2024
nextjs 14 typescript contentful app router - full example

Next.js 14 - Complete Example - Typescript / App Router / Contentful with GraphQL - Part 1

Let’s create a completely new website powered with next.js 14, Contentful, and Typescript

Author Cloudapp
E.G.
March 8, 2024
Nextjs-Speedup

How to speed up your Next.js 14 website and improve your SEO ranking

Page speed is a significant ranking factor for search engines. Next.js offers various features to improve your site’s loading times like optimized image loading.

Author Cloudapp
E.G.
April 5, 2024
Nextjs 14 Part5

Next.js 14 — Complete Example — Adding Internationalization / Multilanguage to our Blog based on Contentful/GraphQL — Part 5

In Part 5, we will add multilanguage support and a language switcher to our example Blog powered by Contentful and the GraphQL API. (Client/Server Components)

Author Cloudapp
E.G.
March 29, 2024