When scaling Project NEXAāa full-stack e-commerce platformāperformance and SEO were non-negotiable. Next.js App Router provided the perfect architecture, but unlocking its full potential required a deep understanding of React Server Components (RSC) and dynamic routing.
Here are the key best practices I implemented to handle high concurrency and maximize performance.
## 1. Default to Server Components
In the App Router, every component is a Server Component by default. Early on, it's tempting to slap "use client" at the top of every file so you can use useState. **Don't do this.**
Client components increase the JavaScript bundle size shipped to the browser, impacting TTI (Time to Interactive). I pushed all interactivity to the very edges of the component tree. For example, the Product Page itself is a Server Component that fetches data directly from the Supabase database. Only the "Add to Cart" button is a Client Component.
## 2. Secure Data Fetching
Because Server Components run on the server, you can securely access your database directly without building an intermediate API route.
`javascript
// This runs securely on the server
const { data: product } = await supabase
.from('products')
.select('*')
.eq('id', params.id)
.single();
`
This pattern not only improves security by keeping API keys entirely off the client but also significantly reduces latency since the server is usually physically closer to the database than the user's browser.
## 3. Streaming and Suspense
E-commerce pages have varying data dependencies. The product title and description load instantly, but related reviews might take longer to query.
Instead of blocking the entire page load until the reviews arrive, I utilized React Suspense boundaries.
`jsx
<Suspense fallback={<ReviewsSkeleton />}>
<ProductReviews productId={product.id} />
</Suspense>
`
This allows Next.js to stream the core HTML to the user instantly, and then seamlessly pop the reviews into the DOM once the database query resolves. The perceived performance improvement is massive.
## 4. Route Handlers for External Integrations
For external integrations like Stripe Webhooks, standard Server Components aren't enough. I utilized Next.js Route Handlers (app/api/webhook/route.js) to build robust, serverless endpoints that securely verify Stripe signatures and update the PostgreSQL database in real-time.
By strictly separating concernsāRSC for UI data fetching, Route Handlers for external API consumption, and Client Components for interactivityāProject NEXA became a highly scalable, enterprise-grade platform.
Aug 30, 2023ā¢7 min read
Next.js Best Practices
Next.jsPerformanceRSC