From d5b8faa860e95eebfe6a101daab7c73e5bd7ed5d Mon Sep 17 00:00:00 2001 From: Iagor Moraes <13892132+iagormoraes@users.noreply.github.com> Date: Wed, 28 Dec 2022 15:12:33 -0300 Subject: [PATCH] React Suspense on Next 13 patch fix (#4009) * fix: add regex to support inline and non-inline codebase - add proper regex test for package version * chore: add changelog * chore: update lock file Co-authored-by: Dillon Raphael --- .changeset/three-toes-sell.md | 5 +++++ packages/blitz/src/cli/utils/codegen-tasks.ts | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changeset/three-toes-sell.md diff --git a/.changeset/three-toes-sell.md b/.changeset/three-toes-sell.md new file mode 100644 index 0000000000..d4e30479c9 --- /dev/null +++ b/.changeset/three-toes-sell.md @@ -0,0 +1,5 @@ +--- +"blitz": patch +--- + +add regex to support inline and non-inline codebase and proper next.js package version check diff --git a/packages/blitz/src/cli/utils/codegen-tasks.ts b/packages/blitz/src/cli/utils/codegen-tasks.ts index e2003a6965..cf558cb73d 100644 --- a/packages/blitz/src/cli/utils/codegen-tasks.ts +++ b/packages/blitz/src/cli/utils/codegen-tasks.ts @@ -9,7 +9,7 @@ import {runPrisma} from "../../utils/run-prisma" import resolveFrom from "resolve-from" export const codegenTasks = async () => { try { - /* + /* Updates the user's nextjs file and adds onRecoverableError to the hydrateRoot 3rd parameter object. We can remove this when https://github.com/vercel/next.js/pull/38207 is merged into next.js */ @@ -18,7 +18,7 @@ export const codegenTasks = async () => { const readFile = await fs.readFile(nextClientIndex) const packageJson = await getPackageJson() const nextVersion = packageJson.dependencies.next - if (nextVersion && nextVersion.startsWith("12")) { + if (nextVersion && /^([~^])?12/.test(nextVersion)) { const updatedFile = readFile .toString() .replace( @@ -27,11 +27,11 @@ export const codegenTasks = async () => { ) await fs.writeFile(nextClientIndex, updatedFile) log.success("Next.js was successfully patched with a React Suspense fix") - } else if (nextVersion && nextVersion.startsWith("13")) { + } else if (nextVersion && /^([~^])?13/.test(nextVersion)) { const updatedFile = readFile .toString() .replace( - /_client.default\.hydrateRoot\(.*?\);/, + /_client\.default\.hydrateRoot\(.*?\{?[\s\S]*?}?\);/, `_client.default.hydrateRoot(domEl, reactEl, {onRecoverableError: (err) => (err.toString().includes("could not finish this Suspense boundary") || err.toString().includes("Minified React error #419")) ? null : console.error(err)});`, ) await fs.writeFile(nextClientIndex, updatedFile)