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
npm i -D loom-devThat is all. Your app keeps its own React and Next, and no @loom-dev/* packages enter its
dependency tree.
next start also loads next.config, so loom-dev must be installed wherever it runs. If your
deploy prunes devDependencies, list it under dependencies.
2. The config
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.
withLoomGallery returns Next’s function config form, which other wrappers can’t wrap. Put it
outside: withLoomGallery(withMDX(config), options).
Add the build output to .gitignore:
public/loom-preview/3. What each command does
| Command | Where the gallery comes from |
|---|---|
next dev | A loom Vite server on a free loopback port, started on first use. Next proxies /loom-preview/* to it. |
next build | Built into public/loom-preview before the app compiles. |
next start | The 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 devkeeps running. The error is logged and/loom-previewreturns 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.tsxdoesn’t breaknext 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
| Option | Type | Default | Notes |
|---|---|---|---|
root | string | required | Project dir whose targets are served, relative to the app dir. |
targets | string | string[] | true | true | A glob, a directory, a list of either, or true for **/*.loom.tsx. Same as loom preview --targets. |
base | string | /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. |
port | number | free port | Pin the dev gallery’s port. |
hmrPort | number | false | free port | Vite’s HMR WebSocket port. false turns off hot reload. |
staticBuild | boolean | true | false skips the gallery build during next build. |
shims | Record<string, string> | {} | Replacements for roblox-ts packages loom can’t run. Paths are relative to root, not the Next app. See Package compatibility. |
assets | boolean | true | false 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:
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/:
## 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 handle | Meaning |
|---|---|
origin | http://127.0.0.1:<port>, the URL to proxy to. |
port | The bound port: the one you asked for, or a free one. |
base | The 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.
The server binds to 127.0.0.1 on purpose. It is for local development, not for exposing. Serve the
static build in production.
Routing details worth knowing
- Your own
rewrites()are kept, in either shape. A flat array staysafterFiles. A{ beforeFiles, afterFiles, fallback }object is merged, with loom’s rules in front of each group andfallbackuntouched. - 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 buildbakes the rewrites into the routes manifest, which is how thenext startrule reaches production.
Known limitations
trailingSlash: trueis 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 inout/with the rest ofpublic/, and works at its full path,/loom-preview/index.html.output: "standalone"doesn’t copypublic/, which is where the gallery is built. Copy it into the standalone output with your other public assets.