WebGL gradient background in Next.js: App Router, layouts and static export

Add a live WebGL gradient background to a Next.js app: import it in a Server Component, keep it in a layout across navigations, and skip next/dynamic with ssr: false.

To add a WebGL gradient background in Next.js, install a gradients.sh component with the shadcn CLI and import it straight into a Server Component. Skip next/dynamic with ssr: false: it removes the server-rendered fallback and the headline inside it.

Ship faster

Everything below was checked on 2026-11-23 in a fresh create-next-app project: Next.js 16.4.6 with Turbopack, React 19.3 and Tailwind 4.3.3, built with next build and opened in headless Chrome.

1. Install the component

Set the project up for shadcn/ui if it isn't yet, then add a background:

npx shadcn@latest init
npx shadcn@latest add https://gradients.sh/r/mesh-gradient.json

The CLI copies three things into your project: components/mesh-gradient.tsx, the shared components/gradient-surface.tsx, and the engine in lib/gradient-engine/ (65 files). There's no npm package to keep updated. The gradient draws with WebGPU, or WebGL2 where WebGPU isn't available.

To pick colours and motion by eye first, open the gradient editor, tune it and install what you see.

2. Use it in a Server Component

Can you use a WebGL component in a Server Component? Yes. The component files start with "use client", so a Server Component can import and render them like any other element:

app/page.tsx
import { MeshGradient } from "@/components/mesh-gradient";

export default function Page() {
  return (
    <MeshGradient
      className="h-[480px] p-10"
      colors={["#7c3aed", "#ec4899", "#f59e0b"]}
      background="#0b0b12"
    >
      <h1 className="text-4xl text-white">Ship your next launch</h1>
    </MeshGradient>
  );
}

The page still prerenders as static. In the HTML that next build writes, the canvas already carries the gradient's colours as a CSS background:

<canvas
  aria-hidden="true"
  data-gradient-kind="mesh"
  style="background:radial-gradient(85% 95% at 16% 20%, #7c3aed 0%, transparent 68%),
    radial-gradient(85% 95% at 84% 26%, #ec4899 0%, transparent 68%),
    radial-gradient(85% 95% at 72% 86%, #f59e0b 0%, transparent 68%), #0b0b12"
  class="pointer-events-none absolute inset-0 -z-10 block size-full"
></canvas>

So the box has its colours before any JavaScript runs, and the first GPU frame replaces that background in place. The <h1> is in the HTML too, so crawlers see your headline and the browser can paint it before hydration, instead of an empty div.

Props cross the client boundary, so they have to be serialisable: colour strings, numbers, booleans and children are fine. Define ordinary callbacks such as onClick inside a Client Component instead of passing them from the server.

3. Don't wrap it in next/dynamic with ssr: false

Many Next.js WebGL tutorials load the canvas with next/dynamic and ssr: false, because code that reads window while rendering crashes on the server. These components only use browser APIs in an effect, after mount, so they render on the server as they are.

In a Server Component, Next.js 16 refuses to build:

Error: `ssr: false` is not allowed with `next/dynamic` in Server Components. Please move it into a Client Component.

Moved into a client wrapper, it builds, but the result is worse than a plain import:

components/mesh-no-ssr.tsx
"use client";
import dynamic from "next/dynamic";

export const MeshGradientNoSsr = dynamic(
  () => import("@/components/mesh-gradient").then((m) => m.MeshGradient),
  { ssr: false }
);

We built both versions of the same page and compared their HTML with a plain page that has no gradient:

VersionCanvas fallback in HTML<h1> in HTMLExtra gzipped JS in the HTML's script tags, vs the plain page
Direct importYesYesabout 54 KB
next/dynamic, no SSRNoNoabout 2 KB, plus the component's chunk fetched after startup

With ssr: false, nothing inside the component renders on the server, including the children. Wrap a hero in it and the headline only appears after the component's JavaScript downloads and runs. Import the component directly.

4. Put it in a layout to keep it running across pages

Should the gradient go in a layout or a page? Put it in a layout when it should stay on screen while visitors move between pages. Next.js keeps a layout mounted while visitors move between the routes inside it, so the gradient isn't torn down. Here /one and /two both live under app/(site)/, so they share this layout:

app/(site)/layout.tsx
import Link from "next/link";
import { MeshGradient } from "@/components/mesh-gradient";

export default function SiteLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <MeshGradient
      className="min-h-svh"
      colors={["#7c3aed", "#ec4899", "#f59e0b"]}
    >
      <nav className="flex gap-4 p-4 text-white">
        <Link href="/one">One</Link>
        <Link href="/two">Two</Link>
      </nav>
      {children}
    </MeshGradient>
  );
}

Add the two pages as app/(site)/one/page.tsx and app/(site)/two/page.tsx, each returning its own heading. We tagged the canvas element on /one, clicked through to /two and back, and it was the same DOM node each time, still drawing. The animation carries on where it was.

In a page.tsx, the gradient belongs to that page, so its animation can restart from the beginning when a visitor comes back. Use a shared layout when the background should feel continuous across pages, and a page when each page has its own.

5. Static export and the Pages Router

Both work. With output: "export", next build wrote the canvas fallback and the headline into out/*.html for App Router pages and for a Pages Router page in pages/.

One catch with a fresh create-next-app 16.4 project: it turns on cacheComponents and partialPrefetching in next.config.ts, and export mode stops with Invariant: PPR cannot be enabled in export mode. Remove those two lines before adding output: "export". This comes from Next.js, not from the gradient.

next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: "export",
};

export default nextConfig;

In the Pages Router, import the component in any page under pages/. If the project started on the App Router, pages/ doesn't get the stylesheet that app/layout.tsx imports, so import your global Tailwind CSS in pages/_app.tsx as well.

6. Theme colours and dark mode

Colour props take hex values, oklch() and other colour literals, or a variable that holds a complete colour, such as shadcn's var(--primary). Alpha is ignored, and a variable that holds only channels, as in oklch(var(--channels)), won't resolve in the shader. The components re-read the variables when the class on <html> changes, so next-themes with attribute="class" switches the gradient along with the page. The shadcn/ui guide has the props, and gradients that follow light and dark mode covers choosing dark colours.

Performance

The Next.js-specific cost is the one in the table above: about 54 KB of gzipped JavaScript on the page that imports the component, measured against a plain page in the same build. Pausing off screen and in hidden tabs, the default device pixel ratio cap of 1.5, reduced motion and the CSS fallback for browsers without WebGPU or WebGL2 work the same in any React app; the shadcn/ui guide's performance section covers them. React gradient libraries compared has bundle sizes for Paper Shaders, Hazeglow and ShaderGradient, and WebGPU vs WebGL2 for UI backgrounds explains how the engine picks a backend.