From 178c152b2c187b9f1e4a01500af3f95c40463d8a Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 2 Apr 2024 20:04:27 +0530 Subject: [PATCH] fix: pages and app directory - `Uncaught Error: DYNAMIC_SERVER_USAGE` (#4318) * fix stray DYNAMIC_SERVER_USAGE thrown * cleanup * chore: changeset --- .changeset/swift-owls-admire.md | 5 ++++ packages/blitz/src/cli/utils/codegen-tasks.ts | 25 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 .changeset/swift-owls-admire.md diff --git a/.changeset/swift-owls-admire.md b/.changeset/swift-owls-admire.md new file mode 100644 index 0000000000..d7eea832b1 --- /dev/null +++ b/.changeset/swift-owls-admire.md @@ -0,0 +1,5 @@ +--- +"blitz": patch +--- + +fix: patch next.js to hide intentional throws of `DYNAMIC_SERVER_USAGE` diff --git a/packages/blitz/src/cli/utils/codegen-tasks.ts b/packages/blitz/src/cli/utils/codegen-tasks.ts index e991cc92dc..e07d132cd1 100644 --- a/packages/blitz/src/cli/utils/codegen-tasks.ts +++ b/packages/blitz/src/cli/utils/codegen-tasks.ts @@ -16,7 +16,14 @@ export const codegenTasks = async () => { */ const nextDir = await resolveCwd("next") const nextClientIndex = join(nextDir, "../..", "client", "index.js") + const nextClientOnRecoverableErrorIndex = join( + nextDir, + "../..", + "client", + "on-recoverable-error.js", + ) const readFile = await fs.readFile(nextClientIndex) + const readOnRecoverableErrorFile = await fs.readFile(nextClientOnRecoverableErrorIndex) const packageJson = await getPackageJson() const version = packageJson.dependencies.next const nextVersion = semver.clean(version, {loose: true}) || semver.valid(semver.coerce(version)) @@ -48,14 +55,16 @@ export const codegenTasks = async () => { ) await fs.writeFile(nextClientIndex, updatedFile) log.success("Next.js was successfully patched with a React Suspense fix") - } else if (nextVersion && semver.satisfies(nextVersion, ">=13.3.1")) { - const updatedFile = readFile - .toString() - .replace( - /_onrecoverableerror\.default$/gm, - `(err) => (err.toString().includes("DYNAMIC_SERVER_USAGE") || err.toString().includes("could not finish this Suspense boundary") || err.toString().includes("Minified React error #419")) ? null : _onrecoverableerror.default(err)`, - ) - await fs.writeFile(nextClientIndex, updatedFile) + } else { + const updatedFile = readOnRecoverableErrorFile.toString().replace( + /defaultOnRecoverableError\(err\);/gm, + ` if (err.toString().includes("DYNAMIC_SERVER_USAGE") || err.toString().includes("could not finish this Suspense boundary") || err.toString().includes("Minified React error #419")) { + return; + } else { + defaultOnRecoverableError(err); + }`, + ) + await fs.writeFile(nextClientOnRecoverableErrorIndex, updatedFile) log.success("Next.js was successfully patched with a React Suspense fix") } } catch (err) {