Skip to content

Commit

Permalink
fix: don't render plugins twice when using React strict mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chrisvxd committed Dec 22, 2023
1 parent 7636e53 commit f70c722
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/core/lib/__tests__/load-overrides.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
});
});
2 changes: 1 addition & 1 deletion packages/core/lib/load-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const loadOverrides = ({
overrides: Partial<Overrides>;
plugins: Plugin[];
}) => {
const collected = overrides;
const collected = { ...overrides };

plugins.forEach((plugin) => {
Object.keys(plugin.overrides).forEach((overridesType) => {
Expand Down

0 comments on commit f70c722

Please sign in to comment.