UNPKG

12.6 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 { __DocContext } from "./document";
15import render from "preact-render-to-string";
16export let warned = false;
17export const __SeoContext = createContext({
18 seo: { current: {} },
19});
20export function Head({ children }) {
21 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;
22 const seo = useRef({});
23 const { head } = useContext(__DocContext);
24 render(h(__SeoContext.Provider, { value: { seo } }, children), {}, { pretty: true });
25 head.current.push(h(Fragment, null,
26 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"}` }),
27 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"}` }),
28 seo.current.title && h("title", null, seo.current.title),
29 seo.current.description && (h("meta", { name: "description", content: seo.current.description })),
30 seo.current.canonical && (h("link", { rel: "canonical", href: seo.current.canonical })),
31 ((_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 })),
32 ((_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 })),
33 ((_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 })),
34 ((_s = seo.current.openGraph) === null || _s === void 0 ? void 0 : _s.type) && (h("meta", { property: "og:type", content: seo.current.openGraph.type })),
35 ((_t = seo.current.images) === null || _t === void 0 ? void 0 : _t.length) &&
36 seo.current.images.map((image) => (h(Fragment, null,
37 image.src && h("meta", { property: "og:image", content: image.src }),
38 image.alt && h("meta", { property: "og:image:alt", content: image.alt }),
39 image.width && (h("meta", { property: "og:image:width", content: image.width.toString() })),
40 image.height && (h("meta", { property: "og:image:height", content: image.height.toString() }))))),
41 ((_u = seo.current.videos) === null || _u === void 0 ? void 0 : _u.length) &&
42 seo.current.videos.map((video) => (h(Fragment, null,
43 video.src && h("meta", { property: "og:video", content: video.src }),
44 video.width && (h("meta", { property: "og:video:width", content: video.width.toString() })),
45 video.height && (h("meta", { property: "og:video:height", content: video.height.toString() }))))),
46 ((_v = seo.current.audio) === null || _v === void 0 ? void 0 : _v.length) &&
47 seo.current.audio.map((audio) => (h("meta", { property: "og:video", content: audio.src }))),
48 ((_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 })),
49 ((_z = (_y = seo.current.openGraph) === null || _y === void 0 ? void 0 : _y.altLocales) === null || _z === void 0 ? void 0 : _z.length) && ((_0 = seo.current.openGraph) === null || _0 === void 0 ? void 0 : _0.altLocales.map((locale) => (h("meta", { property: "og:locale:alternate", content: locale })))),
50 ((_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 })),
51 ((_3 = seo.current.openGraph) === null || _3 === void 0 ? void 0 : _3.type) === "profile" && (h(Fragment, null,
52 ((_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 })),
53 ((_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 })),
54 ((_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 })),
55 ((_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 })))),
56 ((_12 = seo.current.openGraph) === null || _12 === void 0 ? void 0 : _12.type) === "book" && (h(Fragment, null,
57 ((_14 = (_13 = seo.current.openGraph) === null || _13 === void 0 ? void 0 : _13.authors) === null || _14 === void 0 ? void 0 : _14.length) && ((_15 = seo.current.openGraph) === null || _15 === void 0 ? void 0 : _15.authors.map((author) => (h("meta", { property: "book:author", content: author })))),
58 ((_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 })),
59 ((_19 = (_18 = seo.current.openGraph) === null || _18 === void 0 ? void 0 : _18.tags) === null || _19 === void 0 ? void 0 : _19.length) && ((_20 = seo.current.openGraph) === null || _20 === void 0 ? void 0 : _20.tags.map((tag) => (h("meta", { property: "book:tag", content: tag })))),
60 ((_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() })))),
61 ((_23 = seo.current.openGraph) === null || _23 === void 0 ? void 0 : _23.type) === "article" && (h(Fragment, null,
62 ((_24 = seo.current.openGraph) === null || _24 === void 0 ? void 0 : _24.publishedTime) && (h("meta", { property: "article:published_time", content: seo.current.openGraph.publishedTime.toISOString() })),
63 ((_25 = seo.current.openGraph) === null || _25 === void 0 ? void 0 : _25.modifiedTime) && (h("meta", { property: "article:modified_time", content: seo.current.openGraph.modifiedTime.toISOString() })),
64 ((_26 = seo.current.openGraph) === null || _26 === void 0 ? void 0 : _26.expirationTime) && (h("meta", { property: "article:expiration_time", content: seo.current.openGraph.expirationTime.toISOString() })),
65 ((_28 = (_27 = seo.current.openGraph) === null || _27 === void 0 ? void 0 : _27.authors) === null || _28 === void 0 ? void 0 : _28.length) && ((_29 = seo.current.openGraph) === null || _29 === void 0 ? void 0 : _29.authors.map((author) => (h("meta", { property: "article:author", content: author })))),
66 seo.current.openGraph.section && (h("meta", { property: "article:section", content: seo.current.openGraph.section })),
67 ((_30 = seo.current.openGraph.tags) === null || _30 === void 0 ? void 0 : _30.length) &&
68 seo.current.openGraph.tags.map((tag) => (h("meta", { property: "article:tag", content: tag }))))),
69 ((_31 = seo.current.facebook) === null || _31 === void 0 ? void 0 : _31.appId) && (h("meta", { property: "fb:app_id", content: seo.current.facebook.appId })),
70 seo.current.twitter && (h(Fragment, null,
71 seo.current.twitter.card && (h("meta", { name: "twitter:card", content: seo.current.twitter.card })),
72 seo.current.twitter.handle && (h("meta", { name: "twitter:creator", content: `@${seo.current.twitter.handle}`.replace(/^@@/, "@") })),
73 seo.current.twitter.site && (h("meta", { name: "twitter:site", content: `@${seo.current.twitter.site}`.replace(/^@@/, "@") }))))));
74 const _children = Array.isArray(children) ? children : [children];
75 head.current.push(_children.map((child) => {
76 if (child.type === "title" && !child.props["data-microsite-ignore"]) {
77 console.warn(`Prefer microsite's built-in <seo.title> component over <title>${child.props.children}</title>`);
78 if (!warned) {
79 console.warn(`\nimport { seo } from 'microsite/head';\nTo suppress this wanring, pass <title data-microsite-ignore />`);
80 warned = true;
81 }
82 }
83 return child;
84 }));
85 return null;
86}
87const normalizeChildren = (children) => {
88 if (Array.isArray(children))
89 return children.map(normalizeChildren).join("");
90 if (["string", "number"].includes(typeof children))
91 return children.toString();
92 if (typeof children === "boolean")
93 return children ? "" : null;
94 return render(children, {});
95};
96const robots = (_a) => {
97 var { children } = _a, props = __rest(_a, ["children"]);
98 const { seo } = useContext(__SeoContext);
99 seo.current.robots = props;
100 return null;
101};
102const title = ({ children }) => {
103 const { seo } = useContext(__SeoContext);
104 seo.current.title = normalizeChildren(children);
105 return null;
106};
107const description = ({ children }) => {
108 const { seo } = useContext(__SeoContext);
109 seo.current.description = normalizeChildren(children);
110 return null;
111};
112const image = (_a) => {
113 var _b;
114 var { children } = _a, props = __rest(_a, ["children"]);
115 const { seo } = useContext(__SeoContext);
116 seo.current.images = [...((_b = seo.current.images) !== null && _b !== void 0 ? _b : []), props];
117 return null;
118};
119const video = (_a) => {
120 var _b;
121 var { children } = _a, props = __rest(_a, ["children"]);
122 const { seo } = useContext(__SeoContext);
123 seo.current.videos = [...((_b = seo.current.videos) !== null && _b !== void 0 ? _b : []), props];
124 return null;
125};
126const audio = (_a) => {
127 var _b;
128 var { children } = _a, props = __rest(_a, ["children"]);
129 const { seo } = useContext(__SeoContext);
130 seo.current.audio = [...((_b = seo.current.audio) !== null && _b !== void 0 ? _b : []), props];
131 return null;
132};
133const canonical = ({ children }) => {
134 const { seo } = useContext(__SeoContext);
135 seo.current.canonical = normalizeChildren(children);
136 return null;
137};
138const twitter = (_a) => {
139 var { children } = _a, props = __rest(_a, ["children"]);
140 const { seo } = useContext(__SeoContext);
141 seo.current.twitter = props;
142 return null;
143};
144const facebook = (_a) => {
145 var { children } = _a, props = __rest(_a, ["children"]);
146 const { seo } = useContext(__SeoContext);
147 seo.current.facebook = props;
148 return null;
149};
150const openGraph = (_a) => {
151 var { children } = _a, props = __rest(_a, ["children"]);
152 const { seo } = useContext(__SeoContext);
153 seo.current.openGraph = props;
154 return null;
155};
156export const seo = {
157 robots,
158 title,
159 description,
160 image,
161 video,
162 audio,
163 canonical,
164 twitter,
165 facebook,
166 openGraph,
167};