-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "feat(app-router): introduce `experimental.missingSuspenseWit…
…hCSRBailout` flag" (#60508) (#60751) This reapplies the `experimental.missingSuspenseWithCSRBailout` option to bail out during build if there was a missing suspense boundary when using something that bails out to client side rendering (like `useSearchParams()`). See #57642 Closes [NEXT-1770](https://linear.app/vercel/issue/NEXT-1770)
- Loading branch information
Showing
41 changed files
with
625 additions
and
430 deletions.
There are no files selected for viewing
This file contains 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,15 @@ | ||
--- | ||
title: Missing Suspense with CSR Bailout | ||
--- | ||
|
||
#### Why This Error Occurred | ||
|
||
Certain methods like `useSearchParams()` opt Next.js into client-side rendering. Without a suspense boundary, this will opt the entire page into client-side rendering, which is likely not intended. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Make sure that the method is wrapped in a suspense boundary. This way Next.js will only opt the component into client-side rendering up to the suspense boundary. | ||
|
||
### Useful Links | ||
|
||
- [`useSearchParams`](https://nextjs.org/docs/app/api-reference/functions/use-search-params) |
13 changes: 5 additions & 8 deletions
13
packages/next/src/client/components/bailout-to-client-rendering.ts
This file contains 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,14 +1,11 @@ | ||
import { throwWithNoSSR } from '../../shared/lib/lazy-dynamic/no-ssr-error' | ||
import { BailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' | ||
import { staticGenerationAsyncStorage } from './static-generation-async-storage.external' | ||
|
||
export function bailoutToClientRendering(): void | never { | ||
export function bailoutToClientRendering(reason: string): void | never { | ||
const staticGenerationStore = staticGenerationAsyncStorage.getStore() | ||
|
||
if (staticGenerationStore?.forceStatic) { | ||
return | ||
} | ||
if (staticGenerationStore?.forceStatic) return | ||
|
||
if (staticGenerationStore?.isStaticGeneration) { | ||
throwWithNoSSR() | ||
} | ||
if (staticGenerationStore?.isStaticGeneration) | ||
throw new BailoutToCSRError(reason) | ||
} |
19 changes: 16 additions & 3 deletions
19
packages/next/src/client/components/hooks-server-context.ts
This file contains 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,9 +1,22 @@ | ||
export const DYNAMIC_ERROR_CODE = 'DYNAMIC_SERVER_USAGE' | ||
const DYNAMIC_ERROR_CODE = 'DYNAMIC_SERVER_USAGE' | ||
|
||
export class DynamicServerError extends Error { | ||
digest: typeof DYNAMIC_ERROR_CODE = DYNAMIC_ERROR_CODE | ||
|
||
constructor(type: string) { | ||
super(`Dynamic server usage: ${type}`) | ||
constructor(public readonly description: string) { | ||
super(`Dynamic server usage: ${description}`) | ||
} | ||
} | ||
|
||
export function isDynamicServerError(err: unknown): err is DynamicServerError { | ||
if ( | ||
typeof err !== 'object' || | ||
err === null || | ||
!('digest' in err) || | ||
typeof err.digest !== 'string' | ||
) { | ||
return false | ||
} | ||
|
||
return err.digest === DYNAMIC_ERROR_CODE | ||
} |
This file contains 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 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 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 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 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 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,10 +1,10 @@ | ||
import { DYNAMIC_ERROR_CODE } from '../../client/components/hooks-server-context' | ||
import { isDynamicServerError } from '../../client/components/hooks-server-context' | ||
import { isNotFoundError } from '../../client/components/not-found' | ||
import { isRedirectError } from '../../client/components/redirect' | ||
import { isBailoutCSRError } from '../../shared/lib/lazy-dynamic/no-ssr-error' | ||
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' | ||
|
||
export const isDynamicUsageError = (err: any) => | ||
err.digest === DYNAMIC_ERROR_CODE || | ||
export const isDynamicUsageError = (err: unknown) => | ||
isDynamicServerError(err) || | ||
isBailoutToCSRError(err) || | ||
isNotFoundError(err) || | ||
isBailoutCSRError(err) || | ||
isRedirectError(err) |
This file contains 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 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 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 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 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.