@pylonsync/next is the Next.js-specific layer on top of @pylonsync/react. It adds:
- Server Actions — call Pylon functions from
'use server'with the user’s session - RSC data fetching — load entities in React Server Components without round-tripping through the browser
- Middleware helpers — gate routes on auth in
middleware.ts - Cookie auth —
HttpOnlysession cookies that work with both server-rendered and client-rendered routes
@pylonsync/react directly.
Install
Configure
Inapp/providers.tsx:
app/layout.tsx:
Server Components
Server Actions
Call Pylon functions from'use server' actions. The session cookie is read automatically.
Middleware
Gate routes on auth without a round-trip to a client component:resolveSession reads the pylon_session cookie (or Authorization header) and hits /api/auth/me once per request. Cache it on req.locals if you call it from multiple places.
Auth flows
For SSR-friendly sign-in, use the cookie-based flow. Set up Pylon with cookie auth enabled (see Sessions):signInServer({ method: "oauth-callback", provider: "google", code, state }).
Streaming Server Components with streamFn
For AI-style streamed responses inside RSC:
Configuration
| Env | Purpose |
|---|---|
NEXT_PUBLIC_PYLON_URL | Public base URL the browser hits |
PYLON_URL | Server-side base URL — defaults to NEXT_PUBLIC_PYLON_URL |
localhost:4321, Next on localhost:3000), Cloud users typically use the same URL for both.
Edge runtime
Most@pylonsync/next/server helpers work in the Edge runtime. Exceptions:
streamFnServerrequires the Node.js runtime (usesURLSession.bytes-equivalent streaming)getServerDataworks on EdgefetchListServer/callFnServerwork on EdgesignInServerworks on Edge
export const runtime = "nodejs" on the route segment.
Common pitfalls
- Don’t call client hooks in Server Components —
useQueryis browser-only. For SSR, usefetchListServerand pass the result down as props. - Don’t mix server and client
configureClient— the server readsPYLON_URL, the client readsNEXT_PUBLIC_PYLON_URL. Set both. - Server Actions auto-revalidate — call
revalidatePathorrevalidateTagafter mutations so RSC components re-render with fresh data.