/Next-js-Boilerplate

Primary LanguageTypeScriptMIT LicenseMIT

Boilerplate and Starter for Next.js 14+, Tailwind CSS 3.4, and TypeScript.

Next js starter banner

πŸš€ Boilerplate and Starter for Next.js with App Router support, Tailwind CSS, and TypeScript ⚑️ Prioritizing developer experience first: Next.js, TypeScript, ESLint, Prettier, Husky, Lint-Staged, Jest, Testing Library, Commitlint, VSCode, PostCSS, Tailwind CSS, Authentication with Clerk, Database with DrizzleORM (SQLite, PostgreSQL, and MySQL) and Turso, Error Monitoring with Sentry, Logging with Pino.js and Log Management with Better Stack, Monitoring as Code with Checkly, Storybook, Multi-language (i18n), One-Click Deploy on Netlify, and more.

Clone this project and use it to create your own Next.js project. You can check a Next js templates demo.

Sponsors

Clerk – Authentication & User Management for Next.js SQLite Developer Experience Crowdin
Sentry Codecov PostHog Better Stack
Upstash Checkly React SaaS Boilerplate Next.js
Add your logo here

Features

Developer experience first, extremely flexible code structure and only keep what you need:

  • ⚑ Next.js with App Router support
  • πŸ”₯ Type checking TypeScript
  • πŸ’Ž Integrate with Tailwind CSS
  • βœ… Strict Mode for TypeScript and React 18
  • πŸ”’ Authentication with Clerk: Sign up, Sign in, Sign out, Forgot password, Reset password, and more.
  • πŸ“¦ Type-safe ORM with DrizzleORM, compatible with SQLite, PostgreSQL, and MySQL
  • πŸ’½ Global Database with Turso
  • 🌐 Multi-language (i18n) with next-intl and Crowdin
  • ♻️ Type-safe environment variables with T3 Env
  • ⌨️ Form handling with React Hook Form
  • πŸ”΄ Validation library with Zod
  • πŸ“ Linter with ESLint (default Next.js, Next.js Core Web Vitals, Tailwind CSS and Airbnb configuration)
  • πŸ’– Code Formatter with Prettier
  • 🦊 Husky for Git Hooks
  • 🚫 Lint-staged for running linters on Git staged files
  • πŸš“ Lint git commit with Commitlint
  • πŸ““ Write standard compliant commit messages with Commitizen
  • 🦺 Unit Testing with Jest and React Testing Library
  • πŸ§ͺ Integration and E2E Testing with Playwright
  • πŸ‘· Run tests on pull request with GitHub Actions
  • πŸŽ‰ Storybook for UI development
  • 🚨 Error Monitoring with Sentry
  • β˜‚οΈ Code coverage with Codecov
  • πŸ“ Logging with Pino.js and Log Management with Better Stack
  • πŸ–₯️ Monitoring as Code with Checkly
  • 🎁 Automatic changelog generation with Semantic Release
  • πŸ” Visual testing with Percy (Optional)
  • πŸ’‘ Absolute Imports using @ prefix
  • πŸ—‚ VSCode configuration: Debug, Settings, Tasks and Extensions
  • πŸ€– SEO metadata, JSON-LD and Open Graph tags
  • πŸ—ΊοΈ Sitemap.xml and robots.txt with next-sitemap
  • ⌘ Database exploration with Drizzle Studio and CLI migration tool with Drizzle Kit
  • βš™οΈ Bundler Analyzer
  • πŸ–±οΈ One click deployment with Netlify (or manual deployment to any hosting services)
  • 🌈 Include a FREE minimalist theme
  • πŸ’― Maximize lighthouse score

Built-in feature from Next.js:

  • β˜• Minify HTML & CSS
  • πŸ’¨ Live reload
  • βœ… Cache busting

Philosophy

  • Nothing is hidden from you, so you have the freedom to make the necessary adjustments to fit your needs and preferences.
  • Easy to customize
  • Minimal code
  • SEO-friendly
  • πŸš€ Production-ready

Requirements

  • Node.js 18+ and npm

Getting started

Run the following command on your local environment:

git clone --depth=1 https://github.com/ixartz/Next-js-Boilerplate.git my-project-name
cd my-project-name
npm install

Then, you can run the project locally in development mode with live reload by executing:

npm run dev

Open http://localhost:3000 with your favorite browser to see your project.

Set up authentication

Create a Clerk account at Clerk.com and create a new application in Clerk Dashboard. Then, copy NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY into .env.local file (not tracked by Git):

NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_clerk_pub_key
CLERK_SECRET_KEY=your_clerk_secret_key

Now, you have a fully working authentication system with Next.js: Sign up, Sign in, Sign out, Forgot password, Reset password, Update profile, Update password, Update email, Delete account, and more.

Set up remote database

The project uses DrizzleORM, a type-safe ORM compatible with SQLite, PostgreSQL, and MySQL databases. By default, the project is set up to work seamlessly with libSQL, and for production purposes, it's integrated with Turso. The Next.js Boilerplate also enables a smooth transition to an alternative database provider if your project requires it.

First, you need to create a Turso account at Turso.tech and install the Turso CLI:

brew install tursodatabase/tap/turso
turso auth signup # Sign up to Turso

Then, create a new database:

turso db create nextjs-boilerplate

Now, you need to update the DATABASE_URL in .env file with the database URL provided by Turso:

turso db show nextjs-boilerplate --url

# .env
# DATABASE_URL=libsql://[RANDOM-CHARS]-[DB-NAME]-[ORG-NAME].turso.io

Finally, you also need to create a new environement variable DATABASE_AUTH_TOKEN in .env.local (not tracked by Git) with the auth token provided by Turso:

turso db tokens create nextjs-boilerplate

# .env.local
# DATABASE_AUTH_TOKEN=[your-auth-token]

Translation (i18n) setup

For translation, the project uses next-intl combined with Crowdin. As a developer, you only need to take care of the English (or another default language) version. Other languages are automatically generated and handled by Crowdin. You can use Crowdin to collaborate with your translation team or translate the messages yourself with the help of machine translation.

To set up translation (i18n), create an account at Crowdin.com and create a new project. In the newly created project, you will able to find the project ID. You'll also require to create a new Personal Access Tokens by going to Account Settings > API. Then, in your GitHub Actions, you need to define the following environment variables CROWDIN_PROJECT_ID and CROWDIN_PERSONAL_TOKEN.

After defining the environment variables in your GitHub Actions, your localization files will be synchronized with Crowdin everytime you push a new commit to the main branch.

Project structure

.
β”œβ”€β”€ README.md                       # README file
β”œβ”€β”€ .github                         # GitHub folder
β”œβ”€β”€ .husky                          # Husky configuration
β”œβ”€β”€ .storybook                      # Storybook folder
β”œβ”€β”€ .vscode                         # VSCode configuration
β”œβ”€β”€ migrations                      # Database migrations
β”œβ”€β”€ public                          # Public assets folder
β”œβ”€β”€ scripts                         # Scripts folder
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ app                         # Next JS App (App Router)
β”‚   β”œβ”€β”€ components                  # React components
β”‚   β”œβ”€β”€ libs                        # 3rd party libraries configuration
β”‚   β”œβ”€β”€ locales                     # Locales folder (i18n messages)
β”‚   β”œβ”€β”€ models                      # Database models
β”‚   β”œβ”€β”€ styles                      # Styles folder
β”‚   β”œβ”€β”€ templates                   # Templates folder
β”‚   β”œβ”€β”€ types                       # Type definitions
β”‚   β”œβ”€β”€ utils                       # Utilities folder
β”‚   └── validations                 # Validation schemas
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ e2e                         # E2E tests, also includes Monitoring as Code
β”‚   └── integration                 # Integration tests
β”œβ”€β”€ tailwind.config.js              # Tailwind CSS configuration
└── tsconfig.json                   # TypeScript configuration

Customization

You can easily configure Next js Boilerplate by making a search in the whole project with FIXME: for making quick customization. Here is some of the most important files to customize:

  • public/apple-touch-icon.png, public/favicon.ico, public/favicon-16x16.png and public/favicon-32x32.png: your website favicon, you can generate from https://favicon.io/favicon-converter/
  • src/utils/AppConfig.ts: configuration file
  • src/templates/BaseTemplate.tsx: default theme
  • next.config.mjs: Next.js configuration
  • next-sitemap.config.js: sitemap configuration
  • .env: default environment variables

You have access to the whole code source if you need further customization. The provided code is only example for you to start your project. The sky is the limit πŸš€.

Commit Message Format

The project enforces Conventional Commits specification. This means that all your commit messages must be formatted according to the specification. To help you write commit messages, the project uses Commitizen, an interactive CLI that guides you through the commit process. To use it, run the following command:

npm run commit

One of the benefits of using Conventional Commits is that it allows us to automatically generate a CHANGELOG file. It also allows us to automatically determine the next version number based on the types of commits that are included in a release.

Testing

All unit tests are located with the source code inside the same directory. So, it makes it easier to find them. The project uses Jest and React Testing Library for unit testing. You can run the tests with:

npm run test

Integration & E2E Testing

The project uses Playwright for Integration and E2E testing. You can run the tests with:

npx playwright install # Only for the first time in a new environment
npm run test:e2e

Enable Edge runtime (optional)

The App Router folder is compatible with the Edge runtime. You can enable it by uncommenting the following lines src/app/layouts.tsx:

// export const runtime = 'edge';

For your information, the database migration is not compatible with the Edge runtime. So, you need to disable the automatic migration in src/libs/DB.ts:

if (process.env.NODE_ENV !== 'production') {
  await migrate(db, { migrationsFolder: './migrations' });
}

After disabling it, you are required to run the migration manually with:

npm run db:migrate

You also require to run the command each time you want to update the database schema.

Deploy to production

During the build process, the database migration is automatically executed. So, you don't need to run the migration manually. But, in your environment variable, DATABASE_URL and DATABASE_AUTH_TOKEN need to be defined.

Then, you can generate a production build with:

$ npm run build

It generates an optimized production build of the boilerplate. For testing the generated build, you can run:

$ npm run start

You also need to defined the environment variables CLERK_SECRET_KEY using your own key.

The command starts a local server with the production build. Then, you can now open http://localhost:3000 with your favorite browser to see the project.

Deploy to Netlify

Clone this repository on own GitHub account and deploy to Netlify in one click:

Netlify Deploy button

During the setup, you need to define the DATABASE_URL and DATABASE_AUTH_TOKEN environment variables.

Error Monitoring

The project uses Sentry to monitor errors. For development environment, you don't need to do anything: Next.js Boilerplate is already configured to use Sentry and Spotlight (Sentry for Development). All errors will be automatically sent to your local Spotlight instance. So, you can try the Sentry experience locally.

For production environment, you need to create a Sentry account and create a new project. Then, in next.config.mjs, you need to update the org and project attribute in withSentryConfig function. You also need to add your Sentry DSN in sentry.client.config.ts, sentry.edge.config.ts and sentry.server.config.ts.

Code coverage

Next.js Boilerplate relies on Codecov for code coverage reporting solution. To use Codecov, create a Codecov account and connect it to your GitHub account. On your Codecov dashboard, it should display a list of your repositories. Select the repository you want to enable Codecov for and copy the token. Then, in your GitHub Actions, you need to define the CODECOV_TOKEN environment variable and paste the token you copied.

Be sure to create the CODECOV_TOKEN as a Github Actions secret, do not paste it directly into your source code.

Logging

The project uses Pino.js for logging. By default, for development environment, the logs are displayed in the console.

For production environment, the project is already integrated with Better Stack to manage and query your logs using SQL. To use Better Stack, you need to create a Better Stack account and create a new source: go to your Better Stack Logs Dashboard > Sources > Connect source. Then, you need to give a name to your source and select Node.js as the platform.

After creating the source, you able to see your source token and copy it. Then, in your environment variabless, you can paste the token in LOGTAIL_SOURCE_TOKEN variable. Now, all your logs will be automatically sent and ingested by Better Stack.

Checkly monitoring

The project uses Checkly to ensure that your production environment is always up and running. At regular intervals, Checkly runs the tests ending with *.check.spec.ts extension and notifies you if any of the tests fail. Additionally, you have the flexibility to execute tests across multiple locations to ensure that your application is available worldwide.

To use Checkly, you must first create an account on their website. Once you have an account, you can set the CHECKLY_API_KEY environment variable in GitHub Actions by generating a new API key in the Checkly Dashboard. Additionally, you will need to define the CHECKLY_ACCOUNT_ID, which can also be found in your Checkly Dashboard under User Settings > General.

To complete the setup, make sure to update the checkly.config.ts file with your own email address and production URL.

Useful commands

Bundle Analyzer

Next.js Boilerplate comes with a built-in bundle analyzer. It can be used to analyze the size of your JavaScript bundles. To begin, run the following command:

npm run build-stats

By running the command, it'll automatically open a new browser window with the results.

Database Studio

The project is already configured with Drizzle Studio to explore the database. You can run the following command to open the database studio:

npm run db:studio

Then, you can open https://local.drizzle.studio with your favorite browser to explore your database.

Known warnings

webpack.cache.PackFileCacheStrategy

Warning: webpack.cache.PackFileCacheStrategy Serializing big strings (104kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)

This warning is caused by using Clerk and next-intl middlewares. It only happens when both middlewares are used together.

VSCode information (optional)

If you are VSCode users, you can have a better integration with VSCode by installing the suggested extension in .vscode/extension.json. The starter code comes up with Settings for a seamless integration with VSCode. The Debug configuration is also provided for frontend and backend debugging experience.

With the plugins installed on your VSCode, ESLint and Prettier can automatically fix the code and show you the errors. Same goes for testing, you can install VSCode Jest extension to automatically run your tests and it also show the code coverage in context.

Pro tips: if you need a project wide type checking with TypeScript, you can run a build with Cmd + Shift + B on Mac.

Contributions

Everyone is welcome to contribute to this project. Feel free to open an issue if you have question or found a bug. Totally open to any suggestions and improvements.

License

Licensed under the MIT License, Copyright Β© 2024

See LICENSE for more information.

Sponsors

Clerk – Authentication & User Management for Next.js SQLite Developer Experience Crowdin
Sentry Codecov PostHog Better Stack
Upstash Checkly React SaaS Boilerplate Next.js
Add your logo here

Made with β™₯ by CreativeDesignsGuru Twitter

Sponsor Next JS Boilerplate