Skip to content

Commit a48680d

Browse files
authored
Don't warn on well-known properties in searchParams (#71142)
1 parent b524c36 commit a48680d

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

packages/next/src/server/request/search-params.ts

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
describeStringPropertyAccess,
2323
describeHasCheckingStringProperty,
2424
throwWithStaticGenerationBailoutErrorWithDynamicError,
25+
wellKnownProperties,
2526
} from './utils'
2627

2728
export type SearchParams = { [key: string]: string | string[] | undefined }
@@ -588,63 +589,38 @@ function makeDynamicallyTrackedExoticSearchParamsWithDevWarnings(
588589
})
589590

590591
Object.keys(underlyingSearchParams).forEach((prop) => {
591-
switch (prop) {
592-
// Object prototype
593-
case 'hasOwnProperty':
594-
case 'isPrototypeOf':
595-
case 'propertyIsEnumerable':
596-
case 'toString':
597-
case 'valueOf':
598-
case 'toLocaleString':
599-
600-
// Promise prototype
601-
// fallthrough
602-
case 'then':
603-
case 'catch':
604-
case 'finally':
605-
606-
// React Promise extension
607-
// fallthrough
608-
case 'status':
609-
610-
// Common tested properties
611-
// fallthrough
612-
case 'toJSON':
613-
case '$$typeof':
614-
case '__esModule': {
615-
// These properties cannot be shadowed because they need to be the
616-
// true underlying value for Promises to work correctly at runtime
617-
unproxiedProperties.push(prop)
618-
break
619-
}
620-
default: {
621-
proxiedProperties.add(prop)
622-
Object.defineProperty(promise, prop, {
623-
get() {
624-
return proxiedUnderlying[prop]
625-
},
626-
set(newValue) {
627-
Object.defineProperty(promise, prop, {
628-
value: newValue,
629-
writable: true,
630-
enumerable: true,
631-
})
632-
},
633-
enumerable: true,
634-
configurable: true,
635-
})
636-
}
592+
if (wellKnownProperties.has(prop)) {
593+
// These properties cannot be shadowed because they need to be the
594+
// true underlying value for Promises to work correctly at runtime
595+
unproxiedProperties.push(prop)
596+
} else {
597+
proxiedProperties.add(prop)
598+
Object.defineProperty(promise, prop, {
599+
get() {
600+
return proxiedUnderlying[prop]
601+
},
602+
set(newValue) {
603+
Object.defineProperty(promise, prop, {
604+
value: newValue,
605+
writable: true,
606+
enumerable: true,
607+
})
608+
},
609+
enumerable: true,
610+
configurable: true,
611+
})
637612
}
638613
})
639614

640615
const proxiedPromise = new Proxy(promise, {
641616
get(target, prop, receiver) {
642617
if (typeof prop === 'string') {
643618
if (
644-
// We are accessing a property that was proxied to the promise instance
645-
proxiedProperties.has(prop) ||
646-
// We are accessing a property that doesn't exist on the promise nor the underlying
647-
Reflect.has(target, prop) === false
619+
!wellKnownProperties.has(prop) &&
620+
(proxiedProperties.has(prop) ||
621+
// We are accessing a property that doesn't exist on the promise nor
622+
// the underlying searchParams.
623+
Reflect.has(target, prop) === false)
648624
) {
649625
const expression = describeStringPropertyAccess('searchParams', prop)
650626
warnForSyncAccess(store.route, expression)

packages/next/src/server/request/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,28 @@ export function throwWithStaticGenerationBailoutErrorWithDynamicError(
5353
`Route ${route} with \`dynamic = "error"\` couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
5454
)
5555
}
56+
57+
export const wellKnownProperties = new Set([
58+
'hasOwnProperty',
59+
'isPrototypeOf',
60+
'propertyIsEnumerable',
61+
'toString',
62+
'valueOf',
63+
'toLocaleString',
64+
65+
// Promise prototype
66+
// fallthrough
67+
'then',
68+
'catch',
69+
'finally',
70+
71+
// React Promise extension
72+
// fallthrough
73+
'status',
74+
75+
// Common tested properties
76+
// fallthrough
77+
'toJSON',
78+
'$$typeof',
79+
'__esModule',
80+
])

0 commit comments

Comments
 (0)