Skip to content
synccn

Next.js

Use synccn with the App Router, server components and SSR.

Client components

Components that use state, effects or browser APIs already start with "use client". You don't need to add anything: import them straight into a server component and Next.js handles the boundary.

app/page.tsx
import { Button } from "@/components/ui/button";import { Badge } from "@/components/ui/badge";// A server component. Button and Badge render here without any wrapper.export default function Page() {  return (    <main className="grid gap-4 p-10">      <Badge variant="soft">Beta</Badge>      <Button>Get started</Button>    </main>  );}

Each component page shows a Client component or Server-safe tag next to its description.

Server-safe components

These have no state or effects and no directive, so they render entirely on the server and send no JavaScript:

Functions and icons as props

A server component can't pass a function to a client component. That includes handlers such as onClick or onValueChange, render props, and component references such as a lucide icon in icon={Home} or inside an items array. Put that part in a small client component of your own:

components/save-button.tsx
"use client";import { Button } from "@/components/ui/button";import { toast } from "@/components/ui/toast";// Handlers and icon components can't be passed from a server component,// so put the interactive part in a small client component.export function SaveButton() {  return <Button onClick={() => toast.success("Saved")}>Save</Button>;}

Root layout

Mount <Toaster /> once, then call toast() from any client component. The inline script sets the dark class before the first paint, and suppressHydrationWarning stops React warning about the class it added.

app/layout.tsx
import type { Metadata } from "next";import { Toaster } from "@/components/ui/toast";import "./globals.css";export const metadata: Metadata = { title: "My app" };// Sets the theme class before the first paint, so there's no flash.const themeScript = `(() => {  const saved = localStorage.getItem("theme");  const dark = saved ? saved === "dark" : matchMedia("(prefers-color-scheme: dark)").matches;  document.documentElement.classList.toggle("dark", dark);})()`;export default function RootLayout({ children }: { children: React.ReactNode }) {  return (    <html lang="en" suppressHydrationWarning>      <head>        <script dangerouslySetInnerHTML={{ __html: themeScript }} />      </head>      <body>        {children}        <Toaster />      </body>    </html>  );}

Overlays and hydration

  • Dialog, Sheet, Drawer, Alert Dialog, Lightbox, Tour and Toaster render into document.body through a portal, but only after hydration. Rendering one with open already true on the server is safe.
  • useMediaQuery returns false on the server and during hydration, then updates, so server and client HTML always match.
  • Canvas components (Globe, Particles, Signature Pad, Confetti) only touch the canvas in effects, which never run on the server.
  • Countdown's digits are allowed to differ between server and browser, since a second may pass in between. If you render your own times or dates on the server, format them the same way on both sides or render them after mount.

Pages Router

Everything works the same in the Pages Router. The "use client" lines are ignored there, and you'd mount the toaster in pages/_app instead of the root layout.