Slimmer API Responses
Send only the data your UI actually needs.
TL;DR
Select only needed fields, paginate results, compress responses. Every byte costs money in bandwidth. A 10x smaller response = 10x less transfer cost.
Field Selection
Don't send data the client doesn't need:
// Returns everything
const articles = await db.articles.findMany({
include: { author: true, revisions: true }
});Response: 50KB with revision history, internal notes, etc.
// Returns only what list view needs
const articles = await db.articles.findMany({
select: {
id: true,
title: true,
slug: true,
excerpt: true,
author: { select: { name: true, avatar: true } }
}
});Response: 8KB - same data the UI uses.
Pagination
Don't return everything at once:
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const cursor = searchParams.get('cursor');
const limit = 20;
const articles = await db.articles.findMany({
take: limit + 1,
cursor: cursor ? { id: cursor } : undefined,
orderBy: { publishedAt: 'desc' },
select: { id: true, title: true, slug: true, excerpt: true },
});
const hasMore = articles.length > limit;
const items = hasMore ? articles.slice(0, -1) : articles;
return Response.json({
items,
nextCursor: hasMore ? items.at(-1)?.id : null,
});
}Normalize Repeated Data
{
"articles": [
{ "id": "a1", "author": { "id": "u1", "name": "Jane", "bio": "..." } },
{ "id": "a2", "author": { "id": "u1", "name": "Jane", "bio": "..." } },
{ "id": "a3", "author": { "id": "u1", "name": "Jane", "bio": "..." } }
]
}Author data sent 3 times.
{
"articles": [
{ "id": "a1", "authorId": "u1" },
{ "id": "a2", "authorId": "u1" },
{ "id": "a3", "authorId": "u1" }
],
"authors": {
"u1": { "id": "u1", "name": "Jane", "bio": "..." }
}
}Author data sent once.
For 100 articles by 10 authors, this cuts response size by 60%+.
Key Takeaways
Use select for field filtering
Return only the fields your UI needs, not entire database records.
Paginate all lists and feeds Never return unbounded arrays. Set sensible defaults and max limits.
Normalize repeated data Extract repeated objects into a lookup table to avoid duplication.
Audit for internal field leaks Ensure passwords, internal IDs, and debug data never reach the client.
With bandwidth optimized, see Function Efficiency for server-side savings.