Unverified Commit c11c48e4 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Replace useSearchParams bailout error with ReactDOM.browser behind a flag (#96843)

Stacked on #96826.

This PR replaces the bailout error used by `useSearchParams` with
`ReactDOM.browser()` behind the experimental flag.

### How?

Move the bailout behind a browser-safe module boundary, use
`ReactDOM.browser()` when enabled, and preserve existing behavior
otherwise.

<!-- NEXT_JS_LLM -->
parent 23985382
import React from 'react' import React from 'react'
import { browser } from 'react-dom'
import { import {
throwForMissingRequestStore, throwForMissingRequestStore,
workUnitAsyncStorage, workUnitAsyncStorage,
...@@ -10,6 +11,12 @@ import { ...@@ -10,6 +11,12 @@ import {
} from '../dynamic-rendering-utils' } from '../dynamic-rendering-utils'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { BailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' import { BailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { createReactBrowserBailoutReason } from '../../shared/lib/lazy-dynamic/react-browser-bailout'
const getUseSearchParamsBailoutReason = createReactBrowserBailoutReason.bind(
null,
'useSearchParams()'
)
// TODO(veil): This module is separated from `dynamic-rendering.ts` as a workaround. // TODO(veil): This module is separated from `dynamic-rendering.ts` as a workaround.
// When these hooks were part of `dynamic-rendering.ts, the source location of these // When these hooks were part of `dynamic-rendering.ts, the source location of these
...@@ -101,7 +108,13 @@ export function useDynamicSearchParams(expression: string) { ...@@ -101,7 +108,13 @@ export function useDynamicSearchParams(expression: string) {
if (workStore.forceStatic) { if (workStore.forceStatic) {
return return
} }
throw new BailoutToCSRError(expression) if (process.env.__NEXT_EXPERIMENTAL_REACT_BROWSER_BAILOUT) {
// @ts-expect-error TODO: Update @types/react-dom to include the reason argument.
React.use(browser(getUseSearchParamsBailoutReason))
return
} else {
throw new BailoutToCSRError(expression)
}
} }
case 'prerender': case 'prerender':
case 'prerender-runtime': case 'prerender-runtime':
......
...@@ -33,6 +33,9 @@ describe('missing-suspense-with-csr-bailout', () => { ...@@ -33,6 +33,9 @@ describe('missing-suspense-with-csr-bailout', () => {
const { exitCode } = await next.build() const { exitCode } = await next.build()
expect(exitCode).toBe(1) expect(exitCode).toBe(1)
expect(next.cliOutput).toContain(message) expect(next.cliOutput).toContain(message)
expect(next.cliOutput).not.toContain(
'The server render could not complete because client rendering was requested outside a Suspense boundary'
)
// Can show the trace where the searchParams hook is used // Can show the trace where the searchParams hook is used
// TODO: This path is different for Turbopack. Builds need to have sourcemaps support. // TODO: This path is different for Turbopack. Builds need to have sourcemaps support.
if (!process.env.IS_TURBOPACK_TEST) { if (!process.env.IS_TURBOPACK_TEST) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment