Server data access
What you need to know
A Server Component can read data by calling a server-side function directly. The data access layer (DAL) is the code that centralizes those reads, validation and access decisions. River Books can render a book list from a local repository without first creating an HTTP endpoint. This removes an unnecessary network boundary, not the need for authorization.
By the end, you can: choose direct server-side DAL access; avoid unnecessary self-HTTP calls.
Read a local repository in app/catalog/page.tsx
import { listPublicBooks } from "../../lib/dal";export default async function Catalog() { const books = await listPublicBooks(); return <ul>{books.map(book => <li key={book.slug}>{book.title}</li>)}</ul>;}Expected behavior (not framework-tested): The V5 fixture supplies the DAL. Three public titles appear in the configured order. A draft record does not appear.
How it works
An async Server Component waits for a Promise before using its result. A repository owns retrieval; the DAL checks whether the caller can see the record and returns a small display object. The page chooses presentation. With a remote service, server fetch is appropriate, but use an approved fixed endpoint, inspect HTTP status, validate the response and set an explicit freshness policy. A Server Component does not need to fetch its own Route Handler: that adds HTTP serialization, an extra hop, and possibly a build-time failure because the app server is not listening. React cache can deduplicate calls within a server render request; it is not a shared persistent data cache.
Memoize a public read for a render (partial lib/dal.ts)
import "server-only";import { cache } from "react";import { readPublishedBook } from "./repository";export const getPublicBook = cache(async (slug: string) => { if (!/^[a-z0-9-]{1,60}$/.test(slug)) return null; const row = await readPublishedBook(slug); return row ? { slug: row.slug, title: row.title } : null;});Expected behavior (not framework-tested): Within a supported React render context, repeated calls with the same primitive slug can reuse the result. A different request must not be assumed to share the memoized result.
Common mistake
Do not put database clients or unrestricted rows in a client module. The server-only marker catches an invalid client import during the framework build; it does not automatically authorize callers. Keep access rules near reads and writes. Do not fetch a URL assembled from arbitrary user input.
Practice and verification
Trace page → DAL → repository → DAL projection → rendered list. For a second consumer, decide whether it needs an HTTP contract or merely a shared server helper. Predict what happens for an unpublished slug and confirm no draft title is returned.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Fetching · Data Security · React Cache · Fetch