Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: importing routes.ts into a route causes RR7 to throw error: "this is a bug with react router - please file an issue!" #12392

Open
ryanxcharles opened this issue Nov 25, 2024 · 4 comments

Comments

@ryanxcharles
Copy link

What version of React Router are you using?

7

Steps to Reproduce

import your routes.ts file into a route. this circularity seems to cause an issue with RR7. it throws an error in production (but not in dev - so it's a build issue?). i use flat routes currently, maybe that has something to do with it.

Expected Behavior

you should be able to include routes.ts in a route. or, if not, it should give you an intelligent warning or error.

Actual Behavior

this error is thrown:

The following error is a bug in React Router; please open an issue! https://github.com/remix-run/react-router/issues/new/choose

detailed server log:

2024-11-25T19:44:27.238 app[9185e294c5ed78] iad [info] The following error is a bug in React Router; please open an issue! https://github.com/remix-run/react-router/issues/new/choose

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] /app/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_react@1_xrt5j6my7kk7o6272atwjzf56q/node_modules/@react-router/dev/dist/routes.js:63

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] throw new Error(message);

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] ^

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] Error

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at invariant (/app/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_react@1_xrt5j6my7kk7o6272atwjzf56q/node_modules/@react-router/dev/dist/routes.js:63:11)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at getAppDirectory (/app/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_react@1_xrt5j6my7kk7o6272atwjzf56q/node_modules/@react-router/dev/dist/routes.js:69:3)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at flatRoutes2 (/app/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_react-router@_2kjjmyj2jbidpkkbyhpj354c6m/node_modules/@react-router/fs-routes/dist/index.js:464:56)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at file:///app/www-internetkyc-com/build/app/server/index.js:116:1

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at ModuleJob.run (node:internal/modules/esm/module_job:268:25)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:543:26)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at async getAppBuild (file:///app/www-internetkyc-com/build/server/server/server-express.js:46:17)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at async build (file:///app/www-internetkyc-com/build/server/server/server-express.js:58:34)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at async requestHandler (/app/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/react-router/dist/production/index.js:9111:44)

2024-11-25T19:44:27.292 app[9185e294c5ed78] iad [info] at async /app/node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]___p4tyv7g2rvexoe7porl6mu2m5u/node_modules/@react-router/express/dist/index.js:50:22
@ryanxcharles
Copy link
Author

By the way, in case it's not obvious, the reason why I imported my routes.ts file into a route was because I added some route-related code into there which I used in my routes. I had to put that code into a different file to fix the issue.

@timdorr
Copy link
Member

timdorr commented Nov 26, 2024

Can you give an example of the code you're exporting from the routes.ts file? It sounds to me like something that should be imported into routes.ts, not exported from it. That would break the circular dependency.

This is what I think you're doing and how I might implement it:

/// routes/config.ts

import { route } from "@react-router/dev/routes"

function generateRoute(path, metadata) {
  return {
    config: (options, children) => route(path, `pages/${path}.tsx`, options, children)
    metadata
  }
}

export const fooRoute = generateRoute('foo', { bar: 'baz' } )
export const barRoute = generateRoute('bar', { bif: 'buz' } )
// More route configs below

/// routes.ts
import type { RouteConfig } from "@react-router/dev/routes"

import { fooRoute, barRoute } from './routes/config.ts'

export default [
  fooRoute.config({}, 
	barRoute.config()
  )
)
] satisfies RouteConfig;

/// other-module.ts

import { fooRoute, barRoute } from './routes/config.ts'

const metadatas = [fooRoute.metadata, barRoute.metadata]

@pcattori
Copy link
Contributor

This is expected routes.ts is a configuration file that is run during development and build before any app code is executed. It is part of the configuration environment, not the app environment and so cannot be referenced by app code.

@markdalgleish we could check in the Vite plugin if routes.ts (or react-router.config.ts for that matter) are being resolved and then log a more specific, user-facing error.

@ryanxcharles
Copy link
Author

@timdorr In my quest to implement type-safe Link, I had code like this is my routes.ts:

type Paths = {
  "/": undefined;
  "/home": undefined;
  "/signin": undefined;
  "/signup": undefined;
  "/delete": undefined;
  "/user-challenge": undefined;
  "/user-name": undefined;
  "/user-avatar": undefined;
};

export function $path(path: keyof Paths, params?: Record<string, string>) {
  const pathParts = path.split("/");
  const newPathParts = pathParts.map((part) => {
    if (part.startsWith(":")) {
      const paramName = part.slice(1);
      return params?.[paramName] ?? part;
    }
    return part;
  });
  return newPathParts.join("/");
}

This is the start my new app. I am describing all routes, and exporting that so that I can import it in my routes to use the $path function to have type-safe links. The issue was fixed by moving this code from routes.ts to path.ts. In the future, routes.ts will import the same path.ts file so that I have one place to describe all paths for type-safe links.

@pcattori Yes I think simply having a specific error message such as "You have a circular routes file. Try putting your extra code in a new file instead of importing routes."

Also, it would be helpful if this error occurred in development. The real problem is that I didn't notice the error until I deployed to production and it crashed my app, leaving me to debug in production while the app was down. Getting an explanatory error in development as soon as I tried to put my routes.ts file in a route would have saved my app from dying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants