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

✨ [RUM-5500] React-router v7 support #3299

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

RomanGaignault
Copy link
Contributor

@RomanGaignault RomanGaignault commented Jan 24, 2025

Motivation

Add support for react-router v7 for the rum-react plugin.

Changes

Reuse as much as possible the code from react-router-6, adapt unit test instead of cloning them

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@RomanGaignault RomanGaignault requested a review from a team as a code owner January 24, 2025 10:04
Copy link

cit-pr-commenter bot commented Jan 24, 2025

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 145.98 KiB 145.98 KiB 0 B 0.00%
Logs 51.06 KiB 51.06 KiB 0 B 0.00%
Rum Slim 104.77 KiB 104.77 KiB 0 B 0.00%
Worker 24.50 KiB 24.50 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base Average Cpu Time (ms) Local Average Cpu Time (ms) 𝚫
addglobalcontext 0.003 0.002 -0.001
addaction 0.039 0.056 0.017
addtiming 0.001 0.001 0.000
adderror 0.046 0.060 0.014
startstopsessionreplayrecording 0.008 0.026 0.018
startview 0.414 0.523 0.109
logmessage 0.022 0.030 0.008
🧠 Memory Performance
Action Name Base Consumption Memory (bytes) Local Consumption Memory (bytes) 𝚫 (bytes)
addglobalcontext 27.33 KiB 28.46 KiB 1.12 KiB
addaction 54.12 KiB 56.95 KiB 2.83 KiB
addtiming 26.15 KiB 25.69 KiB -466 B
adderror 59.09 KiB 60.51 KiB 1.42 KiB
startstopsessionreplayrecording 24.90 KiB 27.00 KiB 2.10 KiB
startview 413.20 KiB 422.75 KiB 9.55 KiB
logmessage 60.18 KiB 64.74 KiB 4.56 KiB

🔗 RealWorld

@codecov-commenter
Copy link

codecov-commenter commented Jan 24, 2025

Codecov Report

Attention: Patch coverage is 96.49123% with 2 lines in your changes missing coverage. Please review.

Project coverage is 93.70%. Comparing base (bfffdaf) to head (55599e3).

Files with missing lines Patch % Lines
...rum-react/src/domain/reactRouterV7/createRouter.ts 86.66% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3299   +/-   ##
=======================================
  Coverage   93.70%   93.70%           
=======================================
  Files         290      294    +4     
  Lines        7652     7708   +56     
  Branches     1745     1758   +13     
=======================================
+ Hits         7170     7223   +53     
- Misses        482      485    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +41 to +42
"react-router-6": "npm:[email protected]",
"react-router-7": "npm:[email protected]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 suggestion:

Suggested change
"react-router-6": "npm:[email protected]",
"react-router-7": "npm:[email protected]"
"react-router-dom-6": "npm:[email protected]",
"react-router-dom-7": "npm:[email protected]"

Comment on lines +1 to +28
import {
createBrowserRouter as originalCreateBrowserRouter,
createHashRouter as originalCreateHashRouter,
createMemoryRouter as originalCreateMemoryRouter,
} from 'react-router-7'
import { startReactRouterView } from './startReactRouterView'

type Router = ReturnType<typeof originalCreateBrowserRouter>

export const createBrowserRouter: typeof originalCreateBrowserRouter = (routes, options) =>
registerRouter(originalCreateBrowserRouter(routes, options))
export const createHashRouter: typeof originalCreateHashRouter = (routes, options) =>
registerRouter(originalCreateHashRouter(routes, options))
export const createMemoryRouter: typeof originalCreateMemoryRouter = (routes, options) =>
registerRouter(originalCreateMemoryRouter(routes, options))

export function registerRouter(router: Router) {
let location = router.state.location.pathname
router.subscribe((routerState) => {
const newPathname = routerState.location.pathname
if (location !== newPathname) {
startReactRouterView(routerState.matches)
location = newPathname
}
})
startReactRouterView(router.state.matches)
return router
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 suggestion: ‏To help maintenance, we need to avoid code duplication as much as possible.
What about having a structure like this:

domain
  reactRouter
    startReactRouterView.ts
    useRoutes.ts
    createRouterts
    routesComponent.ts
entries
  reactRouterV6.ts
  reactRouterV7.ts

Entry files would look like:

import { wrapCreateRouter } from '../domain/reactRouter/createRouter'
import { createRoutesComponent } from '../domain/reactRouter/routesComponent'
import {
  createBrowserRouter as originalCreateBrowserRouter,
  createHashRouter as originalCreateHashRouter,
  createMemoryRouter as originalCreateMemoryRouter,
  useRoutes as originalUseRoutes,
  useLocation,
} from 'react-router-7'

export const createBrowserRouter = wrapCreateRouter(originalCreateBrowserRouter)
export const createHashRouter = wrapCreateRouter(originalCreateHashRouter)
export const createMemoryRouter = wrapCreateRouter(originalCreateMemoryRouter)
export const useRoutes = wrapUseRoutes({ useRoutes: originalUseRoutes, useLocation, ... })
export const Routes = createRoutesComponent(useRoutes)

And domain files would look like (types might be a bit harder to get well, let's see):

export function wrapCreateRouter(originalCreateRouter) {
  return (routes, options) => registerRouter(originalCreateRouter(routes, options))
}

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

Successfully merging this pull request may close these issues.

3 participants