UNPKG

14 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import { h, createContext, Fragment } from "preact";
13import { useRef, useContext } from "preact/hooks";
14import { __HeadContext } from "./document.js";
15import render from "preact-render-to-string";
16export let warned = false;
17export const __SeoContext = createContext({
18 seo: { current: {} },
19});
20const isElement = (node) => node.nodeType === node.ELEMENT_NODE;
21export const Head = ({ children }) => {
22 var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _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;
23 const seo = useRef({});
24 const { head } = useContext(__HeadContext);
25 const prevHead = useRef(null);
26 render(h(__SeoContext.Provider, { value: { seo } }, children), {}, { pretty: true });
27 head.current = [
28 h(Fragment, null,
29 h("meta", { name: "robots", content: `${((_a = seo.current.robots) === null || _a === void 0 ? void 0 : _a.noindex) ? "noindex" : "index"},${((_b = seo.current.robots) === null || _b === void 0 ? void 0 : _b.nofollow) ? "nofollow" : "follow"}` }),
30 h("meta", { name: "googlebot", content: `${((_c = seo.current.robots) === null || _c === void 0 ? void 0 : _c.noindex) ? "noindex" : "index"},${((_d = seo.current.robots) === null || _d === void 0 ? void 0 : _d.nofollow) ? "nofollow" : "follow"}` }),
31 seo.current.title && h("title", null, seo.current.title),
32 seo.current.description && (h("meta", { name: "description", content: seo.current.description })),
33 seo.current.canonical && (h("link", { rel: "canonical", href: seo.current.canonical })),
34 ((_f = (_e = seo.current.openGraph) === null || _e === void 0 ? void 0 : _e.title) !== null && _f !== void 0 ? _f : seo.current.title) && (h("meta", { property: "og:title", content: (_h = (_g = seo.current.openGraph) === null || _g === void 0 ? void 0 : _g.title) !== null && _h !== void 0 ? _h : seo.current.title })),
35 ((_k = (_j = seo.current.openGraph) === null || _j === void 0 ? void 0 : _j.description) !== null && _k !== void 0 ? _k : seo.current.description) && (h("meta", { property: "og:description", content: (_m = (_l = seo.current.openGraph) === null || _l === void 0 ? void 0 : _l.description) !== null && _m !== void 0 ? _m : seo.current.description })),
36 ((_p = (_o = seo.current.openGraph) === null || _o === void 0 ? void 0 : _o.url) !== null && _p !== void 0 ? _p : seo.current.canonical) && (h("meta", { property: "og:url", content: (_r = (_q = seo.current.openGraph) === null || _q === void 0 ? void 0 : _q.url) !== null && _r !== void 0 ? _r : seo.current.canonical })),
37 ((_s = seo.current.openGraph) === null || _s === void 0 ? void 0 : _s.type) && (h("meta", { property: "og:type", content: seo.current.openGraph.type })),
38 ((_t = seo.current.images) === null || _t === void 0 ? void 0 : _t.length) &&
39 seo.current.images.map((image) => (h(Fragment, null,
40 image.src && h("meta", { property: "og:image", content: image.src }),
41 image.alt && h("meta", { property: "og:image:alt", content: image.alt }),
42 image.width && (h("meta", { property: "og:image:width", content: image.width.toString() })),
43 image.height && (h("meta", { property: "og:image:height", content: image.height.toString() }))))),
44 ((_u = seo.current.videos) === null || _u === void 0 ? void 0 : _u.length) &&
45 seo.current.videos.map((video) => (h(Fragment, null,
46 video.src && h("meta", { property: "og:video", content: video.src }),
47 video.width && (h("meta", { property: "og:video:width", content: video.width.toString() })),
48 video.height && (h("meta", { property: "og:video:height", content: video.height.toString() }))))),
49 ((_v = seo.current.audio) === null || _v === void 0 ? void 0 : _v.length) &&
50 seo.current.audio.map((audio) => (h("meta", { property: "og:video", content: audio.src }))),
51 ((_w = seo.current.openGraph) === null || _w === void 0 ? void 0 : _w.locale) && (h("meta", { property: "og:locale", content: (_x = seo.current.openGraph) === null || _x === void 0 ? void 0 : _x.locale })),
52 ((_z = (_y = seo.current.openGraph) === null || _y === void 0 ? void 0 : _y.altLocales) === null || _z === void 0 ? void 0 : _z.length) &&
53 ((_0 = seo.current.openGraph) === null || _0 === void 0 ? void 0 : _0.altLocales.map((locale) => (h("meta", { property: "og:locale:alternate", content: locale })))),
54 ((_1 = seo.current.openGraph) === null || _1 === void 0 ? void 0 : _1.siteName) && (h("meta", { property: "og:site_name", content: (_2 = seo.current.openGraph) === null || _2 === void 0 ? void 0 : _2.siteName })),
55 ((_3 = seo.current.openGraph) === null || _3 === void 0 ? void 0 : _3.type) === "profile" && (h(Fragment, null,
56 ((_4 = seo.current.openGraph) === null || _4 === void 0 ? void 0 : _4.firstName) && (h("meta", { property: "profile:first_name", content: (_5 = seo.current.openGraph) === null || _5 === void 0 ? void 0 : _5.firstName })),
57 ((_6 = seo.current.openGraph) === null || _6 === void 0 ? void 0 : _6.lastName) && (h("meta", { property: "profile:last_name", content: (_7 = seo.current.openGraph) === null || _7 === void 0 ? void 0 : _7.lastName })),
58 ((_8 = seo.current.openGraph) === null || _8 === void 0 ? void 0 : _8.username) && (h("meta", { property: "profile:username", content: (_9 = seo.current.openGraph) === null || _9 === void 0 ? void 0 : _9.username })),
59 ((_10 = seo.current.openGraph) === null || _10 === void 0 ? void 0 : _10.gender) && (h("meta", { property: "profile:gender", content: (_11 = seo.current.openGraph) === null || _11 === void 0 ? void 0 : _11.gender })))),
60 ((_12 = seo.current.openGraph) === null || _12 === void 0 ? void 0 : _12.type) === "book" && (h(Fragment, null,
61 ((_14 = (_13 = seo.current.openGraph) === null || _13 === void 0 ? void 0 : _13.authors) === null || _14 === void 0 ? void 0 : _14.length) &&
62 ((_15 = seo.current.openGraph) === null || _15 === void 0 ? void 0 : _15.authors.map((author) => (h("meta", { property: "book:author", content: author })))),
63 ((_16 = seo.current.openGraph) === null || _16 === void 0 ? void 0 : _16.isbn) && (h("meta", { property: "book:isbn", content: (_17 = seo.current.openGraph) === null || _17 === void 0 ? void 0 : _17.isbn })),
64 ((_19 = (_18 = seo.current.openGraph) === null || _18 === void 0 ? void 0 : _18.tags) === null || _19 === void 0 ? void 0 : _19.length) &&
65 ((_20 = seo.current.openGraph) === null || _20 === void 0 ? void 0 : _20.tags.map((tag) => (h("meta", { property: "book:tag", content: tag })))),
66 ((_21 = seo.current.openGraph) === null || _21 === void 0 ? void 0 : _21.releaseDate) && (h("meta", { property: "profile:username", content: (_22 = seo.current.openGraph) === null || _22 === void 0 ? void 0 : _22.releaseDate.toISOString() })))),
67 ((_23 = seo.current.openGraph) === null || _23 === void 0 ? void 0 : _23.type) === "article" && (h(Fragment, null,
68 ((_24 = seo.current.openGraph) === null || _24 === void 0 ? void 0 : _24.publishedTime) && (h("meta", { property: "article:published_time", content: seo.current.openGraph.publishedTime.toISOString() })),
69 ((_25 = seo.current.openGraph) === null || _25 === void 0 ? void 0 : _25.modifiedTime) && (h("meta", { property: "article:modified_time", content: seo.current.openGraph.modifiedTime.toISOString() })),
70 ((_26 = seo.current.openGraph) === null || _26 === void 0 ? void 0 : _26.expirationTime) && (h("meta", { property: "article:expiration_time", content: seo.current.openGraph.expirationTime.toISOString() })),
71 ((_28 = (_27 = seo.current.openGraph) === null || _27 === void 0 ? void 0 : _27.authors) === null || _28 === void 0 ? void 0 : _28.length) &&
72 ((_29 = seo.current.openGraph) === null || _29 === void 0 ? void 0 : _29.authors.map((author) => (h("meta", { property: "article:author", content: author })))),
73 seo.current.openGraph.section && (h("meta", { property: "article:section", content: seo.current.openGraph.section })),
74 ((_30 = seo.current.openGraph.tags) === null || _30 === void 0 ? void 0 : _30.length) &&
75 seo.current.openGraph.tags.map((tag) => (h("meta", { property: "article:tag", content: tag }))))),
76 ((_31 = seo.current.facebook) === null || _31 === void 0 ? void 0 : _31.appId) && (h("meta", { property: "fb:app_id", content: seo.current.facebook.appId })),
77 seo.current.twitter && (h(Fragment, null,
78 seo.current.twitter.card && (h("meta", { name: "twitter:card", content: seo.current.twitter.card })),
79 seo.current.twitter.handle && (h("meta", { name: "twitter:creator", content: `@${seo.current.twitter.handle}`.replace(/^@@/, "@") })),
80 seo.current.twitter.site && (h("meta", { name: "twitter:site", content: `@${seo.current.twitter.site}`.replace(/^@@/, "@") }))))),
81 ];
82 const _children = Array.isArray(children) ? children : [children];
83 head.current.push(_children.map((child) => {
84 if (child.type === "title" && !child.props["data-microsite-ignore"]) {
85 console.warn(`Prefer microsite's built-in <seo.title> component over <title>${child.props.children}</title>`);
86 if (!warned) {
87 console.warn(`\nimport { seo } from 'microsite/head';\nTo suppress this wanring, pass <title data-microsite-ignore />`);
88 warned = true;
89 }
90 }
91 return child;
92 }));
93 if (typeof window !== "undefined") {
94 let html = render(h(Fragment, null, head.current), {}, { pretty: true });
95 if (prevHead.current !== html) {
96 let marker = null;
97 let managed = false;
98 for (const node of Array.from(document.head.childNodes)) {
99 if (isElement(node) &&
100 node.tagName.toLowerCase() === "meta" &&
101 ((_32 = node.getAttribute("name")) === null || _32 === void 0 ? void 0 : _32.startsWith("microsite"))) {
102 if (node.getAttribute("name").endsWith("start")) {
103 managed = true;
104 marker = node;
105 continue;
106 }
107 else {
108 break;
109 }
110 }
111 if (managed) {
112 node.remove();
113 }
114 }
115 marker.insertAdjacentHTML("afterend", html);
116 }
117 prevHead.current = html;
118 }
119 return null;
120};
121const normalizeChildren = (children) => {
122 if (Array.isArray(children))
123 return children.map(normalizeChildren).join("");
124 if (["string", "number"].includes(typeof children))
125 return children.toString();
126 if (typeof children === "boolean")
127 return children ? "" : null;
128 return render(children, {});
129};
130const robots = (_a) => {
131 var { children } = _a, props = __rest(_a, ["children"]);
132 const { seo } = useContext(__SeoContext);
133 seo.current.robots = props;
134 return null;
135};
136const title = ({ children }) => {
137 const { seo } = useContext(__SeoContext);
138 seo.current.title = normalizeChildren(children);
139 return null;
140};
141const description = ({ children }) => {
142 const { seo } = useContext(__SeoContext);
143 seo.current.description = normalizeChildren(children);
144 return null;
145};
146const image = (_a) => {
147 var _b;
148 var { children } = _a, props = __rest(_a, ["children"]);
149 const { seo } = useContext(__SeoContext);
150 seo.current.images = [...((_b = seo.current.images) !== null && _b !== void 0 ? _b : []), props];
151 return null;
152};
153const video = (_a) => {
154 var _b;
155 var { children } = _a, props = __rest(_a, ["children"]);
156 const { seo } = useContext(__SeoContext);
157 seo.current.videos = [...((_b = seo.current.videos) !== null && _b !== void 0 ? _b : []), props];
158 return null;
159};
160const audio = (_a) => {
161 var _b;
162 var { children } = _a, props = __rest(_a, ["children"]);
163 const { seo } = useContext(__SeoContext);
164 seo.current.audio = [...((_b = seo.current.audio) !== null && _b !== void 0 ? _b : []), props];
165 return null;
166};
167const canonical = ({ children }) => {
168 const { seo } = useContext(__SeoContext);
169 seo.current.canonical = normalizeChildren(children);
170 return null;
171};
172const twitter = (_a) => {
173 var { children } = _a, props = __rest(_a, ["children"]);
174 const { seo } = useContext(__SeoContext);
175 seo.current.twitter = props;
176 return null;
177};
178const facebook = (_a) => {
179 var { children } = _a, props = __rest(_a, ["children"]);
180 const { seo } = useContext(__SeoContext);
181 seo.current.facebook = props;
182 return null;
183};
184const openGraph = (_a) => {
185 var { children } = _a, props = __rest(_a, ["children"]);
186 const { seo } = useContext(__SeoContext);
187 seo.current.openGraph = props;
188 return null;
189};
190export const seo = {
191 robots,
192 title,
193 description,
194 image,
195 video,
196 audio,
197 canonical,
198 twitter,
199 facebook,
200 openGraph,
201};