-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgatsby-ssr.js
38 lines (33 loc) · 1020 Bytes
/
gatsby-ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { setup } from "otion";
import {
filterOutUnusedRules,
getStyleElement,
VirtualInjector,
// eslint-disable-next-line import/no-unresolved
} from "react-otion/server";
import options from "./src/options";
/** @type {Map<string, ReturnType<typeof VirtualInjector>>} */
const injectorsByPathname = new Map();
/** @type {import("gatsby").GatsbyBrowser["wrapRootElement"]} */
export const wrapRootElement = ({ pathname, element }) => {
const injector = VirtualInjector();
setup({ ...options, injector });
injectorsByPathname.set(pathname, injector);
return element;
};
/**
* @param {{
* pathname: string;
* bodyHtml: string;
* setHeadComponents: (components: React.ReactNode) => void;
* }} apiCallbackContext
*/
export const onRenderBody = ({ pathname, bodyHtml, setHeadComponents }) => {
const injector = injectorsByPathname.get(pathname);
if (injector) {
setHeadComponents(
getStyleElement(filterOutUnusedRules(injector, bodyHtml)),
);
injectorsByPathname.delete(pathname);
}
};