Boundary Performance, Security & Debugging
Server/Client Component architecture is not complete when the code compiles.
A production boundary should also be reviewed for:
- browser JavaScript cost
- hydration work
- server latency
- payload size
- sensitive data exposure
- authorization correctness
- dependency placement
- deterministic rendering
- failure diagnosis
Performance has multiple budgetsβ
A Server Component can reduce client JavaScript while increasing server work.
A Client Component can improve interaction ownership while increasing browser cost.
Measure both sides:
SERVER
βββ render duration
βββ data latency
βββ RSC payload size
βββ cache effectiveness
BROWSER
βββ JS bytes
βββ parse/execute cost
βββ hydration work
βββ interaction latency
βββ route transition cost
Do not optimise one column while ignoring the other.
Client boundary cost modelβ
When you add 'use client', ask what enters the client graph:
interactive component
+
its imports
+
third-party libraries
+
client helpers
+
provider dependencies
The cost is not the line containing the directive. The cost is the graph reachable from that boundary.
Measure before moving everything server-sideβ
Suppose a dashboard feels slow.
Possible bottlenecks:
slow database
large RSC payload
large chart library
broad hydration
expensive client render
sequential data fetches
no useful loading boundary
βUse more Server Componentsβ is not a diagnosis.
Find the stage first.
Narrow boundaries around real interactionβ
Bad:
'use client' Dashboard
βββ server-readable title
βββ navigation
βββ static cards
βββ table
βββ filters
βββ one dropdown
Better:
Server Dashboard
βββ Server title
βββ Server navigation
βββ Server cards
βββ Server table
βββ Client Filters
βββ Client Dropdown
But do not fragment every button into its own artificial boundary if one cohesive interactive widget owns the state.
Hydration scopeβ
Hydration work belongs to Client Components.
Large client trees can increase:
- JavaScript download
- parse/execute work
- hydration duration
- main-thread contention
Server-rendered markup alone is not free either, but it does not require the Server Component implementation to execute in the browser.
Hydration mismatch taxonomyβ
Time/randomnessβ
'use client'
export function Clock() {
return <span>{Date.now()}</span>
}
Server prerender and browser render may disagree.
Browser API in initial renderβ
return <span>{window.innerWidth}</span>
Unsafe for server prerender.
Locale/environment differenceβ
Server locale/time zone differs from browser.
Invalid HTMLβ
Browser reparses markup differently from React's expected tree.
External DOM mutationβ
Browser extension or third-party script changes markup before hydration.
Debug the source of divergence rather than suppressing the warning globally.
Safe browser-only initializationβ
Use stable initial markup:
'use client'
import { useEffect, useState } from 'react'
export function ClientPreference() {
const [value, setValue] = useState<string | null>(null)
useEffect(() => {
setValue(localStorage.getItem('preference'))
}, [])
return <span>{value ?? 'Loading preferenceβ¦'}</span>
}
Or choose server-readable state when the value affects initial rendering materially.
Security boundary reviewβ
There are at least four different trust questions:
1. Can this module execute in the browser?
2. What data is sent to the browser?
3. What user input reaches the server?
4. Who is authorized to perform the operation?
Do not collapse them into one concept.
server-only helps question 1.
DTO minimisation helps question 2.
validation helps question 3.
authorization helps question 4.
Secrets and serialized propsβ
A server module may safely read:
process.env.PRIVATE_KEY
but if it returns an object containing sensitive provider data and that object is passed to a Client Component, the exposure problem has moved rather than disappeared.
Audit both imports and outputs.
Client props are untrusted on later requestsβ
Suppose the server renders:
<DeleteProjectButton projectId="p_42" canDelete={true} />
The browser can modify requests, replay operations, or call server endpoints independently of the rendered UI.
canDelete is presentation state.
The server mutation must verify:
current user
project scope
permission
request validity
again.
Boundary observabilityβ
Useful production signals:
Serverβ
- route render timing
- database timing
- service calls
- authorization failures
- RSC/server errors
Browserβ
- hydration errors
- JS exceptions
- interaction latency
- route transition timing
- chunk loading failures
Correlate both sides where possible.
A user-visible failure may begin on the server but surface during client navigation.
Debugging window is not definedβ
Checklist:
- Find the import path to the browser-only code.
- Determine whether failure happens at module evaluation or render.
- Put browser dependency behind
'use client'. - If initial prerender still fails, move browser access into an effect.
- If the package is fundamentally browser-only, consider client-only dynamic loading.
- Avoid disabling SSR for unrelated parent UI.
Debugging hook errorsβ
Symptom:
useState/useEffect can only be used in a Client Component
Ask:
- Does this component truly need client capability?
- Is the hook actually needed?
- Can only a smaller child become client-side?
- Is a third-party package missing its client directive?
Do not automatically put 'use client' at the page root.
Debugging non-serializable propsβ
Find the server-to-client edge and inspect:
functions
class instances
library handles
request/response objects
large database models
opaque SDK objects
Convert to a minimal transport-friendly contract.
Debugging unexpected client bundle growthβ
Symptoms:
- route JS increases sharply
- hydration slower after innocuous refactor
- a server library appears in bundle analysis
Check:
- Was
'use client'moved upward? - Did an interactive component import a large barrel file?
- Did a global provider gain a heavy dependency?
- Was a lazy-loaded feature imported eagerly?
- Did a component library mark too broad an entry point client-side?
Debugging server work on client navigationβ
Remember App Router navigation can request a new RSC payload.
A route transition may feel slow because of:
server fetch latency
uncached server work
network latency
large payload
client rendering
Use browser Network timing plus server logs.
Do not assume slow navigation means βReact rendering is slow.β
Production build as boundary validationβ
Run:
npm run build
A production build can expose:
- invalid server/client imports
- bad static assumptions
- non-supported environment usage
- route rendering failures
- malformed document/sidebars in this handbook project
Treat build failures as architectural feedback.
Bundle review workflowβ
For a meaningful client-boundary change:
baseline
β
measure route/client JS
β
move boundary or dependency
β
rebuild
β
measure again
Avoid performance claims based solely on source-code appearance.
Security review workflowβ
For each Client Component receiving server data:
- list every prop
- classify sensitivity
- remove fields the browser does not need
- validate that permissions are enforced elsewhere
- inspect programmatic destinations/IDs as untrusted inputs
- verify secrets never appear in client config
Senior review scenarioβ
Scenario: A team marks the root layout 'use client' because it needs theme context, auth context, analytics, and a modal provider.
A stronger design review asks:
- Which providers truly need global scope?
- Can the root layout remain server-side?
- Can providers be wrapped in a small client entry component?
- Can dashboard-specific providers move under
/dashboard? - Does auth context duplicate authoritative server session logic?
- What client dependencies are now global?
- How much route JS/hydration did the change add?
The goal is not to eliminate providers. It is to place them according to ownership.
Senior review scenario: secret-bearing data helperβ
Scenario: A shared api.ts module reads process.env.INTERNAL_TOKEN, exports public formatters, and is imported from both server and client code.
Fix the architecture:
shared/formatting.ts
server/internal-api.ts + server-only
client/browser-api.ts if needed
Do not keep a mixed-environment barrel merely for import convenience.
Review checklistβ
Boundary necessityβ
- Every
'use client'entry has a browser/interactivity reason. - Parent server rendering is preserved where useful.
- provider scope is intentional.
Performanceβ
- client JS measured before/after major boundary changes.
- heavy client libraries lazy-load where justified.
- server latency is measured separately.
- hydration cost is not confused with server render cost.
Securityβ
- secret-bearing modules are server-only.
- client props contain only required fields.
- client visibility is not authorization.
- server operations revalidate identity and permission.
Reliabilityβ
- initial full load works.
- client navigation works.
- hydration is deterministic.
- browser-only dependencies are isolated.
- production build passes.
Interview questionsβ
What is the performance cost of 'use client'?
The reachable client module graph may add browser JavaScript, parse/execute work, and hydration. Measure the graph, not just the component file.
Can Server Components still be slow?
Yes. Server I/O, computation, caching strategy, and network delivery can dominate route latency.
Why is prop minimisation a security measure?
Values sent across the server/client boundary should be considered browser-visible.
What is the first thing to do with a hydration mismatch?
Identify why the server-produced markup and browser's initial React render differ. Do not hide the warning first.
How do you diagnose slow App Router navigation?
Decompose server response time, RSC payload/network, client bundle/chunks, hydration/rendering, and loading feedback rather than blaming one layer.
Exerciseβ
Profile a route with:
- one broad Client Component page
- a chart package
- a provider
- server-fetched table data
Refactor into server-first composition, then record:
client JS before/after
server route time before/after
interaction timing
which props cross the boundary
which modules are server-only
Explain the trade-off rather than declaring the refactor faster without measurements.