-
Notifications
You must be signed in to change notification settings - Fork 190
🐛 Prevent duplicate Next.js RUM views from discarded renders #4940
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
Open
BeltranBulbarellaDD
wants to merge
1
commit into
main
Choose a base branch
from
beltran.bulbarella/next_js_render_issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−24
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 2 additions & 8 deletions
10
packages/browser-rum-nextjs/src/domain/nextJSRouter/datadogAppRouter.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,14 @@ | ||
| 'use client' | ||
|
|
||
| import { useRef } from 'react' | ||
| import { usePathname, useParams } from 'next/navigation' | ||
| import { mockable } from '@datadog/browser-core' | ||
| import { startNextjsView } from '../nextjsPlugin' | ||
| import { computeViewNameFromParams } from './computeViewNameFromParams' | ||
| import { useStartNextjsView } from './useStartNextjsView' | ||
|
|
||
| export function DatadogAppRouter() { | ||
| const pathname = mockable(usePathname)() | ||
| const params = mockable(useParams)() | ||
| const previousPathname = mockable(useRef)<string | null>(null) | ||
|
|
||
| if (previousPathname.current !== pathname) { | ||
| previousPathname.current = pathname | ||
| startNextjsView(computeViewNameFromParams(pathname, params)) | ||
| } | ||
| useStartNextjsView(pathname, computeViewNameFromParams(pathname, params)) | ||
|
|
||
| return null | ||
| } |
21 changes: 6 additions & 15 deletions
21
packages/browser-rum-nextjs/src/domain/nextJSRouter/datadogPagesRouter.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,17 @@ | ||
| import { useRef } from 'react' | ||
| import { useRouter } from 'next/router' | ||
| import { mockable } from '@datadog/browser-core' | ||
| import { startNextjsView } from '../nextjsPlugin' | ||
| import { useStartNextjsView } from './useStartNextjsView' | ||
|
|
||
| export function DatadogPagesRouter() { | ||
| const router = mockable(useRouter)() | ||
| const previousPath = mockable(useRef)<string | null>(null) | ||
|
|
||
| if (!router.isReady) { | ||
| return null | ||
| } | ||
|
|
||
| // Extract the path portion of asPath (without query params or hash) to detect navigations. | ||
| const path = router.asPath.split(/[?#]/)[0] | ||
| const path = router.isReady ? router.asPath.split(/[?#]/)[0] : null | ||
|
|
||
| if (previousPath.current !== path) { | ||
| // router.pathname is the route pattern (e.g., "/user/[id]") — used as the view name | ||
| // router.asPath is the actual URL (e.g., "/user/42") — used to detect navigations between | ||
| // different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43) | ||
| previousPath.current = path | ||
| startNextjsView(router.pathname) | ||
| } | ||
| // router.pathname is the route pattern (e.g., "/user/[id]") — used as the view name | ||
| // router.asPath is the actual URL (e.g., "/user/42") — used to detect navigations between | ||
| // different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43) | ||
| useStartNextjsView(path, router.pathname) | ||
|
|
||
| return null | ||
| } |
75 changes: 75 additions & 0 deletions
75
packages/browser-rum-nextjs/src/domain/nextJSRouter/useStartNextjsView.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import React, { act } from 'react' | ||
| import { appendComponent } from '../../../../browser-rum-react/test/appendComponent' | ||
| import { initReactOldBrowsersSupport } from '../../../../browser-rum-react/test/reactOldBrowsersSupport' | ||
| import { initializeNextjsPlugin } from '../../../test/initializeNextjsPlugin' | ||
| import { useStartNextjsView } from './useStartNextjsView' | ||
|
|
||
| describe('useStartNextjsView', () => { | ||
| beforeEach(() => { | ||
| initReactOldBrowsersSupport() | ||
| }) | ||
|
|
||
| it('starts a single view when React renders the component twice in Strict Mode', () => { | ||
| const startViewSpy = jasmine.createSpy() | ||
| initializeNextjsPlugin({ publicApi: { startView: startViewSpy } }) | ||
|
|
||
| function TestRouter() { | ||
| useStartNextjsView('/user/42', '/user/[id]') | ||
| return null | ||
| } | ||
|
|
||
| appendComponent( | ||
| <React.StrictMode> | ||
| <TestRouter /> | ||
| </React.StrictMode> | ||
| ) | ||
|
|
||
| expect(startViewSpy).toHaveBeenCalledOnceWith({ name: '/user/[id]', url: undefined }) | ||
| }) | ||
|
|
||
| it('starts a view when the path changes', () => { | ||
| const startViewSpy = jasmine.createSpy() | ||
| initializeNextjsPlugin({ publicApi: { startView: startViewSpy } }) | ||
|
|
||
| let setRoute: (route: { path: string; viewName: string }) => void | ||
|
|
||
| function TestRouter() { | ||
| const [route, setCurrentRoute] = React.useState({ path: '/', viewName: '/' }) | ||
| setRoute = setCurrentRoute | ||
| useStartNextjsView(route.path, route.viewName) | ||
| return null | ||
| } | ||
|
|
||
| appendComponent(<TestRouter />) | ||
| startViewSpy.calls.reset() | ||
|
|
||
| act(() => { | ||
| setRoute({ path: '/user/42', viewName: '/user/[id]' }) | ||
| }) | ||
|
|
||
| expect(startViewSpy).toHaveBeenCalledOnceWith({ name: '/user/[id]', url: undefined }) | ||
| }) | ||
|
|
||
| it('does not start a view until the router is ready', () => { | ||
| const startViewSpy = jasmine.createSpy() | ||
| initializeNextjsPlugin({ publicApi: { startView: startViewSpy } }) | ||
|
|
||
| let setPath: (path: string | null) => void | ||
|
|
||
| function TestRouter() { | ||
| const [path, setCurrentPath] = React.useState<string | null>(null) | ||
| setPath = setCurrentPath | ||
| useStartNextjsView(path, '/user/[id]') | ||
| return null | ||
| } | ||
|
|
||
| appendComponent(<TestRouter />) | ||
| expect(startViewSpy).not.toHaveBeenCalled() | ||
|
|
||
| act(() => { | ||
| setPath('/user/42') | ||
| }) | ||
|
|
||
| expect(startViewSpy).toHaveBeenCalledOnceWith({ name: '/user/[id]', url: undefined }) | ||
| }) | ||
| }) |
13 changes: 13 additions & 0 deletions
13
packages/browser-rum-nextjs/src/domain/nextJSRouter/useStartNextjsView.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { useLayoutEffect, useRef } from 'react' | ||
| import { startNextjsView } from '../nextjsPlugin' | ||
|
|
||
| export function useStartNextjsView(path: string | null, viewName: string) { | ||
| const previousPath = useRef<string | null>(null) | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (path !== null && previousPath.current !== path) { | ||
| previousPath.current = path | ||
| startNextjsView(viewName) | ||
| } | ||
| }, [path, viewName]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use client' | ||
|
|
||
| let renderAttempt = 0 | ||
| let suspendPromise: Promise<void> | undefined | ||
|
|
||
| export function DiscardedRenderProbe() { | ||
| if (typeof window === 'undefined' || !new URLSearchParams(window.location.search).has('discard-nextjs-render')) { | ||
| return null | ||
| } | ||
|
|
||
| renderAttempt += 1 | ||
|
|
||
| if (renderAttempt === 1) { | ||
| suspendPromise = new Promise((resolve) => { | ||
| setTimeout(resolve) | ||
| }) | ||
| throw suspendPromise | ||
| } | ||
|
|
||
| return <span data-testid="discarded-render-probe-ready" hidden /> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question:
Why not just
useEffect()? We're blocking the render by usinguseLayoutEffect(), which is not that I would expect from a monitoring toolUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is that
useLayoutEffectruns before paint and just by doing this fix we are going to be starting views later that what we would like to. useEffect would be just too late to start a view.