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

[Fiber] Support Suspense boundaries anywhere (excluding hydration) #32163

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
135 changes: 77 additions & 58 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,24 +798,37 @@ export function appendChildToContainer(
container: Container,
child: Instance | TextInstance,
): void {
let parentNode;
if (container.nodeType === COMMENT_NODE) {
parentNode = (container.parentNode: any);
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
parentNode.moveBefore(child, container);
} else {
parentNode.insertBefore(child, container);
let parentNode: Document | Element;
switch (container.nodeType) {
case COMMENT_NODE: {
parentNode = (container.parentNode: any);
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
parentNode.moveBefore(child, container);
} else {
parentNode.insertBefore(child, container);
}
return;
}
} else {
parentNode = container;
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
parentNode.moveBefore(child, null);
} else {
parentNode.appendChild(child);
case DOCUMENT_NODE: {
parentNode = (container: any).body;
break;
}
default: {
if (container.nodeName === 'HTML') {
parentNode = (container.ownerDocument.body: any);
} else {
parentNode = (container: any);
}
}
}
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
parentNode.moveBefore(child, null);
} else {
parentNode.appendChild(child);
}

// This container might be used for a portal.
// If something inside a portal is clicked, that click should bubble
// through the React tree. However, on Mobile Safari the click would
Expand Down Expand Up @@ -852,21 +865,35 @@ export function insertInContainerBefore(
child: Instance | TextInstance,
beforeChild: Instance | TextInstance | SuspenseInstance,
): void {
if (container.nodeType === COMMENT_NODE) {
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
(container.parentNode: any).moveBefore(child, beforeChild);
} else {
(container.parentNode: any).insertBefore(child, beforeChild);
let parentNode: Document | Element;
switch (container.nodeType) {
case COMMENT_NODE: {
parentNode = (container.parentNode: any);
break;
}
} else {
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
container.moveBefore(child, beforeChild);
} else {
container.insertBefore(child, beforeChild);
case DOCUMENT_NODE: {
const ownerDocument: Document = (container: any);
parentNode = (ownerDocument.body: any);
break;
}
default: {
if (container.nodeName === 'HTML') {
parentNode = (container.ownerDocument.body: any);
} else {
parentNode = (container: any);
}
}
}
if (supportsMoveBefore) {
// $FlowFixMe[prop-missing]: We've checked this with supportsMoveBefore.
parentNode.moveBefore(child, beforeChild);
} else {
parentNode.insertBefore(child, beforeChild);
}
}

export function isSingletonScope(type: string): boolean {
return type === 'head';
}

function createEvent(type: DOMEventName, bubbles: boolean): Event {
Expand Down Expand Up @@ -912,11 +939,22 @@ export function removeChildFromContainer(
container: Container,
child: Instance | TextInstance | SuspenseInstance,
): void {
if (container.nodeType === COMMENT_NODE) {
(container.parentNode: any).removeChild(child);
} else {
container.removeChild(child);
let parentNode: Document | Element;
switch (container.nodeType) {
case COMMENT_NODE:
parentNode = (container.parentNode: any);
break;
case DOCUMENT_NODE:
parentNode = (container: any).body;
break;
default:
if (container.nodeName === 'HTML') {
parentNode = (container.ownerDocument.body: any);
} else {
parentNode = (container: any);
}
}
parentNode.removeChild(child);
}

export function clearSuspenseBoundary(
Expand Down Expand Up @@ -964,10 +1002,15 @@ export function clearSuspenseBoundaryFromContainer(
): void {
if (container.nodeType === COMMENT_NODE) {
clearSuspenseBoundary((container.parentNode: any), suspenseInstance);
} else if (container.nodeType === ELEMENT_NODE) {
clearSuspenseBoundary((container: any), suspenseInstance);
} else if (container.nodeType === DOCUMENT_NODE) {
clearSuspenseBoundary((container: any).body, suspenseInstance);
} else if (container.nodeName === 'HTML') {
clearSuspenseBoundary(
(container.ownerDocument.body: any),
suspenseInstance,
);
} else {
// Document nodes should never contain suspense boundaries.
clearSuspenseBoundary((container: any), suspenseInstance);
}
// Retry if any event replaying was blocked on this.
retryIfBlockedOn(container);
Expand Down Expand Up @@ -2297,30 +2340,6 @@ export function releaseSingletonInstance(instance: Instance): void {
detachDeletedInstance(instance);
}

export function clearSingleton(instance: Instance): void {
const element: Element = (instance: any);
let node = element.firstChild;
while (node) {
const nextNode = node.nextSibling;
const nodeName = node.nodeName;
if (
isMarkedHoistable(node) ||
nodeName === 'HEAD' ||
nodeName === 'BODY' ||
nodeName === 'SCRIPT' ||
nodeName === 'STYLE' ||
(nodeName === 'LINK' &&
((node: any): HTMLLinkElement).rel.toLowerCase() === 'stylesheet')
) {
// retain these nodes
} else {
element.removeChild(node);
}
node = nextNode;
}
return;
}

// -------------------
// Resources
// -------------------
Expand Down
Loading
Loading