How to Use Three.js in Next.js Without Breaking Server Rendering
The integration pattern that keeps Three.js out of your server bundle: a client boundary, dynamic import, a strict effect lifecycle, and full GPU cleanup.

TL;DR: Three.js crashes in Next.js for one predictable reason: it touches window and WebGL at import and setup time, and Next.js executes your component code on the server first. The fix is structural, not a workaround: put the canvas behind a "use client" component, load it with a dynamic import that skips server rendering, initialise inside useEffect, and dispose of everything on unmount. That is the pattern running in the Genç Group platform's 3D sections.
Why the Naive Import Fails
This is the code everyone writes first, and it fails in one of two ways:
// Do not do this in a server-rendered page
import * as THREE from "three";
export default function Page() {
const scene = new THREE.Scene(); // runs on the server too
return <canvas />;
}Either the build or the first server render hits window is not defined (from Three.js internals or its addons), or the page ships a canvas the server and client disagree about. Three.js is a browser library. Treat it like one.
The Boundary: One Client Component, Dynamically Imported
Keep the page itself a server component. Create the studio as a client component and import it so it never executes on the server:
// app/[lang]/project/page.tsx (server component)
import dynamic from "next/dynamic";
const ThreeStudio = dynamic(() => import("@/components/ThreeStudio"), {
ssr: false,
loading: () => <StudioPoster />, // a still image with the same aspect ratio
});
export default function Page() {
return (
<main>
<ProjectText />
<ThreeStudio modelUrl="/models/project.glb" />
</main>
);
}// components/ThreeStudio.tsx
"use client";
// Three.js imports live here and only hereThree rules make this work in practice:
- No Three.js import crosses the boundary. Server components, metadata files, and shared utilities must never import
three, even for types alone in a way that pulls the runtime in. Keep types for scene data as plain interfaces. - The poster is part of the component contract.
loadingrenders a real image, so layout, LCP, and the no-JS experience are all defined before WebGL exists. - Props stay serialisable. Pass URLs and plain option objects into the client component, not class instances or functions created on the server.
The Effect Lifecycle Inside the Studio
Initialise once, in an effect, against a ref'd canvas. Handle resize with the container, not the window, and keep the render loop pausable:
"use client";
import { useEffect, useRef } from "react";
import * as THREE from "three";
export default function ThreeStudio({ modelUrl }: { modelUrl: string }) {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 500);
const resize = () => {
const { clientWidth: w, clientHeight: h } = canvas.parentElement!;
renderer.setSize(w, h, false);
camera.aspect = w / h;
camera.updateProjectionMatrix();
};
resize();
const observer = new ResizeObserver(resize);
observer.observe(canvas.parentElement!);
let raf = 0;
const loop = () => {
renderer.render(scene, camera);
raf = requestAnimationFrame(loop);
};
loop();
return () => {
cancelAnimationFrame(raf);
observer.disconnect();
renderer.dispose();
};
}, [modelUrl]);
return <canvas ref={canvasRef} className="h-full w-full" />;
}React strict mode mounts effects twice in development. If your init is not idempotent and your cleanup is not complete, you will see two canvases, doubled listeners, or a black frame, and it will look like a Three.js bug. It is a lifecycle bug; the cleanup function above is the fix.
Loading Models: GLTFLoader with Draco
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";
const draco = new DRACOLoader();
draco.setDecoderPath("/draco/"); // decoder files served from public/
const loader = new GLTFLoader();
loader.setDRACOLoader(draco);
loader.load(modelUrl, (gltf) => scene.add(gltf.scene));Host the Draco decoder files in your own public/ folder instead of a CDN. A third-party decoder outage should never be able to blank your product page.
Errors I See in Inherited Next.js + Three.js Code
| Symptom | Actual cause |
|---|---|
window is not defined | Three.js imported by a server component or shared util |
| Black canvas, no error | Canvas has zero height, or the render loop never started |
| Memory climbs on every page change | Geometries and materials never disposed on unmount |
| Scene doubles in development | Effect ran twice, cleanup incomplete (strict mode) |
| Mobile Safari crashes the tab | Uncapped pixel ratio plus an uncompressed model |
How This Looks Shipped
On the Genç Group platform, every 3D surface, the architectural design lab and the automotive anatomy, sits behind this boundary: server-rendered layouts and metadata carry the SEO, and Three.js loads only inside its client sections. The step-by-step scene build is in how to build an interactive 3D website, and the architecture layer is in the architecture website tutorial. To have it built into your own Next.js site, see interactive 3D website development.
Three.js fighting your Next.js build?
Send me the error or the repo situation on WhatsApp. I will point at the boundary or lifecycle problem and tell you what fixing it takes.
Healix Medical: 5-Language International Patient Coordination Platform
Case study: 5-language patient coordination platform in Istanbul. 355 pages in the live sitemap, Arabic RTL, 33 contracted healthcare facilities.
QR Attendance System
QR attendance system built by Hamed Mohamed for 1,713 users (1,666 students, 40 academics, 7 admins) across 6 faculties: 98% latency reduction, 99.9% uptime.