Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Commit 5f0d5bb

Browse files
committed
Initial hydration techniques configuration
1 parent 568c80b commit 5f0d5bb

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/gutenberg-packages/frontend.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ class GutenbergBlock extends HTMLElement {
2222
setTimeout(() => {
2323
const blockType = this.getAttribute("data-gutenberg-block-type");
2424
const attributes = JSON.parse(
25-
this.getAttribute("data-gutenberg-attributes"),
25+
this.getAttribute("data-gutenberg-attributes")
2626
);
2727
const blockProps = JSON.parse(
28-
this.getAttribute("data-gutenberg-block-props"),
28+
this.getAttribute("data-gutenberg-block-props")
2929
);
3030
const innerBlocks = this.querySelector("gutenberg-inner-blocks");
3131
const Comp = blockTypes.get(blockType);
32+
const technique = this.getAttribute("data-gutenberg-hydrate");
33+
3234
hydrate(
3335
<EnvContext.Provider value="frontend">
3436
<Comp
@@ -43,6 +45,7 @@ class GutenbergBlock extends HTMLElement {
4345
</Comp>
4446
</EnvContext.Provider>,
4547
this,
48+
technique
4649
);
4750
});
4851
}

src/gutenberg-packages/wordpress-blocks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { InnerBlocks, useBlockProps } from "@wordpress/block-editor";
22
import { registerBlockType as gutenbergRegisterBlockType } from "@wordpress/blocks";
33

4-
const save = (name, Comp) =>
4+
const save =
5+
(name, Comp) =>
56
({ attributes }) => {
67
const blockProps = useBlockProps.save();
78
return (
89
<gutenberg-interactive-block
910
data-gutenberg-block-type={name}
1011
data-gutenberg-attributes={JSON.stringify(attributes)}
1112
data-gutenberg-block-props={JSON.stringify(blockProps)}
13+
data-gutenberg-hydrate="view"
1214
>
1315
<Comp blockProps={blockProps} attributes={attributes}>
1416
<gutenberg-inner-blocks>

src/gutenberg-packages/wordpress-element.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
useEffect as useReactEffect,
55
useState as useReactState,
66
} from "@wordpress/element";
7-
export { hydrate } from "react-dom";
7+
import { hydrate as ReactHydrate } from "react-dom";
88

99
export const EnvContext = createContext(null);
1010

@@ -34,3 +34,29 @@ export const useState = (init) =>
3434

3535
export const useEffect = (...args) =>
3636
useBlockEnvironment() !== "save" ? useReactEffect(...args) : noop;
37+
38+
export const hydrate = (container, element, technique) => {
39+
switch (technique) {
40+
case "view":
41+
const io = new IntersectionObserver((entries) => {
42+
for (const entry of entries) {
43+
if (!entry.isIntersecting) continue;
44+
// As soon as we hydrate, disconnect this IntersectionObserver.
45+
io.disconnect();
46+
ReactHydrate(container, element);
47+
break; // break loop on first match
48+
}
49+
});
50+
io.observe(element.children[0]);
51+
break;
52+
case "idle":
53+
if ("requestIdleCallback" in window) {
54+
window.requestIdleCallback(() => ReactHydrate(container, element));
55+
} else {
56+
setTimeout(ReactHydrate(container, element), 200);
57+
}
58+
break;
59+
default:
60+
ReactHydrate(container, element);
61+
}
62+
};

0 commit comments

Comments
 (0)