-
Notifications
You must be signed in to change notification settings - Fork 917
coderabbit test #7165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
coderabbit test #7165
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| chat: | ||
| art: false | ||
| finishing_touches: | ||
| docstrings: | ||
| enabled: false | ||
| unit_tests: | ||
| enabled: false | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Image | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| src="/profile.png" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| alt="Profile" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width={500} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height={500} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unoptimized={true} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 2. Data Fetching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function getServerSideProps() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const res = fetch('https://api.example.com/data') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data = await res.json() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| props: { data }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix missing await and avoid non‑serializable props
-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 => <div>{n}</div>)
- },
- }
+export async function getServerSideProps() {
+ const res = await fetch('https://api.example.com/data')
+ if (!res.ok) {
+ return { notFound: true }
+ }
+ const data = await res.json()
+ const processed = data.map((item: { name: string }) => item.name)
+ return {
+ props: {
+ data: processed
+ // Render lists in the component, not here
+ },
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When reviewing ✅ Flag These Issues (ONLY)
❌ DO NOT Flag These (Even If "Bad Practice")Code snippets in
Rationale: Documentation often shows "before" examples or common mistakes intentionally. The code snippets reflect real-world code that the guide is addressing. Title Casing RulesAll headings and titles in ✅ Correct Format
❌ Incorrect Format
Product Name ExceptionsAlways preserve the exact casing for these product names:
SummaryFor .mdx files:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aidankmcalister, thank you for the detailed guidance! I've recorded these rules for reviewing For clarity, based on your guidelines:
I'll apply these rules going forward: for ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 3. Dynamic Imports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const DynamicComponent = dynamic(() => import('../components/HeavyComponent')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function Home() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <DynamicComponent /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+50
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major React anti‑patterns: local var state, direct DOM writes, missing keys Use React state/effects, avoid document.getElementById in render, provide keys in lists. -function Home() {
- var count = 0;
-
- const items = ['a', 'b', 'c'];
-
- document.getElementById('counter').innerText = count;
-
- const unused = 'this is never used';
-
- return (
- <div>
- {items.map(item => <div>{item}</div>)}
- <button onClick={() => count++}>Increment</button>
- <DynamicComponent />
- </div>
- )
-}
+import { useEffect, useRef, useState } from 'react'
+
+function Home() {
+ const [count, setCount] = useState(0)
+ const items = ['a', 'b', 'c']
+ const counterRef = useRef<HTMLDivElement | null>(null)
+
+ useEffect(() => {
+ if (counterRef.current) counterRef.current.innerText = String(count)
+ }, [count])
+
+ return (
+ <div>
+ <div id="counter" ref={counterRef} />
+ {items.map(item => <div key={item}>{item}</div>)}
+ <button onClick={() => setCount(c => c + 1)}>Increment</button>
+ <DynamicComponent />
+ </div>
+ )
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When reviewing ✅ Flag These Issues (ONLY)
❌ DO NOT Flag These (Even If "Bad Practice")Code snippets in
Rationale: Documentation often shows "before" examples or common mistakes intentionally. The code snippets reflect real-world code that the guide is addressing. Title Casing RulesAll headings and titles in ✅ Correct Format
❌ Incorrect Format
Product Name ExceptionsAlways preserve the exact casing for these product names:
SummaryFor .mdx files:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aidankmcalister, thank you for the clear guidance! I understand now that I apologize for the previous comment on this PR that flagged React anti-patterns—I should not have suggested those changes since they fall under the "DO NOT flag" category for I've recorded these guidelines for future reviews. ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 4. API Routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function handler(req, res) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { id } = req.query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (req.method === 'POST') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return res.status(200).json({ message: 'Success' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res.status(200).json({ id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 5. Environment Variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // .env.local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NEXT_PUBLIC_API_URL=https://api.example.com | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| API_SECRET_KEY=supersecret | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+85
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid showing real‑looking secrets; use placeholders and guidance Don’t encourage committing secrets or exposing non‑public values in docs. -```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
+```dotenv
+# Public values (safe to expose to browser)
+NEXT_PUBLIC_API_URL=https://api.example.com
+
+# Secrets (never commit; store only in .env.local / your secret manager)
+API_SECRET_KEY=YOUR_SECRET_KEY_HERE
+DATABASE_URL=postgres://USER:PASSWORD@HOST:5432/DB
+# Avoid including cloud keys in examples; if needed, use placeholders:
+AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
+AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When reviewing ✅ Flag These Issues (ONLY)
❌ DO NOT Flag These (Even If "Bad Practice")Code snippets in
Rationale: Documentation often shows "before" examples or common mistakes intentionally. The code snippets reflect real-world code that the guide is addressing. Title Casing RulesAll headings and titles in ✅ Correct Format
❌ Incorrect Format
Product Name ExceptionsAlways preserve the exact casing for these product names:
SummaryFor .mdx files:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aidankmcalister, thank you for the detailed guidance! I understand and will apply these rules when reviewing ✅ I will ONLY flag:
❌ I will NOT flag:
📝 Title casing:
I've noted that my previous comment about the environment variables actually aligns with your first flaggable issue (exposed secrets), so that was appropriate per your guidelines. The AWS keys and database credentials appeared to be real-looking values that should be replaced with placeholders, which matches your "Action" for exposed secrets. I've recorded these guidelines for future reviews of ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 6. Middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { NextResponse } from 'next/server' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { NextRequest } from 'next/server' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function middleware(request: NextRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const token = request.cookies.get('token') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!token) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.redirect(new URL('/login', request.url)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.next() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+101
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cookie security flags and token read are unsafe
export function middleware(request: NextRequest) {
- const token = request.cookies.get('token')
-
- if (token !== 'my-secure-token') {
+ const token = request.cookies.get('token')?.value
+ if (!token) {
return NextResponse.redirect(new URL('/login', request.url))
}
const response = NextResponse.next()
- response.cookies.set('session', '123', {
- httpOnly: false,
- secure: false,
- sameSite: 'lax',
- })
+ response.cookies.set('session', '123', {
+ httpOnly: true,
+ secure: true,
+ sameSite: 'lax',
+ path: '/',
+ })
return response
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 7. Error Handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function ErrorBoundary({ error, reset }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <h2>Something went wrong!</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button onClick={() => reset()}>Try again</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function Page() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <ErrorBoundary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <MyComponent /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </ErrorBoundary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 8. Performance Monitoring | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { reportWebVitals } from 'next/app' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function reportWebVitals(metric) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(metric) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
aidankmcalister marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+168
to
+176
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Avoid new PrismaClient per request; add error handling Reuse PrismaClient (cache in dev), handle methods and errors. -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)
-}
+import { PrismaClient } from '@prisma/client'
+declare global { var prisma: PrismaClient | undefined }
+const prisma = global.prisma ?? new PrismaClient()
+if (process.env.NODE_ENV !== 'production') global.prisma = prisma
+
+export default async function handler(req, res) {
+ if (req.method !== 'GET') return res.status(405).end()
+ try {
+ const users = await prisma.user.findMany()
+ return res.status(200).json(users)
+ } catch (e) {
+ console.error(e)
+ return res.status(500).json({ error: 'Internal Server Error' })
+ }
+}🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved Comment Policy DO NOT re-suggest the same change in future reviews or commits Examples: If you suggest adding error handling and it's resolved without changes → Don't suggest it again Exception: Only re-suggest if the context has changed significantly (e.g., the same file is completely rewritten, or the code moves to a different type of file).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aidankmcalister, understood! I've noted this Resolved Comment Policy for future reviews. I will respect resolved comments as final decisions and will not re-suggest the same changes in subsequent reviews or commits, even if the code remains unchanged. This applies especially to .mdx documentation files where resolved suggestions indicate intentional choices by the maintainers. The only exception will be if the context changes significantly (e.g., complete file rewrites or code moving to a different file type). ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.