页面中title的修改是非常常用的功能,尤其是 title ,icon 和 theme-color。我们需要有个 hooks 来方便的管理他们,并且提供双向绑定的能力。
支持的设置项
页面基本信息最重要的是title 和 icon,theme-color 在 safari 中支持较好。但是 chrome 系列的 pc 浏览器中支持不多,相对不常用。keywords 和 description 则一般用于 seo,每个页面使用一次即可。
| 属性名称 | 描述 | 类型 | 是否常用 |
|---|---|---|---|
| title | 页面的标题 | string | 是 |
| icon | 页面的icon,也可以用来指示任务状态 | url | 是 |
| themeColor | 页面的主题色 | 颜色(string) | 否 |
| keywords | 页面的关键字 | string[] | 否 |
| description | 页面的描述 | string | 否 |
受控设置
在 2020 年我们终于有了一个没有副作用的的方式来监听 页面的标题改动,感谢 MutationObserver。
var target = document.querySelector('title');// create an observer instancevar observer = new MutationObserver(function(mutations) {// We need only first event and only new value of the titleconsole.log(document.title );});// configuration of the observer:var config = { subtree: true, characterData: true, childList: true };// pass in the target node, as well as the observer optionsobserver.observe(target, config);
SEO
node 中没有 MutationObserver 和 document ,可能需要一个seo的兼容方式,现在的想法是直接短路,不进行任何设置。
因为不是很了解 seo,希望有更好的方案。
