Loomguides

Next.js integration

Serve the gallery from a Next.js app with `withLoomGallery()` — proxied in dev, static at build.

withLoomGallery() from loom-dev/next serves a loom gallery inside a Next.js app. You start next dev as usual and the gallery comes up at /loom-preview/. next build builds it as a static site in public/.

It works with the pages router and the App Router, webpack and Turbopack, and React 18 or 19 in your app. It has been tested on Next 15.5 and 16.2.

1. Dependencies

Terminal window
npm i -D loom-dev

That is all. Your app keeps its own React and Next, and no @loom-dev/* packages enter its dependency tree.

2. The config

next.config.ts
import { withLoomGallery } from "loom-dev/next";
export default withLoomGallery(
{ reactStrictMode: true }, // your Next config
{
root: "../my-ui", // the roblox-ts project, relative to the app dir
targets: "src/scenes", // same semantics as loom preview --targets
},
);

That is the whole setup. No prebuild script, no manual loom build, no second terminal.

Add the build output to .gitignore:

.gitignore
public/loom-preview/

3. What each command does

CommandWhere the gallery comes from
next devA loom Vite server on a free loopback port, started on first use. Next proxies /loom-preview/* to it.
next buildBuilt into public/loom-preview before the app compiles.
next startThe static build in public/loom-preview.

The gallery runs in its own Vite instance with its own React. Your app’s React and bundler config are untouched.

next dev

  • Target discovery, per-scene hot reload and the error panel work as under loom preview.
  • The proxy runs ahead of public/, so an old static gallery there can’t shadow the live one.
  • If the gallery fails to start, next dev keeps running. The error is logged and /loom-preview returns 404. It retries on the next config reload.
browser ──► next dev (:3000) ── beforeFiles rewrite ──► loom Vite server (127.0.0.1:<ephemeral>)
│ │
└────────────── HMR WebSocket (direct, bypasses Next) ◄──┘

next build

The wrapper builds the same bundle as loom build into public/loom-preview, once, then the app build continues.

  • Zero targets warns and skips, so adding the wrapper before your first *.loom.tsx doesn’t break next build.
  • A real gallery build error fails the app build, so a broken gallery doesn’t ship silently.

If your CI runs loom build itself, pass staticBuild: false.

next start

Nothing starts and nothing is built. The app serves the gallery from public/, and the wrapper maps the bare /loom-preview to its index.html. Deep links and iframes work the same as in dev.

4. Options

OptionTypeDefaultNotes
rootstringrequiredProject dir whose targets are served, relative to the app dir.
targetsstring | string[] | truetrueA glob, a directory, a list of either, or true for **/*.loom.tsx. Same as loom preview --targets.
basestring/loom-preview/Where the gallery is mounted in the app. Also sets the build output dir (public/<base>). Next’s basePath is added for you, so don’t repeat it here.
portnumberfree portPin the dev gallery’s port.
hmrPortnumber | falsefree portVite’s HMR WebSocket port. false turns off hot reload.
staticBuildbooleantruefalse skips the gallery build during next build.
shimsRecord<string, string>{}Replacements for roblox-ts packages loom can’t run. Paths are relative to root, not the Next app. See Package compatibility.
assetsbooleantruefalse stops the next build gallery downloading its rbxassetid:// images. The dev gallery is unaffected.

5. Embedding scenes in pages

The URL contract is the same in dev and in the static build, so an iframe written once works in both:

<iframe
src="/loom-preview/?chrome=none&target=src/scenes/Card.loom.tsx"
style={{ width: "100%", height: 360, border: 0 }}
/>

target is the scene’s path relative to root, and chrome=none hides the sidebar. Live theme switching works through the proxy too. It doesn’t matter whether the iframe is in an App Router page, a pages-router page, a server component or a client component.

With a basePath, include it in the iframe URL: basePath: "/docs" serves the gallery at /docs/loom-preview/.

6. Fumadocs

A Fumadocs site is a Next.js app, so use the same wrapper around createMDX, with loom outermost:

next.config.ts
import { createMDX } from "fumadocs-mdx/next";
import { withLoomGallery } from "loom-dev/next";
const withMDX = createMDX();
export default withLoomGallery(withMDX({ reactStrictMode: true }), {
root: "../my-ui",
targets: "src/scenes",
});

Then drop the iframe into any MDX page under content/docs/:

content/docs/components/card.mdx
## Live preview
<iframe
src="/loom-preview/?chrome=none&target=src/scenes/Card.loom.tsx"
style={{ width: "100%", height: 360, border: 0 }}
/>

Fumadocs’ markdown proxy only rewrites /docs paths, so it doesn’t collide with the gallery.

7. When you need the server yourself

startGalleryServer is what withLoomGallery uses in dev. Use it from a custom server, or any host that can only forward requests to a URL:

import { startGalleryServer } from "loom-dev/next";
const gallery = await startGalleryServer({
root: "../my-ui",
targets: "src/scenes",
// base?, port?, hmrPort? — same meanings as the wrapper's options
});
Field on the handleMeaning
originhttp://127.0.0.1:<port>, the URL to proxy to.
portThe bound port: the one you asked for, or a free one.
baseThe normalized path the gallery answers under.
close()Shuts down the HTTP server and the Vite server behind it.

Requests outside base get a 404, so you can point a prefix-preserving proxy at it.

Routing details worth knowing

  • Your own rewrites() are kept, in either shape. A flat array stays afterFiles. A { beforeFiles, afterFiles, fallback } object is merged, with loom’s rules in front of each group and fallback untouched.
  • The bare mount path is rewritten straight to the gallery’s slashed URL. Next strips trailing slashes and the gallery adds them, so routing it any other way would redirect forever.
  • next build bakes the rewrites into the routes manifest, which is how the next start rule reaches production.

Known limitations

  • trailingSlash: true is not supported. Next redirects extensionless paths to a slashed form before rewrites run. The gallery’s module URLs, like /@vite/client, are extensionless, so they arrive with a slash appended and miss.
  • output: "export" has no rewrites at runtime. The gallery still lands in out/ with the rest of public/, and works at its full path, /loom-preview/index.html.
  • output: "standalone" doesn’t copy public/, which is where the gallery is built. Copy it into the standalone output with your other public assets.