Feature modules and DAL ownership
What you need to know
A feature module owns a business capability, its data access and its operation contracts. Next.js routes compose those capabilities; they should not become the only place where business rules or access checks live.
By the end, you can: define module interfaces; separate UI and domain logic.
A feature-oriented file tree
app/learning/[slug]/page.tsxapp/api/progress/route.tsfeatures/progress/domain.tsfeatures/progress/dal.tsfeatures/progress/contract.tsfeatures/progress/progress-form.tsxExpected behavior: Both entry points can call the same service. Only route convention files create endpoints.
How it works
Imagine River Learning has a public catalog, enrollment and editorial features. Organize by what changes together: enrollment owns the rule that a member can update their own progress; catalog owns published course discovery. The App Router app directory maps requests to UI and HTTP handlers, while a feature directory can contain domain functions, server-only data access and UI. A data access layer (DAL) verifies the authenticated principal against the requested resource and returns a small data transfer object (DTO), not the raw row. A domain service owns invariants such as a score range, independent of whether the caller is a Route Handler or Server Action. A feature boundary is a design convention, not an automatic security boundary. Use import 'server-only' in modules that must not enter the client graph; it catches invalid imports during framework builds, but does not check whether a user is authorized. Keep Client Components small and pass only explicitly public or permitted values. A shared package should not silently import an app's database. Establish a dependency direction: route → feature service → DAL → database, and UI → public contract. Test allowed and denied cases against the same service.
Project a permitted row into a small DTO
type Row = { lessonId: string; memberId: string; tenantId: string; percent: number; auditNote: string };export function progressDTO(row: Row, actor: {memberId:string;tenantId:string}) { if (row.tenantId !== actor.tenantId || row.memberId !== actor.memberId) return null; return {lessonId: row.lessonId, percent: row.percent};}Expected behavior: The matching member gets lessonId and percent only. A different member or tenant gets null. The caller must obtain actor from a verified server session, not form input; SQL should also scope the lookup.
Common mistake
Putting every query in page.tsx duplicates checks across entry points. Conversely, placing a file in a server directory does not enforce authorization by itself.
Practice and verification
Draw the dependency graph for enrollment and catalog. Identify the owner of the progress invariant and the public DTO. Extend the capstone contract tests with a second member in the same tenant and a different tenant using the same lesson ID.
Scope: Proposed Next.js 16.x / React 19; App Router, Cache Components disabled unless noted. Framework and browser examples are staged, not executed. No exact patched dependency release or live deployment verified.