UNPKG

1.58 kBJavaScriptView Raw
1import React, { forwardRef, createContext, useContext, } from "preact/compat";
2import { Buffer } from "buffer";
3import { __DocContext } from "./document";
4const isServer = typeof window === "undefined";
5const btoa = (str) => Buffer.from(str, "utf-8").toString("base64");
6const HydrateContext = createContext(false);
7export function withHydrate(Component) {
8 const name = Component.displayName || Component.name;
9 return forwardRef((props, ref) => {
10 const { hydrate } = useContext(__DocContext);
11 const hydrateParent = useContext(HydrateContext);
12 if (hydrateParent)
13 throw new Error(`withHydrate() should only be called at the top-level of a Component tree. <${name} /> should not be nested within <${hydrateParent} />`);
14 if (isServer)
15 hydrate.current.push({ name });
16 if (props.children && !["string", "number"].includes(typeof props.children))
17 throw new Error(`withHydrate() is unable to serialize complex \`children\`. Please inline these children into <${name} />.`);
18 return (React.createElement(HydrateContext.Provider, { value: name },
19 React.createElement("div", Object.assign({}, (isServer
20 ? {
21 "data-hydrate": name,
22 "data-props": Object.keys(props).length > 0
23 ? btoa(JSON.stringify(props))
24 : null,
25 }
26 : {})),
27 React.createElement(Component, Object.assign({}, Object.assign(Object.assign({}, props), { ref }))))));
28 });
29}