Preparing for v2

All v2 APIs and behaviors are available in v1 with Future Flags. They can be enabled one at a time to avoid development disruption of your project. After you have enabled all flags, upgrading to v2 should be a non-breaking upgrade.

File System Route Convention

Upgrading without changing files

You can keep using the old convention with @remix-run/v1-route-convention even after upgrading to v2 if you don’t want to make the change right now (or ever, it’s just a convention, and you can use whatever file organization you prefer).

  1. npm i @remix-run/v1-route-convention
  1. const {
  2. createRoutesFromFolders,
  3. } = require("@remix-run/v1-route-convention");
  4. /** @type {import('@remix-run/dev').AppConfig} */
  5. module.exports = {
  6. future: {
  7. // makes the warning go away in v1.15+
  8. v2_routeConvention: true,
  9. },
  10. routes(defineRoutes) {
  11. // uses the v1 convention, works in v1.15+ and v2
  12. return createRoutesFromFolders(defineRoutes);
  13. },
  14. };

Upgrading to the new convention

  • Route nesting is now created by dots (.) in file names instead of folder nesting
  • suffixed_ underscores in segments opt-out of nesting with a potentially matching parent route instead of dots (.).
  • _prefixed underscores in segments create layout routes without a path instead of a __double underscore prefix.
  • _index.tsx files create index routes instead of index.tsx

A routes folder that looks like this in v1:

  1. routes
  2. ├── __auth
  3. ├── login.tsx
  4. ├── logout.tsx
  5. └── signup.tsx
  6. ├── __public
  7. ├── about-us.tsx
  8. ├── contact.tsx
  9. └── index.tsx
  10. ├── dashboard
  11. ├── calendar
  12. ├── $day.tsx
  13. └── index.tsx
  14. ├── projects
  15. ├── $projectId
  16. ├── collaborators.tsx
  17. ├── edit.tsx
  18. ├── index.tsx
  19. ├── settings.tsx
  20. └── tasks.$taskId.tsx
  21. ├── $projectId.tsx
  22. └── new.tsx
  23. ├── calendar.tsx
  24. ├── index.tsx
  25. └── projects.tsx
  26. ├── __auth.tsx
  27. ├── __public.tsx
  28. └── dashboard.projects.$projectId.print.tsx

Becomes this with v2_routeConvention:

  1. routes
  2. ├── _auth.login.tsx
  3. ├── _auth.logout.tsx
  4. ├── _auth.signup.tsx
  5. ├── _auth.tsx
  6. ├── _public._index.tsx
  7. ├── _public.about-us.tsx
  8. ├── _public.contact.tsx
  9. ├── _public.tsx
  10. ├── dashboard._index.tsx
  11. ├── dashboard.calendar._index.tsx
  12. ├── dashboard.calendar.$day.tsx
  13. ├── dashboard.calendar.tsx
  14. ├── dashboard.projects.$projectId._index.tsx
  15. ├── dashboard.projects.$projectId.collaborators.tsx
  16. ├── dashboard.projects.$projectId.edit.tsx
  17. ├── dashboard.projects.$projectId.settings.tsx
  18. ├── dashboard.projects.$projectId.tasks.$taskId.tsx
  19. ├── dashboard.projects.$projectId.tsx
  20. ├── dashboard.projects.new.tsx
  21. ├── dashboard.projects.tsx
  22. └── dashboard_.projects.$projectId.print.tsx

Note that parent routes are now grouped together instead of having dozens of routes between them (like the auth routes). Routes with the same path but not the same nesting (like dashboard and dashboard_) also group together.

With the new convention, any route can be a directory with a route.tsx file inside to define the route module. This enables co-location of modules with the route they’re used in:

For example, we can move _public.tsx to _public/route.tsx and then co-locate modules the route uses:

  1. routes
  2. ├── _auth.tsx
  3. ├── _public
  4. ├── footer.tsx
  5. ├── header.tsx
  6. └── route.tsx
  7. ├── _public._index.tsx
  8. ├── _public.about-us.tsx
  9. └── etc.

For more background on this change, see the original “flat routes” proposal.

Route headers

In Remix v2, the behavior for route headers functions is changing slightly. You can opt-into this new behavior ahead of time via the future.v2_headers flag in remix.config.js.

In v1, Remix would only use the result of the leaf “rendered” route headers function. It was your responsibility to add a headers function to every potential leaf and merge in parentHeaders accordingly. This can get tedious quickly and is also easy to forget to add a headers function when you add a new route, even if you want it to just share the same headers from it’s parent.

In v2, Remix will use the deepest headers function that it finds in the rendered routes. This more easily allows you to share headers across routes from a common ancestor. Then as needed you can add headers functions to deeper routes if they require specific behavior.

Route meta

In Remix v2, the signature for route meta functions and how Remix handles meta tags under the hood have changed.

Instead of returning an object from meta, you will now return an array of descriptors and manage the merge yourself. This brings the meta API closer to links, and it allows for more flexibility and control over how meta tags are rendered.

In addition, <Meta /> will no longer render meta for every route in the hierarchy. Only data returned from meta in the leaf route will be rendered. You can still choose to include meta from the parent route by accessing matches in the function’s arguments.

For more background on this change, see the original v2 meta proposal.

Using v1 meta conventions in v2

You can update your meta exports with the @remix-run/v1-meta package to continue using v1 conventions.

Using the metaV1 function, you can pass in the meta function’s arguments and the same object it currently returns. This function will use the same merging logic to merge the leaf route’s meta with its direct parent route meta before converting it to an array of meta descriptors usable in v2.

  1. export function meta() {
  2. return {
  3. title: "...",
  4. description: "...",
  5. "og:title": "...",
  6. };
  7. }
  1. import { metaV1 } from "@remix-run/v1-meta";
  2. export function meta(args) {
  3. return metaV1(args, {
  4. title: "...",
  5. description: "...",
  6. "og:title": "...",
  7. });
  8. }

It’s important to note that this function will not merge meta across the entire hierarchy by default. This is because you may have some routes that return an array of objects directly without the metaV1 function and this could result in unpredictable behavior. If you want to merge meta across the entire hierarchy, use the metaV1 function for all of your route’s meta exports.

The parentsData argument

In v2, the meta function no longer receives the parentsData argument. This is because meta now has access to all of your route matches via the matches argument, which includes loader data for each match.

To replicate the API of parentsData, the @remix-run/v1-meta package provides a getMatchesData function. It returns an object where the data for each match is keyed by the route’s ID.

  1. export function meta(args) {
  2. const parentData = args.parentsData["routes/parent"];
  3. }

Becomes:

  1. import { getMatchesData } from "@remix-run/v1-meta";
  2. export function meta(args) {
  3. const matchesData = getMatchesData(args);
  4. const parentData = matchesData["routes/parent"];
  5. }

Updating to the new meta

  1. export function meta() {
  2. return {
  3. title: "...",
  4. description: "...",
  5. "og:title": "...",
  6. };
  7. }
  1. export function meta() {
  2. return [
  3. { title: "..." },
  4. { name: "description", content: "..." },
  5. { property: "og:title", content: "..." },
  6. // you can now add SEO related <links>
  7. { tagName: "link", rel: "canonical", href: "..." },
  8. // and <script type=ld+json>
  9. {
  10. "script:ld+json": {
  11. some: "value",
  12. },
  13. },
  14. ];
  15. }

The matches argument

Note that in v1 the objects returned from nested routes were all merged, you will need to manage the merge yourself now with matches:

  1. export function meta({ matches }) {
  2. const rootMeta = matches[0].meta;
  3. const title = rootMeta.find((m) => m.title);
  4. return [
  5. title,
  6. { name: "description", content: "..." },
  7. { property: "og:title", content: "..." },
  8. // you can now add SEO related <links>
  9. { tagName: "link", rel: "canonical", href: "..." },
  10. // and <script type=ld+json>
  11. {
  12. "script:ld+json": {
  13. "@context": "https://schema.org",
  14. "@type": "Organization",
  15. name: "Remix",
  16. },
  17. },
  18. ];
  19. }

The meta v2 docs have more tips on merging route meta.

CatchBoundary and ErrorBoundary

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. future: {
  4. v2_errorBoundary: true,
  5. },
  6. };

In v1, a thrown Response will render the closest CatchBoundary while all other unhandled exceptions render the ErrorBoundary. In v2 there is no CatchBoundary and all unhandled exceptions will render the ErrorBoundary, response or otherwise.

Additionally, the error is no longer passed to ErrorBoundary as props but is accessed with the useRouteError hook.

  1. import { useCatch } from "@remix-run/react";
  2. export function CatchBoundary() {
  3. const caught = useCatch();
  4. return (
  5. <div>
  6. <h1>Oops</h1>
  7. <p>Status: {caught.status}</p>
  8. <p>{caught.data.message}</p>
  9. </div>
  10. );
  11. }
  12. export function ErrorBoundary({ error }) {
  13. console.error(error);
  14. return (
  15. <div>
  16. <h1>Uh oh ...</h1>
  17. <p>Something went wrong</p>
  18. <pre>{error.message || "Unknown error"}</pre>
  19. </div>
  20. );
  21. }

Becomes:

  1. import {
  2. useRouteError,
  3. isRouteErrorResponse,
  4. } from "@remix-run/react";
  5. export function ErrorBoundary() {
  6. const error = useRouteError();
  7. // when true, this is what used to go to `CatchBoundary`
  8. if (isRouteErrorResponse(error)) {
  9. return (
  10. <div>
  11. <h1>Oops</h1>
  12. <p>Status: {error.status}</p>
  13. <p>{error.data.message}</p>
  14. </div>
  15. );
  16. }
  17. // Don't forget to typecheck with your own logic.
  18. // Any value can be thrown, not just errors!
  19. let errorMessage = "Unknown error";
  20. if (isDefinitelyAnError(error)) {
  21. errorMessage = error.message;
  22. }
  23. return (
  24. <div>
  25. <h1>Uh oh ...</h1>
  26. <p>Something went wrong.</p>
  27. <pre>{errorMessage}</pre>
  28. </div>
  29. );
  30. }

formMethod

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. future: {
  4. v2_normalizeFormMethod: true,
  5. },
  6. };

Multiple APIs return the formMethod of a submission. In v1 they return a lowercase version of the method but in v2 they return the UPPERCASE version. This is to bring it in line with HTTP and fetch specifications.

  1. function Something() {
  2. const navigation = useNavigation();
  3. // v1
  4. navigation.formMethod === "post";
  5. // v2
  6. navigation.formMethod === "POST";
  7. }
  8. export function shouldRevalidate({ formMethod }) {
  9. // v1
  10. formMethod === "post";
  11. // v2
  12. formMethod === "POST";
  13. }

useTransition

This hook is now called useNavigation to avoid confusion with the recent React hook by the same name. It also no longer has the type field and flattens the submission object into the navigation object itself.

  1. import { useTransition } from "@remix-run/react";
  2. function SomeComponent() {
  3. const transition = useTransition();
  4. transition.submission.formData;
  5. transition.submission.formMethod;
  6. transition.submission.formAction;
  7. transition.type;
  8. }
  1. import { useNavigation } from "@remix-run/react";
  2. function SomeComponent() {
  3. const navigation = useNavigation();
  4. // transition.submission keys are flattened onto `navigation[key]`
  5. navigation.formData;
  6. navigation.formMethod;
  7. navigation.formAction;
  8. // this key is removed
  9. navigation.type;
  10. }

You can derive the previous transition.type with the following examples. Keep in mind, there’s probably a simpler way to get the same behavior, usually checking navigation.state, navigation.formData or the data returned from an action with useActionData can get the UX you’re looking for. Feel free to ask us in Discord, and we’ll help you out :D

  1. function Component() {
  2. const navigation = useNavigation();
  3. // transition.type === "actionSubmission"
  4. const isActionSubmission =
  5. navigation.state === "submitting";
  6. // transition.type === "actionReload"
  7. const isActionReload =
  8. navigation.state === "loading" &&
  9. navigation.formMethod != null &&
  10. navigation.formMethod != "GET" &&
  11. // We had a submission navigation and are loading the submitted location
  12. navigation.formAction === navigation.location.pathname;
  13. // transition.type === "actionRedirect"
  14. const isActionRedirect =
  15. navigation.state === "loading" &&
  16. navigation.formMethod != null &&
  17. navigation.formMethod != "GET" &&
  18. // We had a submission navigation and are now navigating to different location
  19. navigation.formAction !== navigation.location.pathname;
  20. // transition.type === "loaderSubmission"
  21. const isLoaderSubmission =
  22. navigation.state === "loading" &&
  23. navigation.state.formMethod === "GET" &&
  24. // We had a loader submission and are navigating to the submitted location
  25. navigation.formAction === navigation.location.pathname;
  26. // transition.type === "loaderSubmissionRedirect"
  27. const isLoaderSubmissionRedirect =
  28. navigation.state === "loading" &&
  29. navigation.state.formMethod === "GET" &&
  30. // We had a loader submission and are navigating to a new location
  31. navigation.formAction !== navigation.location.pathname;
  32. }

useFetcher

Like useNavigation, useFetcher has flattened the submission and removed the type field.

  1. import { useFetcher } from "@remix-run/react";
  2. function SomeComponent() {
  3. const fetcher = useFetcher();
  4. fetcher.submission.formData;
  5. fetcher.submission.formMethod;
  6. fetcher.submission.formAction;
  7. fetcher.type;
  8. }
  1. import { useFetcher } from "@remix-run/react";
  2. function SomeComponent() {
  3. const fetcher = useFetcher();
  4. // these keys are flattened
  5. fetcher.formData;
  6. fetcher.formMethod;
  7. fetcher.formAction;
  8. // this key is removed
  9. fetcher.type;
  10. }

You can derive the previous fetcher.type with the following examples. Keep in mind, there’s probably a simpler way to get the same behavior, usually checking fetcher.state, fetcher.formData or the data returned from an action on fetcher.data can get the UX you’re looking for. Feel free to ask us in Discord, and we’ll help you out :D

  1. function Component() {
  2. const fetcher = useFetcher();
  3. // fetcher.type === "done"
  4. const isDone =
  5. fetcher.state === "idle" && fetcher.data != null;
  6. // fetcher.type === "actionSubmission"
  7. const isActionSubmission = fetcher.state === "submitting";
  8. // fetcher.type === "actionReload"
  9. const isActionReload =
  10. fetcher.state === "loading" &&
  11. fetcher.formMethod != null &&
  12. fetcher.formMethod != "GET" &&
  13. // If we returned data, we must be reloading
  14. fetcher.data != null;
  15. // fetcher.type === "actionRedirect"
  16. const isActionRedirect =
  17. fetcher.state === "loading" &&
  18. fetcher.formMethod != null &&
  19. navigation.formMethod != "GET" &&
  20. // If we have no data we must have redirected
  21. fetcher.data == null;
  22. // fetcher.type === "loaderSubmission"
  23. const isLoaderSubmission =
  24. navigation.state === "loading" &&
  25. navigation.state.formMethod === "GET";
  26. // fetcher.type === "normalLoad"
  27. const isNormalLoad =
  28. navigation.state === "loading" &&
  29. navigation.state.formMethod == null;
  30. }

Route links properties should all be the React camelCase values instead of HTML lowercase values. These two values snuck in as lowercase in v1. In v2 only the camelCase versions will be valid:

  1. export const links: LinksFunction = () => {
  2. return [
  3. {
  4. rel: "preload",
  5. as: "image",
  6. imagesrcset: "...",
  7. imagesizes: "...",
  8. },
  9. ];
  10. };
  1. export const links: V2_LinksFunction = () => {
  2. return [
  3. {
  4. rel: "preload",
  5. as: "image",
  6. imageSrcSet: "...",
  7. imageSizes: "...",
  8. },
  9. ];
  10. };

browserBuildDirectory

In your remix.config.js, rename browserBuildDirectory to assetsBuildDirectory.

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. browserBuildDirectory: "./public/build",
  4. };
  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. assetsBuildDirectory: "./public/build",
  4. };

serverBuildDirectory

In your remix.config.js, rename serverBuildDirectory to serverBuildPath and specify a module path, not a directory.

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. serverBuildDirectory: "./build",
  4. };
  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. serverBuildPath: "./build/index.js",
  4. };

Remix used to create more than a single module for the server, but it now creates a single file.

serverBuildTarget

Instead of specifying a build target, use the Remix Config options to generate the server build your server target expects. This change allows Remix to deploy to more JavaScript runtimes, servers, and hosts without Remix source code needing to know about them.

The following configurations should replace your current serverBuildTarget:

arc

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/_static/build/",
  4. serverBuildPath: "server/index.js",
  5. serverMainFields: ["main", "module"], // default value, can be removed
  6. serverMinify: false, // default value, can be removed
  7. serverModuleFormat: "cjs", // default value, can be removed
  8. serverPlatform: "node", // default value, can be removed
  9. };

cloudflare-pages

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: "functions/[[path]].js",
  5. serverConditions: ["worker"],
  6. serverDependenciesToBundle: "all",
  7. serverMainFields: ["browser", "module", "main"],
  8. serverMinify: true,
  9. serverModuleFormat: "esm",
  10. serverPlatform: "neutral",
  11. };

cloudflare-workers

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: "build/index.js", // default value, can be removed
  5. serverConditions: ["worker"],
  6. serverDependenciesToBundle: "all",
  7. serverMainFields: ["browser", "module", "main"],
  8. serverMinify: true,
  9. serverModuleFormat: "esm",
  10. serverPlatform: "neutral",
  11. };

deno

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: "build/index.js", // default value, can be removed
  5. serverConditions: ["deno", "worker"],
  6. serverDependenciesToBundle: "all",
  7. serverMainFields: ["module", "main"],
  8. serverMinify: false, // default value, can be removed
  9. serverModuleFormat: "esm",
  10. serverPlatform: "neutral",
  11. };

netlify

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: ".netlify/functions-internal/server.js",
  5. serverMainFields: ["main", "module"], // default value, can be removed
  6. serverMinify: false, // default value, can be removed
  7. serverModuleFormat: "cjs", // default value, can be removed
  8. serverPlatform: "node", // default value, can be removed
  9. };

node-cjs

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: "build/index.js", // default value, can be removed
  5. serverMainFields: ["main", "module"], // default value, can be removed
  6. serverMinify: false, // default value, can be removed
  7. serverModuleFormat: "cjs", // default value, can be removed
  8. serverPlatform: "node", // default value, can be removed
  9. };

vercel

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. publicPath: "/build/", // default value, can be removed
  4. serverBuildPath: "api/index.js",
  5. serverMainFields: ["main", "module"], // default value, can be removed
  6. serverMinify: false, // default value, can be removed
  7. serverModuleFormat: "cjs", // default value, can be removed
  8. serverPlatform: "node", // default value, can be removed
  9. };

serverModuleFormat

The default server module output format will be changing from cjs to esm.

In your remix.config.js, you should specify either serverModuleFormat: "cjs" to retain existing behavior, or serverModuleFormat: "esm", to opt into the future behavior.

serverNodeBuiltinsPolyfill

Polyfills for Node.js built-in modules will no longer be provided by default for non-Node.js server platforms.

If you are targeting a non-Node.js server platform and want to opt into the future default behavior, in remix.config.js you should first remove all server polyfills by providing an empty object for serverNodeBuiltinsPolyfill.modules:

  1. module.exports = {
  2. serverNodeBuiltinsPolyfill: {
  3. modules: {},
  4. },
  5. };

You can then reintroduce any polyfills (or blank polyfills) as required.

  1. module.exports = {
  2. serverNodeBuiltinsPolyfill: {
  3. modules: {
  4. path: true,
  5. fs: "empty",
  6. },
  7. },
  8. };

For reference, the complete set of default polyfills from v1 can be manually specified as follows:

  1. module.exports = {
  2. serverNodeBuiltinsPolyfill: {
  3. modules: {
  4. _stream_duplex: true,
  5. _stream_passthrough: true,
  6. _stream_readable: true,
  7. _stream_transform: true,
  8. _stream_writable: true,
  9. assert: true,
  10. "assert/strict": true,
  11. buffer: true,
  12. console: true,
  13. constants: true,
  14. crypto: "empty",
  15. diagnostics_channel: true,
  16. domain: true,
  17. events: true,
  18. fs: "empty",
  19. "fs/promises": "empty",
  20. http: true,
  21. https: true,
  22. module: true,
  23. os: true,
  24. path: true,
  25. "path/posix": true,
  26. "path/win32": true,
  27. perf_hooks: true,
  28. process: true,
  29. punycode: true,
  30. querystring: true,
  31. stream: true,
  32. "stream/promises": true,
  33. "stream/web": true,
  34. string_decoder: true,
  35. sys: true,
  36. timers: true,
  37. "timers/promises": true,
  38. tty: true,
  39. url: true,
  40. util: true,
  41. "util/types": true,
  42. vm: true,
  43. wasi: true,
  44. worker_threads: true,
  45. zlib: true,
  46. },
  47. },
  48. };

remix dev

For configuration options, see the remix dev docs.

remix-serve

If you are using the Remix App Server (remix-serve), enable v2_dev:

  1. module.exports = {
  2. future: {
  3. v2_dev: true,
  4. },
  5. };

That’s it!

Custom app server

If you are using your own app server (server.js), then check out our templates for examples of how to integrate with v2_dev or follow these steps:

  1. Enable v2_dev:
  1. module.exports = {
  2. future: {
  3. v2_dev: true,
  4. },
  5. };
  1. Update scripts in package.json:
  • Replace any remix watch with remix dev
  • Remove redundant NODE_ENV=development
  • Use -c / --command to run your app server

For example:

  1. {
  2. "scripts": {
  3. - "dev:remix": "cross-env NODE_ENV=development remix watch",
  4. - "dev:server": "cross-env NODE_ENV=development node ./server.js"
  5. + "dev": "remix dev -c 'node ./server.js'",
  6. }
  7. }
  1. Send a “ready” message to the Remix compiler once your app is running
  1. import { broadcastDevReady } from "@remix-run/node";
  2. // import { logDevReady } from "@remix-run/cloudflare" // use `logDevReady` if using CloudFlare
  3. const BUILD_DIR = path.join(process.cwd(), "build");
  4. // ... code setting up your server goes here ...
  5. const port = 3000;
  6. app.listen(port, async () => {
  7. console.log(`👉 http://localhost:${port}`);
  8. broadcastDevReady(await import(BUILD_DIR));
  9. });
  1. (Optional) --manual

If you were relying on require cache purging, you can keep doing so by using the --manual flag:

  1. remix dev --manual -c 'node ./server.js'

Check out the manual mode guide for more details.

Built-in PostCSS/Tailwind support

In v2, these tools will be automatically used within the Remix compiler if PostCSS and/or Tailwind configuration files are present in your project.

If you have a custom PostCSS and/or Tailwind setup outside of Remix that you’d like to maintain when migrating to v2, you can disable these features in your remix.config.js.

  1. module.exports = {
  2. postcss: false,
  3. tailwind: false,
  4. };