Skip to content

Commit 6da2963

Browse files
ztannermischnic
authored andcommitted
legacyBehavior deprecation error should only trigger once (#77687)
We added a deprecation warning for `legacyBehavior` on the `Link` component but since we show errors in the error overlay, this could lead to a lot of noise if many links are using the `legacyBehavior` prop. This copies over the `warn-once` utility to `error-once` and uses that utility to dedupe.
1 parent e505d36 commit 6da2963

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

packages/next/src/client/app-dir/link.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../components/links'
2121
import { isLocalURL } from '../../shared/lib/router/utils/is-local-url'
2222
import { dispatchNavigateAction } from '../components/app-router-instance'
23+
import { errorOnce } from '../../shared/lib/utils/error-once'
2324

2425
type Url = string | UrlObject
2526
type RequiredKeys<T> = {
@@ -684,7 +685,7 @@ export default function LinkComponent(
684685

685686
if (legacyBehavior) {
686687
if (process.env.NODE_ENV === 'development') {
687-
console.error(
688+
errorOnce(
688689
'`legacyBehavior` is deprecated and will be removed in a future ' +
689690
'release. A codemod is available to upgrade your components:\n\n' +
690691
'npx @next/codemod@latest new-link .\n\n' +

packages/next/src/client/link.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useIntersection } from './use-intersection'
1818
import { getDomainLocale } from './get-domain-locale'
1919
import { addBasePath } from './add-base-path'
2020
import { useMergedRef } from './use-merged-ref'
21+
import { errorOnce } from '../shared/lib/utils/error-once'
2122

2223
type Url = string | UrlObject
2324
type RequiredKeys<T> = {
@@ -668,7 +669,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
668669

669670
if (legacyBehavior) {
670671
if (process.env.NODE_ENV === 'development') {
671-
console.error(
672+
errorOnce(
672673
'`legacyBehavior` is deprecated and will be removed in a future ' +
673674
'release. A codemod is available to upgrade your components:\n\n' +
674675
'npx @next/codemod@latest new-link .\n\n' +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let errorOnce = (_: string) => {}
2+
if (process.env.NODE_ENV !== 'production') {
3+
const errors = new Set<string>()
4+
errorOnce = (msg: string) => {
5+
if (!errors.has(msg)) {
6+
console.error(msg)
7+
}
8+
errors.add(msg)
9+
}
10+
}
11+
12+
export { errorOnce }

test/e2e/app-dir/link-component-deprecations/app/page.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import Link from 'next/link'
22

33
export default function HomePage() {
44
return (
5-
<Link legacyBehavior href="/target-page">
6-
<a>Target page</a>
7-
</Link>
5+
<>
6+
<Link legacyBehavior href="/target-page">
7+
<a>Target page</a>
8+
</Link>
9+
<Link legacyBehavior href="/target-page?foo=bar">
10+
<a>Another Target page</a>
11+
</Link>
12+
</>
813
)
914
}

test/e2e/app-dir/link-component-deprecations/link-component-deprecations.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ describe('Link component deprecations', () => {
99
const browser = await next.browser('/')
1010
const logs = await browser.log()
1111

12-
const didWarn = logs.some(
12+
const errors = logs.filter(
1313
(log) =>
1414
log.source === 'error' &&
1515
log.message.includes(
1616
'`legacyBehavior` is deprecated and will be removed in a future release.'
1717
)
1818
)
1919

20-
expect(didWarn).toBe(isNextDev)
20+
expect(errors.length).toBe(isNextDev ? 1 : 0)
2121
})
2222
})

0 commit comments

Comments
 (0)