Remix Documentation
website·Indexed 43 days ago
https://v2.remix.runDocumentation for the Remix web framework, covering routing, data loading, mutations using HTML forms, and deployment. Includes guides on the Remix Vite plugin, Single Fetch architecture, SPA mode, and migration from React Router applications.
What's inside Remix
- Remix uses adapters to translate a server's specific request/response format into the standard Web Fetch API. This allows Remix apps to be deployed across various environments. Adapters are imported into the server's entry point and are not used within the Remix app logic itself. If using the built-in Remix App Server, these adapters are handled automatically and do not require manual interaction.
Overview of Not Found (404) handling in Remix
Remix handles 404s in two primary scenarios:
- URL doesn't match any routes: Remix handles this automatically. To customize this behavior, consider using a Splat Route.
- Loader fails to find data: This must be handled manually by throwing a
Responsewith a 404 status from the loader.
Overview of Remix SPA Mode
SPA Mode allows you to build a Remix application that does not require a Node.js server at runtime. It generates a static
index.htmlfile at build time and relies on Client Data APIs for data loading and mutations. It is ideal for deploying to static hosts (like GitHub Pages or CDNs), migrating React Router apps to Remix, or creating embedded apps.Key features include:
- File-based or config-based routing
- Automatic route-based code-splitting via
route.lazy <Link prefetch>support- Head management via
<Meta>and<Links>APIs
SPA Mode requires the use of Vite and the Remix Vite Plugin.
Overview of Single Fetch in Remix
Single Fetch is a data loading strategy and streaming format that optimizes client-side transitions by making a single HTTP call to the server instead of multiple parallel calls (one per loader). It also enables loaders and actions to return 'naked' objects—such asDate,Error,Promise, andRegExp—without requiring manual JSON serialization.Overview of Remix Client Data loaders and actions
Introduced in v2.4.0, Remix allows you to opt-into running route loaders and actions in the browser usingclientLoaderandclientActionexports. These are intended for advanced use cases rather than as the primary data mechanism. Caution is advised as manual control over client-side caching can lead to UI synchronization issues if not managed carefully.Overview of Session Management in Remix
Sessions in Remix allow the server to identify requests from the same user. Unlike Express middleware, sessions are managed on a per-route basis withinloaderandactionfunctions using a session storage object. The storage object handles parsing/generating cookies and persisting data to a backend (database, filesystem, or the cookie itself).Overview of Remix full-stack framework
Remix is a full-stack web framework designed to deliver fast and resilient user experiences by leveraging web standards and native browser features. It is built on the Web Fetch API, allowing it to run in various environments including Cloudflare Workers, serverless functions, and traditional Node.js environments. Key architectural goals include eliminating request waterfalls through parallel server-side data loading and reducing loading states via nested routing and pre-fetching.Optimize client-side transitions with the Remix Browser Framework
After the server delivers the initial document, Remix hydrates the page with JavaScript. The browser framework optimizes user experience by emulating browser behavior:
- Client-side Navigation: When clicking a link, Remix fetches only the data for the next page instead of requesting a full document, updating the UI instantly.
- Data Mutations: When a
<Form>is submitted, the runtime intercepts the request and performs a fetch to the server, automatically revalidating all data on the page and updating the UI via React without a full page reload.
Use MDX for build-time content in Remix
Remix provides built-in support for MDX at build-time for users of the Classic Remix Compiler. Note that Vite users should instead use the MDX Rollup/Vite plugin. For better UX/DX and runtime customization,
mdx-bundleris recommended over build-time compilation.There are two primary ways to use MDX:
- Use a
.mdxor.mdfile as a route module inapp/routesto participate in automatic file-system routing. - Import a
.mdxfile into an existing route module.
- Use a
Access pending navigation metadata
The
useNavigationhook provides the following metadata about the pending transition:navigation.formAction: The action URL of the submitted form, if applicable.navigation.formMethod: The HTTP method of the submitted form (e.g., 'get', 'post'), if applicable.navigation.location: The destination location (URL) the application is navigating to.
Use the Remix App Server for production
The Remix App Server (@remix-run/serve) is a basic, production-ready Node.js server built with Express. It is intended for users who do not need to customize the underlying server. If customization of the Express server is required, use the@remix-run/expressadapter instead. The server includes the following default Express middlewares:compression,express.static(viaserve-static), andmorgan.remix-serve <server-build-path> # Example: remix-serve build/index.jsUnderstand the Remix architecture and build process
Remix is composed of four primary parts: a compiler, a server-side HTTP handler, a server framework, and a browser framework. The process begins with the compiler (
remix vite:build), which uses Vite to generate three key artifacts:- Server HTTP Handler: Typically located at
build/server/index.js, containing all routes and modules for server-side rendering and resource handling. - Browser Build: Located in
build/client/*, featuring automatic route-based code splitting and fingerprinted assets. - Asset Manifest: Used by both client and server to track the dependency graph, enabling the elimination of render+fetch waterfalls by preloading resources during initial render and prefetching during client transitions.
- Server HTTP Handler: Typically located at