Analyzing the Objective
I've been focusing on thoroughly grasping the user's objective: a Next.js boilerplate that seamlessly integrates Prisma and Tailwind CSS. The aim is to deliver a convenient starting point, essentially a "jumpstart" for their projects. Currently, I'm working on clarifying the most streamlined path, balancing ease of use with flexibility for customization.
Detailing the Technical Components
I'm now diving into the specifics of the tech stack. Next.js App Router with TypeScript is the clear choice. For Prisma, I'm leaning towards PostgreSQL for generalizability, though I'll highlight SQLite's convenience. Tailwind's built-in presence simplifies things. I've started outlining the response structure, from initialization to running a sample CRUD page, and even considered a bonus section, possibly focusing on best-practice code organization.
Mapping Out the Initial Run
I've been meticulously planning the initial setup process, breaking it down into a sequence of clear, actionable steps. First, the user will leverage create-next-app to set the project's foundation. Then, I'll guide them through installing and initializing Prisma, followed by its configuration, with an emphasis on choosing SQLite for simplicity's sake. The Prisma client singleton step is crucial to prevent database connection exhaustion, so I'll highlight that too. Finally, a Tailwind check will ensure everything is visually ready to go.
Demonstrating Full Functionality
I'm now focusing on a complete example showcasing the integration. I'll include a server component to fetch and display data, alongside a server action for creating new entries. This demonstrates the seamless synergy of the technologies. The code will use a simple "Post" model. I'll show fetching with prisma.post.findMany() and mutating with an async server action inside page.tsx, keeping things straightforward. I'm also revising and refining the key code snippets.
Refining the Setup Process
I'm now zeroing in on the finer points of the boilerplate's setup. I'm streamlining the initial create-next-app instructions by incorporating necessary flags to circumvent the interactive mode, making the process smoother. I'm also ensuring the user remembers to inspect and properly configure environment variables in .env. I've also considered adding value, thinking about common add-ons like clsx and react-icons. Finally, I'm polishing the final output to maintain a helpful and concise tone.
Here is a complete, step-by-step guide to generating a robust Next.js (App Router) + TypeScript + Prisma + Tailwind CSS starter template.
1. Initialize the Project
Run the following command in your terminal. Next.js now comes with Tailwind CSS built-in during the setup process.
npx create-next-app@latest my-starter --typescript --tailwind --eslint
Select the following options when prompted:
- Use
src/ directory? No (or Yes, strictly personal preference)
- Use App Router? Yes (Recommended)
- Customize the default import alias? No (Defaults to
@/*)
cd into your folder:
cd my-starter
2. Install and Setup Prisma
Install the development CLI and the runtime client:
npm install prisma --save-dev
npm install @prisma/client
Initialize Prisma. This creates a prisma folder and an .env file:
npx prisma init --datasource-provider sqlite
Note: I used sqlite for this boilerplate so it works out-of-the-box without setting up a local database server. You can easily change this to postgresql later.
3. Configure Database Schema
Open prisma/schema.prisma and define a sample model:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
// Example Model
model Post {
id String @id @default(uuid())
title String
content String?
createdAt DateTime @default(now())
}
Push this schema to your local database:
npx prisma db push
4. Create Global Prisma Client (Best Practice)
In Next.js dev mode, hot reloading can exhaust your database connections. You need a "Singleton" pattern to prevent this.
Create a file at lib/prisma.ts:
// lib/prisma.ts
import { PrismaClient } from '@prisma/client'
const prismaClientSingleton = () => {
return new PrismaClient()
}
declare global {
var prismaGlobal: undefined | ReturnType<typeof prismaClientSingleton>
}
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()
export default prisma
if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma
5. Clean up Tailwind Config (Optional)
Ensure your tailwind.config.ts looks like this to scan file changes correctly:
import type { Config } from "tailwindcss";
const config: Config = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {},
},
plugins: [],
};
export default config;
6. Create an Example Page (Server Actions + Server Component)
Let's verify everything works by building a simple page that fetches data (Server Component) and adds data (Server Action).
Open app/page.tsx and replace it with:
import prisma from "@/lib/prisma";
import { revalidatePath } from "next/cache";
export default async function Home() {
// 1. SERVER SIDE DATA FETCHING
const posts = await prisma.post.findMany({
orderBy: { createdAt: "desc" },
});
// 2. SERVER ACTION (Form Handler)
async function createPost(formData: FormData) {
"use server";
const title = formData.get("title") as string;
const content = formData.get("content") as string;
await prisma.post.create({
data: { title, content },
});
revalidatePath("/");
}
return (
<main className="flex min-h-screen flex-col items-center p-24 bg-gray-50 text-gray-900">
<h1 className="text-4xl font-bold mb-8">Starter Template</h1>
{/* FORM */}
<form action={createPost} className="w-full max-w-md bg-white p-6 rounded-lg shadow-md mb-8">
<div className="mb-4">
<label className="block text-sm font-bold mb-2">Title</label>
<input
name="title"
type="text"
required
className="w-full p-2 border border-gray-300 rounded"
placeholder="Enter title..."
/>
</div>
<div className="mb-4">
<label className="block text-sm font-bold mb-2">Content</label>
<textarea
name="content"
className="w-full p-2 border border-gray-300 rounded"
placeholder="Enter content..."
/>
</div>
<button
type="submit"
className="w-full bg-blue-600 text-white font-bold py-2 px-4 rounded hover:bg-blue-700 transition"
>
Add Post
</button>
</form>
{/* DATA DISPLAY */}
<div className="w-full max-w-md space-y-4">
{posts.map((post) => (
<div key={post.id} className="p-4 bg-white border border-gray-200 rounded shadow-sm">
<h2 className="text-xl font-semibold">{post.title}</h2>
<p className="text-gray-600">{post.content || "No content"}</p>
<span className="text-xs text-gray-400">
{post.createdAt.toLocaleDateString()}
</span>
</div>
))}
{posts.length === 0 && (
<p className="text-center text-gray-500">No posts yet.</p>
)}
</div>
</main>
);
}
7. Run the Project
You are ready to go. Run the development server:
npm run dev
Visit http://localhost:3000. You should see a form. When you submit it, the data saves to the local SQLite file via Prisma, and the page updates immediately via Tailwind styling.
8. (Bonus) Useful Extension
For this stack, it is highly recommended to install clsx and tailwind-merge to handle dynamic classes cleanly.
npm install clsx tailwind-merge
Create lib/utils.ts:
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Now you can use <div className={cn("bg-red-500", isBlue && "bg-blue-500")}> in your components.