Project Structure (App Router)
Tsx
src/
├── app/
│ ├── (auth)/
│ ├── api/
│ └── globals.css
├── components/
│ ├── ui/
│ ├── forms/
│ └── layout/
├── lib/
├── hooks/
├── types/
└── utils/
Database and Connection Pooling
Tsx
import { Pool } from 'pg';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
export default pool;
Caching with fetch and route segment config
Tsx
export async function GET() {
const users = await fetch('https://api.example.com/users', {
next: { revalidate: 3600 }
});
return Response.json(users);
}
Images and Code Splitting
Tsx
import Image from 'next/image';
<Image
src='/hero-image.jpg'
alt='Hero image'
width={800}
height={600}
priority
placeholder='blur'
blurDataURL='data:image/jpeg;base64,...'
/>
Tsx
const HeavyComponent = dynamic(() => import('./HeavyComponent'), {
loading: () => <p>Loading...</p>,
ssr: false
});
Error Boundaries
Tsx
export class ErrorBoundary extends Component<Props, State> {
static getDerivedStateFromError(): State {
return { hasError: true };
}
componentDidCatch(error: Error, errorInfo: any) {
console.error('Error caught by boundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}