Setting up Next.js
In this chapter we will create a front-end application that renders pages based on content in the CMS.
From an empty folder, run the following command:
npx degit [email protected]:enonic/nextjs-enonic-template.git
The "nextjs-enonic-template" is based on Next.js' own introduction app, so if you’re familiar with that, you’ll recognize the structure of the files.
The template includes some boilerplate we will customize, and some additional code that facilitates and simplifies integration with Enonic - aka "the Enonic adapter".
The following file structure should now exist within your new project folder:
.evn (1) src/ _enonicAdapter/ (2) components/ (3) pages/ [[...contentPath]].tsx (4) _app.tsx (5) _document.tsx (6) _component.tsx (7) _renderable.tsx api/ (8) revalidate.ts preview.ts
|1||Environment variables are placed in this file|
|2||Contains integration and rendering logic to work smoothly together with Enonic XP. You should never need to modify these files. Planned to be released as an NPM in the future.|
|3||Contains your implementation of CMS components|
|4||The fallback next.js router. Enables dynamic rendering based on content in the CMS. File name is not a typo, but Next.js syntax that makes it catch all HTTP requests.|
|5||Invoked on every request. Add common structures here (eg.
|6||Vanilla Next.js file that outlines the basic document structure of all pages.|
|7||Files supporting the visual page editor in Content Studio|
|8||These files support preview mode and regeneration of cached pages|
By default, the configuration should work for your setup, but have a look to be sure:
Verify your configuration
The following configuration values should match your environment.env extract:
# Provide a unique value which will be used to secure the preview mode API_TOKEN=mySecretKey # Enonic app name, must match the name of your app APP_NAME=com.example.myproject # Path to site, or site ID SITE_KEY=/hmdb # Absolute URL to Content API CONTENT_API_DRAFT=http://127.0.0.1:8080/site/hmdb/draft CONTENT_API_MASTER=http://127.0.0.1:8080/site/hmdb/master
|The configuration values can be overridden later when deplying your app to a live server.|
Let’s fire up Next and see if things are working as planned…
|Make sure Enonic and the Headless Movie Database app is running on localhost:8080 before you continue|
Start Next.js in dev mode by running the following commands from within your next project folder:
npm install npm run dev
If Next boots without errors, point your browser to http://localhost:3000 to see the glorious result.
Running Next in
For more details on booting Next, check out the Next.js CLI docs.
The URL structure of your Next site will mirror the structure of the content in the CMS. http://localhost:3000/ is mounted to the site root, which in our case has the internal path
Using the Movie Se7en as an example:
Give the default rendering a spin by trying out some other URLs, for example:
That’s it for the basic Next.js setup.
Next, well have a closer look at how to customize the rendering.