React and Next.js rendering integration
The React package connects a typed UploadThing router to browser upload features and to Next.js server rendering. Its Next.js integration carries the safe, client-readable route rules from the server-rendered tree into the browser so upload controls can use the same rules without exposing the full server router. See React SDK for the package overview and React upload hooks and components for the upload UI layer.
Route configuration boundary
UploadThing separates the server router from the route metadata that a browser needs. A route is identified by its slug and exposes an expanded configuration that describes permitted file kinds and related limits.
Route metadata contract
EndpointMetadata is the shared contract for this handoff. It is a list of endpoint slugs and ExpandedRouteConfig values. The server creates this list from the router's route configuration, so the client receives policy data rather than server callbacks or other private route details.
References: packages/…/types.ts• L223-238 packages/…/route-config.ts• L117-125 packages/…/server.ts• L32-47
Typed router boundary
The React APIs keep the server router as a TypeScript boundary. FileRouter supplies endpoint names and endpoint types, while React helpers infer endpoint input, output, and error shapes from the selected route. This preserves a link between a component or hook and the route it uses without sending those types to the browser at run time.
References: packages/…/types.ts• L15-40 packages/…/types.ts• L52-120 packages/…/types.ts• L122-200
Next.js server-rendered handoff
The Next.js SSR plugin places route metadata into both sides of a rendered application. It runs in a root layout, records the configuration for server-side access, and inserts one script so the hydrated browser can read the same data.
Root layout placement
An application places NextSSRPlugin near the root of its Next.js app and passes it extractRouterConfig(uploadRouter). This placement makes the metadata available to upload controls across the rendered tree. The extractor intentionally returns only route configuration, which limits the data sent to the client.
References: packages/…/next-ssr-plugin.tsx• L12-20 examples/…/layout.tsx• L5-23 examples/…/layout.tsx• L11-46
Server-to-browser transfer
The plugin stores the supplied metadata in the globalThis.__UPLOADTHING application boundary during rendering. Next.js then inserts a single nonce-aware script that assigns the same metadata in the browser. The browser can therefore use route rules immediately after hydration instead of waiting for a metadata request.
The following flow shows how route policy moves from the typed server router into React upload consumers.
flowchart LR
R[Typed UploadThing router] --> E[extractRouterConfig]
E --> P[NextSSRPlugin in root layout]
P --> S[Server global route metadata]
P --> H[Inserted browser script]
H --> C[Browser global route metadata]
C --> U[React hook or helper]
U --> V[Upload UI and client behavior]
References: packages/…/next-ssr-plugin.tsx• L1-40 packages/…/types.ts• L231-234 packages/…/route-config.ts• L117-125
Content security boundary
The plugin accepts an optional nonce and applies it to the inserted script. Applications that protect inline scripts with a content security policy can use this value to keep the route metadata handoff within that policy.
References: packages/…/next-ssr-plugin.tsx• L12-15 packages/…/next-ssr-plugin.tsx• L22-36
React route configuration access
React upload helpers use the server-rendered metadata when it exists and otherwise use the UploadThing API endpoint as their route-information source. This gives server-rendered Next.js applications a fast local path while keeping a fetch-based path for applications that do not provide the SSR plugin.
Hook configuration lookup
useUploadThing resolves the selected endpoint from the typed router and looks for its configuration in the shared browser boundary. If the metadata is not present, it requests endpoint metadata from the configured UploadThing URL. The hook exposes the selected route configuration with upload state and the upload action, so an application can align its own UI with server-defined rules.
References: packages/…/use-uploadthing.ts• L33-50 packages/…/use-uploadthing.ts• L57-70 packages/…/use-uploadthing.ts• L147-154
Non-React configuration lookup
generateReactHelpers binds a router to the UploadThing URL and fetch function, then exposes getRouteConfig for code outside a React render. This direct lookup depends on SSR metadata being present. If a route cannot be found there, the helper stops with an error that points the application to NextSSRPlugin.
References: packages/…/use-uploadthing.ts• L160-205 packages/…/types.ts• L20-50
Configuration-driven application behavior
A client component can use the returned routeConfig to adapt local behavior before upload. In the profile picture example, the route's image properties guide crop sizing and its permitted types guide the file input. The route remains the source of truth while the application decides how to present that policy.
References: examples/…/profile-pic-uploader.tsx• L22-61 examples/…/profile-pic-uploader.tsx• L81-93 examples/…/profile-pic-uploader.tsx• L209-229
Package and runtime boundary
The React package keeps the Next.js integration at a separate package entry point. This lets ordinary React consumers use the main package without requiring Next.js while Next.js applications can opt into the SSR bridge.
SSR package entry point
Next.js applications import NextSSRPlugin from @uploadthing/react/next-ssr-plugin. The package declares Next.js as an optional peer and publishes the SSR entry separately from the main React exports. The main entry still exposes the React upload components, hooks, and shared React types.
References: packages/…/package.json• L15-52 packages/…/package.json• L66-78 packages/…/index.ts• L1-15
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the shape returned by
extractRouterConfigaligned withEndpointMetadata, because the SSR plugin and React lookup path share this contract. - Keep the
__UPLOADTHINGhandoff name and its serialization path aligned between the plugin and the React helper lookup. - When changing the React package entry points or Next.js peer boundary, verify that the dedicated
next-ssr-pluginimport remains available without making Next.js required for the main React package. - When changing route configuration fields, check client consumers that use
ExpandedRouteConfigto shape local file inputs or image behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
The root layout does not render NextSSRPlugin | getRouteConfig cannot find route metadata and throws. A hook must fall back to fetching metadata. | Add the plugin to the rendered Next.js layout and pass the matching router configuration. |
| The plugin receives a router configuration from a different router than the upload endpoint | The client can use rules for the wrong endpoint or fail to find the selected slug. | Extract metadata from the same uploadRouter that backs the server upload route. |
| The SSR script is blocked by the application's content security policy | The browser global remains empty and client route lookup loses the SSR path. | Pass the policy's nonce to NextSSRPlugin. |
| A route slug is renamed without updating its client selection | The typed client points to a slug with no matching metadata or server route. | Keep router keys and client endpoint arguments synchronized. |
Key files
| File | Why to read it |
|---|---|
| packages/…/next-ssr-plugin.tsx | Defines the Next.js rendering bridge from server route metadata to the browser. |
| packages/…/use-uploadthing.ts | Shows how hooks and generated helpers consume route metadata and how the fetch fallback works. |
| packages/…/types.ts | Defines the typed options and route configuration shapes that cross the React boundary. |
| packages/…/types.ts | Defines the shared endpoint metadata and expanded route configuration contract. |
| packages/…/server.ts | Exposes the server-side route configuration extractor used by Next.js layouts. |
| packages/…/route-config.ts | Shows the high-level conversion from a server router to client-readable metadata. |
| packages/…/package.json | Documents the separate Next.js package entry point and optional runtime dependency. |
| examples/…/layout.tsx | Gives the smallest example of placing the SSR plugin in a Next.js app layout. |
| examples/…/profile-pic-uploader.tsx | Shows how a client component uses route configuration to adapt file selection and image handling. |