Next.jsMedium
What are third-party libraries' compatibility issues with RSC?
Many libraries were written for client-only React. If they use hooks, context, browser APIs or runtime style injection but do not declare "use client", importing them in a Server Component fails. Others pull in Node-only code or are large enough to bloat bundles if imported on the wrong side.
Common problems
- Missing
"use client": UI kits, carousels, animation libraries and form libraries that use hooks. Error: "useState only works in Client Components". - Context providers (theme, i18n, state libraries) cannot be rendered by Server Components unless they are client modules.
- Runtime CSS-in-JS (Emotion, older styled-components setups) needs a style registry with
useServerInsertedHTMLand works only in Client Components. - Browser globals at import time (
windowaccess at module scope) crash during server rendering. - Node-only or native packages (
sharp,bcrypt, Prisma engines) must not be bundled for the client and sometimes not even for the server. - Class instances as props (ORM models,
Decimal) are not serializable.
Fixes
- Wrap in your own client file:
"use client"; export { Slider } from "fancy-slider";. - Providers file: create
app/providers.tsxwith"use client"and render it in the layout. - Browser-only libraries: load with
next/dynamicandssr: falsefrom inside a Client Component. - Native packages: list them in
serverExternalPackagesso they are loaded fromnode_modulesat runtime. Since 16.1, Turbopack also externalizes their transitive dependencies. - Monorepo or ESM-only packages: use
transpilePackages. - Big barrel imports:
optimizePackageImports(enabled for many popular libraries automatically).
Checking compatibility
Most popular libraries (MUI, Chakra, Mantine, TanStack, Framer Motion) now ship RSC-compatible entry points. Check their App Router guide.
Code example
tsx
1// app/ui/slider.tsx - wrap a hooks-based library that lacks the directive2"use client";3export { Slider } from "fancy-slider";45// app/ui/map.tsx - library touches window at import time6"use client";7import dynamic from "next/dynamic";8export const Map = dynamic(() => import("leaflet-map").then((m) => m.Map), {9 ssr: false,10 loading: () => <p>Loading map…</p>,11});1213// next.config.ts14// const nextConfig = {15// serverExternalPackages: ["sharp", "@node-rs/argon2"], // native modules16// transpilePackages: ["@acme/ui"], // monorepo package17// experimental: { optimizePackageImports: ["my-icon-lib"] },18// };
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.