🛠 iron-session examples

Cache Components and Partial Prerendering

App Router

How to test: log in, then reload. The cached panel keeps its timestamp, the session panel updates every time.

Cached, shared by everyone

Cache entry filled at 2026-08-30T23:30:07.113Z

Reload the page: this does not change. It is the same HTML for every visitor.

Protected page → redirects back here unless you are logged in, checked in the Data Access Layer.

GitHub Logo Get the code for this example
How it works
  1. cacheComponents: true is set in next.config.js, so a page can mix prerendered, cached and per-request content.
  2. The static shell and the use cache panel are prerendered. Neither one may call cookies(), so neither one may read a session.
  3. The session panel calls getIronSession(await cookies(), ...), which makes it dynamic. It has to sit inside <Suspense>, otherwise the build fails rather than shipping one visitor's session to everybody.
  4. The login form uses useActionState, so a rejected username comes back as a message instead of an exception.
  5. proxy.ts refreshes lastSeen on every request through nextProxyCookies, and the panel below shows the value written by that same request.

← All examples