How Page Modules work in Next.js

Follow

In Agility CMS, Page Modules are functional components Editors use to compose what type of content is on each page and in what order they appear.

Developers define what page modules are available in the CMS and what fields they consist of. Each Module defined within Agility CMS should have a corresponding React Component in your Next.js site.

If modules are not defined, or the editor do not add modules to pages, then there won't be anything to output on your pages.

What is in a Page Module?

A Page Module in Agility CMS has a name, description, and fields.

The Name is a representation of the module's intended functionality, such as Posts Listing or Rich Text Area.

It is recommended that each module gets a description, this helps users understand when and where to use this definition.

Fields represent the content that can be managed by editors within the module. These fields are then used in code (passed as props) to display the content the editor intended.

Learn how to How to Create a Module definition.

An Example

In our agilitycms-nextjs-starter site, the name of the module is used to find a corresponding react component that matches the same name. If there is a match, then that component will be dynamically imported and rendered for you.

For example, if a module has the name of RichTextArea in the CMS, while Next.js is rendering your pages, it will look for ./components/agility-pageModules/RichTextArea.js inside of the ./components/agility-pageModules directory.

Screen_Shot_2021-04-09_at_11.35.44_AM.png

The <ContentZone /> component on your Page Templates will dynamically import the module and pass all the field values for the module as props.

import React from "react";
import { renderHTML } from "@agility/nextjs";

const RichTextArea = ({ module }) => {
  // get module fields
  const { fields } = module;
  return (
    <div className="relative px-8">
      <div className="max-w-2xl mx-auto my-12 md:mt-18 lg:mt-20">
        <div
          className="prose max-w-full mx-auto"
          dangerouslySetInnerHTML={renderHTML(fields.textblob)}
        />
      </div>
    </div>
  );
};

export default RichTextArea;

How to Add a new Page Module

If you create a new Page Module within Agility CMS, you'll want to create the corresponding React component for it within the ./components/agility-pageModules directory.

All of the Page Modules that are being used within the site need to be imported into the index file within the ./components/agility-pageModules directory and added allModules array:

import RichTextArea from "./RichTextArea";
import FeaturedPost from "./FeaturedPost";
import PostsListing from "./PostsListing";
import PostDetails from "./PostDetails";
import Heading from "./Heading";
import TextBlockWithImage from "./TextBlockWithImage";

// All of the Agility Page Module Components that are in use in this site need to be imported into this index file.
// Place Page Modules in allModules array below, passing in a name and the component.

const allModules = [
  { name: "TextBlockWithImage", module: TextBlockWithImage },
  { name: "Heading", module: Heading },
  { name: "FeaturedPost", module: FeaturedPost },
  { name: "PostsListing", module: PostsListing },
  { name: "PostDetails", module: PostDetails },
  { name: "RichTextArea", module: RichTextArea },
];

export const getModule = (moduleName) => {
  if (!moduleName) return null;
  const obj = allModules.find(
    (m) => m.name.toLowerCase() === moduleName.toLowerCase()
  );
  if (!obj) return null;
  return obj.module;
};

If there is no React component for your module, then nothing can be rendered for it in your Next.js site.

How to Change the Fields

You can alter your fields at any time and the field values passed to your component will update automatically the next time you run another build.

The most popular and most used Definition Fields are defined below.

Text - How to Add a Text Field Type

Image - How to Add an Image Field Type

HTML - How to Add a HTML Field Type

2 out of 2 found this helpful

Comments

0 comments

Please sign in to leave a comment.