Next.js

Next.js App Router (TypeScript) Style Guide

General Principles


Folder and File Structure

layout.tsx
page.tsx
global.css
layout.tsx
page.tsx
proxy.ts # 7. Gatekeeper Layer (Middleware)
.env
.env.example
components.json # Shadcn UI Configuration
next.config.ts # Next.js Configuration
package.json
tsconfig.json
eslint.config.mjs
prettier.config.ts
README.md

Naming Conventions

  • Use kebab-case for React component files, utility, hooks, or helper files.
WhatHowGoodBad
React component filekebab-caselogin-form.tsxLogin-Form.tsx
Custom Hookskebab-case, use firstuse-config.tsuse-Config.tsx

Examples:

app/home/page.tsx
components/ui/button.tsx
components/login-form.tsx
hooks/use-fetch.ts
lib/format-date.ts

Imports

  • Organize imports from libraries to internal files:
// React or third-party libraries
import { useState } from 'react';
import { format } from 'date-fns';

// Internal components and helpers
import NavBar from '@/components/navbar';
import { formatDate } from '@/lib/format-date';

React Components

  • Use functional components
  • Use TypeScript for typing props and state.

Example:

interface LoginProps {
status?: string;
canResetPassword: boolean;

}

export default fanction LoginForm({status, canResetPassword}: LoginProps){

return (
  <div>LoginForm</div>

 )
}

TypeScript

  • Enable strict mode in tsconfig.json:
{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts",
    ".next/dev/types/**/*.ts",
    "**/*.mts"
  ],
  "exclude": ["node_modules"]
}
  • Avoid using any. Always use specific types.

Example:

// Correct
const fetchUser = async (id: number): Promise<User> => {
  // ...
};

// Incorrect
const fetchUser = async (id: number): Promise<any> => {
  // ...
};

Styling

  • Use CSS Modules or Tailwind CSS for modular styling.

Example:

import styles from './NavBar.module.css';

const NavBar: React.FC = () => {
  return <nav className={styles.nav}>NavBar</nav>;
};

export default NavBar;
  • Global styles are stored in globals.css and imported in app/layout.tsx.
import './globals.css';

export const metadata = {
  title: 'Next.js App',
  description: 'Generated by Next.js',
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

Testing

see: https://nextjs.org/docs/app/guides/testing

  • Use Jest and React Testing Library for unit testing.

Example:

import { render, screen } from '@testing-library/react';
import Button from '@/components/Button';

test('renders Button with label', () => {
  render(<Button label="Click Me" onClick={() => {}} />);
  expect(screen.getByText(/Click Me/i)).toBeInTheDocument();
});
  • Use Playwright or Cypress for end-to-end testing.

Linting and Formatting

  • Use ESLint with the following configuration in eslint.config.mjs:
import { defineConfig, globalIgnores } from "eslint/config";
import nextVitals from "eslint-config-next/core-web-vitals";
import nextTs from "eslint-config-next/typescript";

const eslintConfig = defineConfig([
  ...nextVitals,
  ...nextTs,
  // Override default ignores of eslint-config-next.
  globalIgnores([
    // Default ignores of eslint-config-next:
    ".next/**",
    "out/**",
    "build/**",
    "next-env.d.ts",
  ]),
]);

export default eslintConfig;
  • Use Prettier with the following configuration in .prettierrc optional:
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "printWidth": 80
}

Agent Skills

Terminal
  npx skills add https://github.com/ekovegeance/agent-skills --skill nextjs

Recommend Tools & Configuration

Last updated on

ekovegeance

On this page