Description
I'm using React Router as a...
framework
Reproduction
Context
First, here's some context on why we're running into this issue:
We're migrating an application to React Router 7's new framework features. We've already migrated from v6 to v7, and our app is currently using createBrowserRouter to set up React Router on the client.
Our application routes are currently split up within a monorepo, and teams work on route groups in isolated projects. This allows us to easily split and cache parts of our builds, typechecks, tests, and other tasks across projects-- speeding up our CI and local dev experience. Groups of routes are imported into our main application as an npm package. For example:
import { teamARoutes } from 'team-a-routes'
import { teamBRoutes } from 'team-b-routes'
const router = createBrowserRouter([...teamARoutes, ...teamBRoutes])
We'd now like to migrate away from using createBrowserRouter
to define our routes, and into a routes.ts file in order to start using more framework features. We'd like to import route definitions in a similar way that we used to in order to maintain our package structure and ability to split up project tasks. Something like:
// apps/my-app/app/routes.ts
import { teamARoutes } from 'team-a-routes';
import { teamBRoutes } from 'team-b-routes'
export default [
// Assuming a workspace structure with apps/ and libs/ as top level directories,
// imported route configs end up looking something like
// { path: "my-path", file: "../../../libs/team-a-routes/routes/my-route.jsx" }
...teamARoutes,
...teamBRoutes,
]
This works well with React Router at runtime. However, we've run into a an issue with the new typegen functionality when attempting this change. Specifically, route typings are generated outside the .react-router
directory when we try to load our route libraries' files in the routes.ts file, which means the types files end up in our project code and cause type checking failures.
Reproduction steps
You can find a reproduction repo here. The included README has steps to get set up and reproduce the issue.
System Info
System:
OS: macOS 15.3
CPU: (10) arm64 Apple M1 Max
Memory: 283.39 MB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.5.1 - ~/.nvm/versions/node/v20.5.1/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v20.5.1/bin/yarn
npm: 9.8.0 - ~/.nvm/versions/node/v20.5.1/bin/npm
pnpm: 9.12.1 - ~/.nvm/versions/node/v20.5.1/bin/pnpm
Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
Browsers:
Chrome: 132.0.6834.160
Safari: 18.3
Used Package Manager
pnpm
Expected Behavior
When provided route file paths are located in libraries, outside the current project's appDirectory
, I would expect React Router to either:
- Not generate types for those routes, since libraries can be type checked separately.
This seems like it could be a reasonable starting place to me, and resolves this issue well-enough in the short-term. We'd be happy to contribute this fix if it sounds good to you all!
or,
- Generate library routes' typings into the
.react-router
folder.
In the future, it could be nice for the main application to have access to imported route typings. However, this idea might require more thought on whether it can exist alongside the current generated types, or needs some special handling. This use case might also fit in somehow with the current work going on in draft: typesafety #12752? We'd be happy to help brainstorm further and contribute to this as well if helpful!
Actual Behavior
When provided route files that are located outside the project's appDirectory
, the typegen command generates files outside of .react-router
. Those files also contain invalid path references when trying to import the source route module file.
To illustrate, in the linked reproduction repo, you can observe that:
- The library's route module type definitions are generated into
apps/test-app/libs/route-library/+types/library-route.ts.
- This file is not located in the .react-router/types folder, and it also attempts to import the source file using
import("../library-route.js")
.../library-route.js
is not a valid path in the test-app project, and causestsc
typechecks to fail.