Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SSR support #637

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-glasses-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@craftjs/core': patch
---

SSR support
10 changes: 4 additions & 6 deletions packages/core/src/nodes/Element.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERROR_TOP_LEVEL_ELEMENT_NO_ID, useEffectOnce } from '@craftjs/utils';
import { ERROR_TOP_LEVEL_ELEMENT_NO_ID } from '@craftjs/utils';
import React, { useState } from 'react';
import invariant from 'tiny-invariant';

Expand Down Expand Up @@ -47,9 +47,7 @@ export function Element<T extends React.ElementType>({
},
}));

const [linkedNodeId, setLinkedNodeId] = useState<NodeId | null>(null);

useEffectOnce(() => {
const [linkedNodeId] = useState<NodeId | null>(() => {
invariant(!!id, ERROR_TOP_LEVEL_ELEMENT_NO_ID);
const { id: nodeId, data } = node;

tomas-c marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -77,9 +75,9 @@ export function Element<T extends React.ElementType>({
linkedNodeId = tree.rootNodeId;
actions.history.ignore().addLinkedNodeFromTree(tree, nodeId, id);
}

setLinkedNodeId(linkedNodeId);
return linkedNodeId;
}
return null;
});

return linkedNodeId ? <NodeElement id={linkedNodeId} /> : null;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/nodes/tests/Element.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ describe('<Element />', () => {
});

it('should call query.parseReactElement()', () => {
expect(parseReactElement).toHaveBeenCalledWith(
const arg0 = parseReactElement.mock.calls[0][0];
const arg0WithoutOwner = { ...arg0, _owner: null };
expect(arg0WithoutOwner).toEqual(
<Element {...elementProps}>{children}</Element>
);
});
tomas-c marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/render/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ export const Frame: React.FC<React.PropsWithChildren<FrameProps>> = ({
initialData: data || json,
});

const isInitialChildrenLoadedRef = useRef(false);
const isInitialLoadedRef = useRef(false);

useEffect(() => {
const { initialChildren, initialData } = initialState.current;
if (!isInitialLoadedRef.current && initialState.current.initialData) {
actions.history.ignore().deserialize(initialState.current.initialData);
isInitialLoadedRef.current = true;
}

if (initialData) {
actions.history.ignore().deserialize(initialData);
return;
}
useEffect(() => {
const { initialChildren } = initialState.current;
tomas-c marked this conversation as resolved.
Show resolved Hide resolved

// Prevent recreating Nodes from child elements if we already did it the first time
tomas-c marked this conversation as resolved.
Show resolved Hide resolved
// Usually an issue in React Strict Mode where this hook is called twice which results in orphaned Nodes
const isInitialChildrenLoaded = isInitialChildrenLoadedRef.current;
const isInitialLoaded = isInitialLoadedRef.current;

if (!initialChildren || isInitialChildrenLoaded) {
if (!initialChildren || isInitialLoaded) {
return;
}

Expand All @@ -72,7 +72,7 @@ export const Frame: React.FC<React.PropsWithChildren<FrameProps>> = ({
});

actions.history.ignore().addNodeTree(node);
isInitialChildrenLoadedRef.current = true;
isInitialLoadedRef.current = true;
}, [actions, query]);

return <RenderRootNode />;
Expand Down
Loading