zesty-io / nextjs-starter

NextJS Starter Example for Running Zesty.io CMS
The Unlicense
9 stars 6 forks source link

Zesty.io + Next.js

Quick start Next.js v12 with [Zesty.io]() as a data source

Getting Started

  1. Requirements
  1. Install

Add Zesty CLI (you may need to sudo this command)

npm install -g @zesty-io/cli

Run the Next.js marketing

npx create-next-app --example https://github.com/zesty-io/nextjs-starter

The install process will let you use an existing account or create a new. When using an existing account you will be prompted to select from your available instances.

  1. Change directory to your project
# Use the name you chose for your directory
cd my-app
  1. Start development server
npm run dev
  1. Open application

open browser to http://localhost:3000/

Syncing Zesty.io Models

As you develop your Zesty.io instance you will commonly add new content models. In order for new content pages to render in nextjs, there needs to be a relative model component in views/zesty to get the lastest models components you can run a script to sync. Do so by running the following command at the root of your project.

npm run sync

This sync script will create new files where needed, but will not overwrite existing files.

Working with Zesty View Components

After a npm run sync a view component is created for each Zesty Content Model in the views/zesty directory. Zesty Content Items that have URL will automatically resolve to the component in that views directory that is assocaited with the content models name.

Each Component loads with a {content} object, this object is a direct feed of that URLs ?toJSON response. Read about toJSON

Diagram showing toJSON data fetching

Custom Integration and the next.config.js file

Here is an explanation of the next.js zesty integration, use this information to setup a custom integration or to modify this marketing in your own project.

Required files:

These files should only be modified for customize integrations.

Optional Files

These files can be removed if there references are removed.

next.config.js

In order for the integration to work, you need trailingSlash: true and the env.zesty: {} object. See the below example.

// generated by lib/sync.js
module.exports = {
  trailingSlash: true,
  eslint: {
    ignoreDuringBuilds: true
  },
  env: {
      zesty: {
          instance_zuid: "", // zesty unique id of content instance
          stage: "", // e.g. https://XYZ-dev.webengine.zesty.io
          production: "", // e.g. https://www.acme.com
          stage_password: "",
          src_dir: "", // where the next project has pages, components, etc folders
          options: {
            skip_config_overwrite: false, // for setups with custom config files, after initial setup of the env.zesty object, set to true
            model_ignore_list: [
              '6-xyz-xyz',
              '6-xyz-xyz' // an array of models ZUIDs to ignore when creating component files in views/zesty
            ]
          }

      }
  }
}

If trailingSlash needs to be false in your project, then the lib/api.js fetch call will need to modifed to append a trailing slash. In this scenario, webengine preview proxying your nextjs app will fail.

How to uninstall the Starting Tutorial

The starting tutorial comes with a couple packages and components

Uninstall Material UI (MUI)

npm uninstall @mui/material @mui/styled-engine-sc styled-components

Uninstall Material Icons

npm uninstall @mui/icons-material

Delete Tutorials

From terminal change directory to project root.

rm -Rf views/tutorials

Open pages/index.js delete the line import Tutorial from 'views/tutorial/ and the line <Tutorial/>