From f70c7222dd844257fab791fb4d5f8cf90e3361df Mon Sep 17 00:00:00 2001 From: Chris Villa Date: Fri, 22 Dec 2023 18:06:39 +0000 Subject: [PATCH] fix: don't render plugins twice when using React strict mode React strict mode causes the Puck component to be rendered twice. The existing implementation treated the overrides as mutable, which resulted in plugins currying themselves on the second render. This would in turn cause a hydration error when server rendering --- .../core/lib/__tests__/load-overrides.spec.tsx | 16 ++++++++++++++++ packages/core/lib/load-overrides.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/__tests__/load-overrides.spec.tsx b/packages/core/lib/__tests__/load-overrides.spec.tsx index b2fcb53d7b..0072e8fd79 100644 --- a/packages/core/lib/__tests__/load-overrides.spec.tsx +++ b/packages/core/lib/__tests__/load-overrides.spec.tsx @@ -49,4 +49,20 @@ describe("load-overrides", () => { "0 | 1 | 2 | 3" ); }); + + it("should avoid mutating the provided overrides", () => { + const overrides = {}; + const loaded = loadOverrides({ + overrides, + plugins: [ + { + overrides: { + fieldTypes: { text: ({ children }) => `${children} | 1` as any }, + }, + }, + ], + }); + + expect(overrides).toEqual({}); + }); }); diff --git a/packages/core/lib/load-overrides.ts b/packages/core/lib/load-overrides.ts index 52ac784ed8..8e32ee575d 100644 --- a/packages/core/lib/load-overrides.ts +++ b/packages/core/lib/load-overrides.ts @@ -8,7 +8,7 @@ export const loadOverrides = ({ overrides: Partial; plugins: Plugin[]; }) => { - const collected = overrides; + const collected = { ...overrides }; plugins.forEach((plugin) => { Object.keys(plugin.overrides).forEach((overridesType) => {