V3 brings significant improvements, most notably that Payload is now a native Next.js app. This change means Payload uses features like the App Router and Next.js route handlers, making it incredibly easy to integrate with any (existing) Next.js frontend.
In this article, we'll guide you through creating a new project using Payload CMS and Next.js, setting up Tailwind CSS, leveraging hot module reloading, and deploying your project on Vercel.
To begin, let's create a new project powered by Payload CMS and running on a Next.js frontend.
Open your terminal and go to the folder where you want to create your project.
Run the following command to set up a new Payload app. Note that it’s in beta, so we need to specify the beta version.
npx create-payload-app@betaYou'll be prompted to name your project. For this example, let's name it "my-first-blog". Choose the blank template (other templates will be available in the stable release) and select MongoDB as your database. You can use a local or remote MongoDB connection string.
? How should I name this project? my-first-blog
? Choose a template: blank
? Choose your database: MongoDBIn the app folder, create a root layout by adding a layout.jsx or layout.tsx file and start building your frontend with Next.js.
Here's a boilerplate root layout file: https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-2-creating-a-root-layout
Adding Tailwind CSS to your project is now easier than ever.
Follow the Tailwind CSS installation guide for Next.js.
https://tailwindcss.com/docs/guides/nextjs
Set Up Global Styles: Create a global.css file in the app folder and import your Tailwind styles.
@tailwind base;
@tailwind components;
@tailwind utilities;import './global.css';// components/TestComponent.jsx
export default function TestComponent() {
return (
<div className="bg-green-500 p-4 text-green-100">
Hey, this is a custom component!
</div>
);
}One of the standout features in version 3 is hot module reloading. Changes to your configuration now appear instantly without needing to restart the server.
Try it out by making changes to a collection schema, and watching the admin panel update in real time
With Payload CMS v3, all custom components are server components by default, allowing direct data fetching from Payload or any third-party API.
Create a Server Component: Fetch data directly within your server component.
import { getPayload } from 'payload';
import config from '@payload/config';
export default async function TestComponent() {
const payload = await getPayload({ config });
const users = await payload.find({
collection: 'users',
});
return (
<div>
<h3>Users</h3>
<div className="flex gap-2">
{users.docs.map((user) => (
<div key={user.id}>{user.email}</div>
))}
</div>
</div>
);
}'use client';
import { useState } from 'react';
export default function ClientComponent() {
const [test, setTest] = useState('Test');
return <div>{test}</div>;
}Deploying your project on Vercel is straightforward and ensures your application is live with minimal effort.