|
| 1 | +//@ts-ignore |
| 2 | +import React, { useContext } from "react"; |
| 3 | +import Effect from "./side-effect.jsx"; |
| 4 | +import { AmpStateContext } from "./amp-context"; |
| 5 | +import { HeadManagerContext } from "./head-manager-context"; |
| 6 | +import { isInAmpMode } from "./amp"; |
| 7 | + |
| 8 | +export function defaultHead(inAmpMode = false) { |
| 9 | + const head = [<meta charSet="utf-8" />]; |
| 10 | + if (!inAmpMode) { |
| 11 | + head.push(<meta name="viewport" content="width=device-width" />); |
| 12 | + } |
| 13 | + return head; |
| 14 | +} |
| 15 | + |
| 16 | +function onlyReactElement(list, child) { |
| 17 | + // React children can be "string" or "number" in this case we ignore them for backwards compat |
| 18 | + if (typeof child === "string" || typeof child === "number") { |
| 19 | + return list; |
| 20 | + } |
| 21 | + // Adds support for React.Fragment |
| 22 | + if (child.type === React.Fragment) { |
| 23 | + return list.concat( |
| 24 | + React.Children.toArray(child.props.children).reduce( |
| 25 | + (fragmentList, fragmentChild) => { |
| 26 | + if ( |
| 27 | + typeof fragmentChild === "string" || |
| 28 | + typeof fragmentChild === "number" |
| 29 | + ) { |
| 30 | + return fragmentList; |
| 31 | + } |
| 32 | + return fragmentList.concat(fragmentChild); |
| 33 | + }, |
| 34 | + [] |
| 35 | + ) |
| 36 | + ); |
| 37 | + } |
| 38 | + return list.concat(child); |
| 39 | +} |
| 40 | + |
| 41 | +const METATYPES = ["name", "httpEquiv", "charSet", "itemProp"]; |
| 42 | + |
| 43 | +/* |
| 44 | + returns a function for filtering head child elements |
| 45 | + which shouldn't be duplicated, like <title/> |
| 46 | + Also adds support for deduplicated `key` properties |
| 47 | +*/ |
| 48 | +function unique() { |
| 49 | + const keys = new Set(); |
| 50 | + const tags = new Set(); |
| 51 | + const metaTypes = new Set(); |
| 52 | + const metaCategories = {}; |
| 53 | + |
| 54 | + return (h) => { |
| 55 | + let isUnique = true; |
| 56 | + let hasKey = false; |
| 57 | + |
| 58 | + if (h.key && typeof h.key !== "number" && h.key.indexOf("$") > 0) { |
| 59 | + hasKey = true; |
| 60 | + const key = h.key.slice(h.key.indexOf("$") + 1); |
| 61 | + if (keys.has(key)) { |
| 62 | + isUnique = false; |
| 63 | + } else { |
| 64 | + keys.add(key); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // eslint-disable-next-line default-case |
| 69 | + switch (h.type) { |
| 70 | + case "title": |
| 71 | + case "base": |
| 72 | + if (tags.has(h.type)) { |
| 73 | + isUnique = false; |
| 74 | + } else { |
| 75 | + tags.add(h.type); |
| 76 | + } |
| 77 | + break; |
| 78 | + case "meta": |
| 79 | + for (let i = 0, len = METATYPES.length; i < len; i++) { |
| 80 | + const metatype = METATYPES[i]; |
| 81 | + if (!h.props.hasOwnProperty(metatype)) continue; |
| 82 | + |
| 83 | + if (metatype === "charSet") { |
| 84 | + if (metaTypes.has(metatype)) { |
| 85 | + isUnique = false; |
| 86 | + } else { |
| 87 | + metaTypes.add(metatype); |
| 88 | + } |
| 89 | + } else { |
| 90 | + const category = h.props[metatype]; |
| 91 | + const categories = metaCategories[metatype] || new Set(); |
| 92 | + if ((metatype !== "name" || !hasKey) && categories.has(category)) { |
| 93 | + isUnique = false; |
| 94 | + } else { |
| 95 | + categories.add(category); |
| 96 | + metaCategories[metatype] = categories; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + return isUnique; |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * |
| 109 | + * @param headElements List of multiple <Head> instances |
| 110 | + */ |
| 111 | +function reduceComponents(headElements, props) { |
| 112 | + return headElements |
| 113 | + .reduce((list, headElement) => { |
| 114 | + const headElementChildren = React.Children.toArray( |
| 115 | + headElement.props.children |
| 116 | + ); |
| 117 | + return list.concat(headElementChildren); |
| 118 | + }, []) |
| 119 | + .reduce(onlyReactElement, []) |
| 120 | + .reverse() |
| 121 | + .concat(defaultHead(props.inAmpMode)) |
| 122 | + .filter(unique()) |
| 123 | + .reverse() |
| 124 | + .map((c, i) => { |
| 125 | + const key = c.key || i; |
| 126 | + if ( |
| 127 | + process.env.NODE_ENV !== "development" && |
| 128 | + process.env.__NEXT_OPTIMIZE_FONTS && |
| 129 | + !props.inAmpMode |
| 130 | + ) { |
| 131 | + if ( |
| 132 | + c.type === "link" && |
| 133 | + c.props["href"] && |
| 134 | + // TODO(prateekbh@): Replace this with const from `constants` when the tree shaking works. |
| 135 | + ["https://fonts.googleapis.com/css", "https://use.typekit.net/"].some( |
| 136 | + (url) => c.props["href"].startsWith(url) |
| 137 | + ) |
| 138 | + ) { |
| 139 | + const newProps = { ...(c.props || {}) }; |
| 140 | + newProps["data-href"] = newProps["href"]; |
| 141 | + newProps["href"] = undefined; |
| 142 | + |
| 143 | + // Add this attribute to make it easy to identify optimized tags |
| 144 | + newProps["data-optimized-fonts"] = true; |
| 145 | + |
| 146 | + return React.cloneElement(c, newProps); |
| 147 | + } |
| 148 | + } |
| 149 | + return React.cloneElement(c, { key }); |
| 150 | + }); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * This component injects elements to `<head>` of your page. |
| 155 | + * To avoid duplicated `tags` in `<head>` you can use the `key` property, which will make sure every tag is only rendered once. |
| 156 | + */ |
| 157 | +export function Head({ children }) { |
| 158 | + const ampState = useContext(AmpStateContext); |
| 159 | + const headManager = useContext(HeadManagerContext); |
| 160 | + return ( |
| 161 | + <Effect |
| 162 | + reduceComponentsToState={reduceComponents} |
| 163 | + headManager={headManager} |
| 164 | + inAmpMode={isInAmpMode(ampState)} |
| 165 | + > |
| 166 | + {children} |
| 167 | + </Effect> |
| 168 | + ); |
| 169 | +} |
0 commit comments