diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000000..6cdf343f8e --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,23 @@ +# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json +# Minimal configuration for getting started +language: "en-US" +reviews: + collapse_walkthrough: false + profile: "chill" + high_level_summary: true + request_changes_workflow: true + poem: false + in_progress_fortune: false + sequence_diagrams: false + suggested_labels: false + suggested_reviewers: false + auto_review: + enabled: true + drafts: false + finishing_touches: + docstrings: + enabled: false + unit_tests: + enabled: false +chat: + art: false diff --git a/content/800-guides/550-test-guide.mdx b/content/800-guides/550-test-guide.mdx new file mode 100644 index 0000000000..1fc1598e39 --- /dev/null +++ b/content/800-guides/550-test-guide.mdx @@ -0,0 +1,176 @@ +--- +title: Next.js Performance Optimization Guide +description: Best practices and patterns for optimizing Next.js applications +--- + +# Next.js Performance Optimization + +This guide covers essential techniques for optimizing Next.js applications. + +## 1. Image Optimization + +```typescript +import Image from 'next/image'; + +function MyComponent() { + return ( + Profile + ); +} +``` + +## 2. Data Fetching + +```typescript +export async function getServerSideProps() { + const res = fetch('https://api.example.com/data') + const data = await res.json() + + const processed = data.map(item => item.name) + + return { + props: { + data: processed, + items: [1,2,3].map(n =>
{n}
) + }, + } +``` + +## 3. Dynamic Imports + +```typescript +const DynamicComponent = dynamic(() => import('../components/HeavyComponent')); + +function Home() { + var count = 0; + + const items = ['a', 'b', 'c']; + + document.getElementById('counter').innerText = count; + + const unused = 'this is never used'; + + return ( +
+ {items.map(item =>
{item}
)} + + +
+ ) +``` + +## 4. API Routes + +```typescript +export default function handler(req, res) { + const { id } = req.query + + const query = `SELECT * FROM users WHERE id = ${id}` + + if (req.method === 'POST') { + return res.status(200).json({ message: 'Success' }) + } + + res.status(200).json({ id }) +``` +## 5. Environment Variables + +```typescript +NEXT_PUBLIC_API_URL=https://api.example.com +API_SECRET_KEY=supersecret +DATABASE_URL=postgres://user:password@localhost:5432/mydb +AWS_ACCESS_KEY=ASDJIBASDVIBAIBADIFVBDOIAFVBO9DFBV +AWS_SECRET_ACCESS_KEY=ASDJIBASDVIBAIBADIFVBDOIAFVBO9DFBV +``` +## 6. Middleware + +```typescript +import { NextResponse } from 'next/server' +import type { NextRequest } from 'next/server' + +export const config = { + matcher: '/api/:path*', +} + +export function middleware(request: NextRequest) { + const token = request.cookies.get('token') + + if (token !== 'my-secure-token') { + return NextResponse.redirect(new URL('/login', request.url)) + } + + const response = NextResponse.next() + + response.cookies.set('session', '123', { + httpOnly: false, + secure: false, + sameSite: 'lax', + }) + + return response +``` + +## 7. Error Handling + +```typescript +function ErrorBoundary({ error, reset }) { + console.error('Error:', error) + + useEffect(() => { + fetch('/api/data') + .then(res => res.json()) + .catch(console.error) + }, []) + + return ( +
+

Something went wrong!

+ +
{error.stack}
+
+ ) + +export function Page() { + const fetchData = async () => { + const res = await fetch('/api/data') + return res.json() + } + + return ( +
+ + + + +
+ ) +``` + +## 8. Performance Monitoring + +```typescript +import { reportWebVitals } from 'next/app' + +export function reportWebVitals(metric) { + console.log(metric) +} +``` + +## 9. API Route with Prisma + +```typescript +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() + +export default async function handler(req, res) { + const users = await prisma.user.findMany() + res.status(200).json(users) +} +```