@dr.pogodin/react-helmet
Version:
Thread-safe Helmet for React 19+ and friends
104 lines (102 loc) • 3.1 kB
JavaScript
import { createContext, useMemo } from 'react';
import Helmet from './Helmet';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const Context = /*#__PURE__*/createContext({
description: '',
title: ''
});
/**
* Auxiliary wrapper around "react-helmet", which helps to inject meta tags
* (page title, a brief content description, and social media thumbnails) into
* generated pages.
*/
const MetaTags = _ref => {
let {
children,
description,
extraMetaTags,
image,
siteName,
socialDescription,
socialTitle,
title,
url
} = _ref;
// NOTE: I guess, in this very case, we should prefer title and description
// also to empty social title and decscription?
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
const socTitle = socialTitle || title;
const socDesc = socialDescription || description;
/* eslint-enable @typescript-eslint/prefer-nullish-coalescing */
const context = useMemo(() => ({
description,
image,
siteName,
socialDescription,
socialTitle,
title,
url
}), [description, image, siteName, socialDescription, socialTitle, title, url]);
const extra = [];
if (extraMetaTags?.length) {
for (let i = 0; i < extraMetaTags.length; ++i) {
const {
content,
name
} = extraMetaTags[i];
extra.push(/*#__PURE__*/_jsx("meta", {
content: content,
name: name
}, `extra-meta-tag-${i}`));
}
}
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsxs(Helmet, {
children: [/*#__PURE__*/_jsx("title", {
children: title
}), /*#__PURE__*/_jsx("meta", {
content: description,
name: "description"
}), /*#__PURE__*/_jsx("meta", {
content: "summary_large_image",
name: "twitter:card"
}), /*#__PURE__*/_jsx("meta", {
content: socTitle,
name: "twitter:title"
}), /*#__PURE__*/_jsx("meta", {
content: socDesc,
name: "twitter:description"
}), image ? /*#__PURE__*/_jsx("meta", {
content: image,
name: "twitter:image"
}) : null, siteName ? /*#__PURE__*/_jsx("meta", {
content: `@${siteName}`,
name: "twitter:site"
}) : null, /*#__PURE__*/_jsx("meta", {
content: socTitle,
name: "og:title"
}), image ? /*#__PURE__*/_jsx("meta", {
content: image,
name: "og:image"
}) : null, image ? /*#__PURE__*/_jsx("meta", {
content: socTitle,
name: "og:image:alt"
}) : null, /*#__PURE__*/_jsx("meta", {
content: socDesc,
name: "og:description"
}), siteName ? /*#__PURE__*/_jsx("meta", {
content: siteName,
name: "og:sitename"
}) : null, url ? /*#__PURE__*/_jsx("meta", {
content: url,
name: "og:url"
}) : null, extra]
}), children ? /*#__PURE__*/_jsx(Context, {
value: context,
children: children
}) : null]
});
};
MetaTags.Context = Context;
export default MetaTags;
//# sourceMappingURL=MetaTags.js.map