This example project demonstrates how to build manageable dotCMS pages headlessly using the Next.js framework. It showcases the integration between dotCMS and Next.js, providing a practical implementation of content-driven web applications.
- Prerequisites
- Getting Started
- Running the Application
- Key Features
- Troubleshooting
- Learn More
- Deployment
Before you begin, ensure you have the following:
- Access to a dotCMS instance (you can use https://demo.dotcms.com if you don't have your own)
- A valid AUTH token for the target dotCMS instance (How to create an API token)
- Node.js (version 18 or higher) and npm installed
- A terminal application
- A code editor of your choice
Open your terminal and create the Next.js app by running the following command:
npx create-next-app YOUR_NAME --example https://github.com/dotCMS/core/tree/main/examples/nextjs
This will create a new Next.js app with the dotCMS example.
To configure the Next.js app to use your dotCMS instance:
- Open the folder
YOUR_NAME
in your code editor - In the root, find the file
.env.local.example
and rename it to.env.local
- Open the
.env.local
file and update the following environment variables:NEXT_PUBLIC_DOTCMS_AUTH_TOKEN
: Your dotCMS auth tokenNEXT_PUBLIC_DOTCMS_HOST
: URL of your dotCMS instance (e.g., https://demo.dotcms.com)
Once configured, follow these steps to run the app:
- Open a terminal in the project root directory
- Install dependencies:
npm install
- Start the development server:
npm run dev
- Open your browser and navigate to
http://localhost:3000
🎉 Congratulations! Your dotCMS Next.js example is now running.
Note: When accessing pages (e.g., localhost:3000/about
), ensure that the corresponding page exists in your dotCMS instance.
This example demonstrates how to integrate dotCMS Vanity URLs with Next.js routing. Vanity URLs in dotCMS provide alternative paths to internal or external URLs, enhancing site maintenance and SEO.
For implementation details, refer to the utils/index.js
file in the example code, which handles Vanity URL redirection.
To enable the Universal Visual Editor (UVE) in dotCMS for your Next.js application, follow these steps:
- In your dotCMS instance, navigate to the "Apps" page
- Find the "UVE - Universal Visual Editor" app and click on it
- Locate the site where you want to enable the UVE and click on it
- In the configuration field, add the following:
{
"config": [
{
"pattern": ".*",
"url": "http://localhost:3000"
}
]
}
- Click on the "Save" button to save the changes
- Now, when you edit any page in dotCMS, you will see the UVE integrated with your Next.js application
For more information about the UVE, please refer to the dotCMS UVE Documentation.
If you encounter issues while setting up or running the dotCMS Next.js example, here are some common problems and their solutions:
Outdated npm packages or dependency issues
The @dotcms/xxx
npm libraries are set to use the latest
tag. Sometimes, npm install
might not install the most recent version, or you might encounter dependency conflicts.
Solution: Perform a clean reinstall of all dependencies by running:
rm -rf .next node_modules && rm package-lock.json && npm i && npm run dev
This command will:
- Remove the
.next
build folder andnode_modules
directory - Delete the
package-lock.json
file - Perform a fresh install of all dependencies
- Start the development server
This ensures you have the latest versions of all packages and resolves most dependency-related issues.
Stale build cache
Sometimes, the Next.js build cache can become stale, leading to unexpected behavior or errors that persist even after updating your code.
Solution: Delete the .next
folder to clear the build cache:
rm -rf .next && npm run dev
This is recommended when:
- You've made significant changes to your project configuration (e.g.,
next.config.js
) - You're experiencing unexplainable build errors
- Your changes aren't reflected in the running application despite saving and restarting the dev server
- You've recently updated Next.js or other critical dependencies
After running this command, Next.js will rebuild the entire project from scratch, ensuring you're working with a fresh build.
Authentication errors (401 Unauthorized)
This often occurs when the environment variables are not set correctly.
Solution:
- Double-check that you've renamed
.env.local.example
to.env.local
. - Ensure you've updated the
NEXT_PUBLIC_DOTCMS_AUTH_TOKEN
in the.env.local
file with a valid token. - Verify that the token hasn't expired. If it has, generate a new one in the dotCMS UI.
Connection issues
If you're having trouble connecting to the dotCMS instance.
Solution:
- Verify that the
NEXT_PUBLIC_DOTCMS_HOST
in.env.local
is correct. - If using
https://demo.dotcms.com
, remember it restarts every 24 hours. You might need to wait or try again later. - Ensure your network allows connections to the dotCMS instance (check firewalls, VPNs, etc.).
Missing pages
If you're getting 404 errors for pages that should exist.
Solution:
- Ensure the page exists in your dotCMS instance. For example, if you're trying to access
/about
, make sure an "about" page exists in dotCMS. - Check if the content types used in the example match those in your dotCMS instance.
- Verify that the content has been published and is not in draft status.
Universal Visual Editor (UVE) not working
If the Universal Visual Editor is not functioning as expected:
Solution:
- Ensure you've correctly configured the UVE in your dotCMS instance as described in the Universal Visual Editor section.
- Verify that your Next.js application is running on
http://localhost:3000
(or update the UVE configuration if using a different port). - Check that you're accessing the dotCMS edit mode from the correct URL.
- Clear your browser cache and try again.
If you continue to experience issues after trying these solutions, please check the dotCMS documentation or reach out to the dotCMS community for further assistance.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out the Next.js deployment documentation for more details.