This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.
Effects are attributes, so a server renders them like any other HTML. The app lives in the browser only: it measures the DOM, draws on a canvas and loads three.js there. This page covers what runs where with Next.js, Nuxt, SvelteKit, Astro and other server renderers, static generation included.
Render the markup on the server
data-dio and its params are plain attributes. A server component, a Nuxt page or an Astro template writes them without any Diorama code on the server.
src/app/page.tsx
exportdefaultfunctionPage(){
return(
<img
src="/cover.jpg"
width={800}
height={450}
data-dio="ripple"
/>
)
}
Until the app starts, the element is ordinary HTML, so the page reads and lays out the same before the engine arrives and on devices that never load it. When the canvas copy is ready, it takes the element’s exact box, and the DOM picture is hidden only from that frame on.
Do not render data-dio-state on the server or set it in templates. The runtime writes it and fixes values it did not write, such as one copied from a cached page.
Start in the browser
Start the app in code that runs only in the browser, after hydration.
useEffect of a client component rendered by the root layout
Nuxt
onNuxtReady in a client plugin, app/plugins/diorama.client.ts
SvelteKit
onMount in the root +layout.svelte
Astro
A <script> in the shared layout
Importing @dioramajs/diorama on the server is safe: it reads no window or document at import and starts no timers, and effect modules load in the browser only. Calling start() is what to keep out of server code. In Node the app is inert, but the module keeps it for the life of the server process, and server code has nothing to draw.
Starting before hydration has a visible cost in React: the runtime may write data-dio-state on a server-rendered element first, and React 19 prints a hydration mismatch warning in development. The node is kept, but useEffect runs after hydration and avoids the warning.
Share the app with components
A module that calls start() at its top level runs on the server as soon as a server-rendered component imports it. Export a function instead, and call it from browser code only: effects, event handlers, client plugins. start() returns the same app on every call, so every caller gets one app with the same options.
src/lib/diorama.ts
import{
functionstart(options?:StartOptions):App
Starts Diorama on the page and returns the app at once. It finishes starting in the
background. A second call returns the same app, so a re-run module doesn't make another.
import { start } from '@dioramajs/diorama'
export const app = start()
start}from'@dioramajs/diorama'
/** The page's app. Call it from browser code only. */
exportconst
constgetApp:()=>App
The page's app. Call it from browser code only.
getApp=()=>
functionstart(options?:StartOptions):App
Starts Diorama on the page and returns the app at once. It finishes starting in the
background. A second call returns the same app, so a re-run module doesn't make another.
import { start } from '@dioramajs/diorama'
export const app = start()
start({
StartOptions.background?: string | number |undefined
Clear colour: a number or any CSS colour, e.g. '#08080b'. Transparent by default.
Render <Diorama /> once in the root layout. Effects of child components run before the effect of the layout, so Hero may be the first to call getApp(); with the options in one place, that makes no difference.
app/plugins/diorama.client.ts
import{ getApp }from'~/lib/diorama'
exportdefaultdefineNuxtPlugin(()=>{
onNuxtReady(()=>{
getApp()
})
})
.client.ts plugins run in the browser only, and onNuxtReady waits for hydration. In components, call getApp().attach(...) in onMounted.
src/layouts/Layout.astro
<script>
import{ getApp }from'../lib/diorama'
getApp()
</script>
Astro bundles the script and runs it once in the browser, and the app lives across ClientRouter navigations. Islands are React, Vue or Svelte components and follow the other tabs. Markup with data-dio in .astro files needs no island.
Call getApp() in onMount of the root +layout.svelte, and in onMount or an attachment of each component.
src/routes/+layout.svelte
<scriptlang="ts">
import{ onMount }from'svelte'
import{ getApp }from'$lib/diorama'
let{ children }=$props()
onMount(()=>{
getApp()
})
</script>
{@renderchildren()}
A handle from attach() works at once. Until the app runs, it records params written to it and hands them over.
transition() is safe to call before the app runs, without the transitions plugin, after the app is destroyed and in Node: it calls swap once and resolves with the status 'skipped'. A router recipe can call it from shared code.
Next.js
Server components write markup only. Keep start() and code that uses the app in client components.
next/image passes unknown attributes to the <img> it renders, so data-dio and its params work on it. Add crossOrigin="anonymous" for images from another origin.
withDiorama writes src/diorama.gen.ts, the registry of your effects folder with lazy imports, and points both bundlers at it. Commit it. The server build includes the registry, but nothing on the server calls it.
App Router navigations swap the page in place. The app and the canvas stay, the effects of the old page are released and those of the new page created.
Static generation
A statically generated page is server rendering at build time, and the same rules apply: markup in the HTML, start() in the browser. Reduced motion, touch, Save-Data and the GPU are all decided in the browser, so one HTML file serves every visitor.