UNPKG

1.03 kBJavaScriptView Raw
1import {
2 default as doUpdate,
3 getInjectedMetaTags
4} from '../React/client-update-page-info';
5
6/**
7 * _仅限客户端_
8 *
9 * 更新页面标题 `<title>` 和 `<meta>` 标签
10 * @param {Object} [info={}]
11 * @param {string} [info.title] 新的标题。如不提供,则不更新标题
12 * @param {Array<Object.<string, string>>} [info.metas] meta 标签信息,需要元素为 `{[name]: value}` 对象的数组。如不提供,则不更新 meta 标签
13 * @void
14 */
15const clientUpdatePageinfo = (info = {}) => {
16 if (!__CLIENT__) return;
17 let { title, metas } = info;
18
19 if (typeof title === 'undefined') title = document.title;
20 if (typeof metas === 'undefined') {
21 metas = [...getInjectedMetaTags()].map(el =>
22 Object.fromEntries(
23 el
24 .getAttributeNames()
25 .map(attr => [attr, el.getAttribute(attr)])
26 )
27 );
28 }
29
30 return doUpdate(title, metas);
31};
32
33export default clientUpdatePageinfo;