惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - 麦舒

网站访问速度的优化 ERP 订单打印的优化 一个网站首页重构小记 广告播放软件的开发 项目分享九:客户端的异常处理 项目分享八:基于按钮点击事件的弹窗 项目分享七:客户端防止表单重复提交 项目分享六:图片的延迟加载 项目分享五:H5图片压缩与上传 项目分享四:购物车页面的更新 项目分享三:页面之间的传值 项目分享二:APP 小红点中数字的处理 项目分享一:在项目中使用 IScroll 所碰到的那些坑 微信开发——通过授权获取用户的基本信息 千呼万唤岂出来,写款软件不容易——Visual Entity 2.0 发布 实现虽易,写好不易——小玩意也能体现编码功力,微信消息处理框架发布 代码重构之 —— 一堆if、esle 逻辑的处理 ALinq Dynamic 使用指南——慨述(上) ALinq Dynamic 使用指南——代码的获取与编译
页面 SEO 信息的优化
麦舒 · 2023-02-06 · via 博客园 - 麦舒

2023-02-06 15:30  麦舒  阅读(39)  评论()    收藏  举报

接到业务部门的一个需求,有一个运营了 10 多年的网站,现在需要添加一个功能,允许运营人员修改首页,商品页,博客页的标题,描述,关键词等信息。功能不复杂,但是有两个点需要注意的:

  1. 这个网站运营了 10 多年,中间已经换了好几拨人了。
  2. 网站必须稳定,可靠。
    对于这种历史性的网站,能不去动里的代码,最好还是不要去动。一旦动了,很容易踩坑,背锅。比如说:一旦流量下降,或者单量减少。运营人员首先就会觉得,是不是做开发那帮人,改了啥导致的。
    所以,对于这个需求,可以通过一个中间件去处理。

如下图:左边是原来的架构,右边是新的架构

原理
中间件对需求修改的页面请求进行拦截,然后对 HTML 进行处理,加上标题,描述,关键词等信息,再返回给客户端。

优点

  1. 不需要改动原来的代码。
  2. 可插拔。一旦出现,可以把中间件去掉,恢复原貌。

缺点
加了一个中间件,增加了系统的复杂性。

后台截图

代码
下面是实现的部分代码

export function processPageSEO(doc: Document, pageInfo: PageInfo) {

    if (!doc) throw errors.argumentNull("doc");
    if (!pageInfo) throw errors.argumentNull("pageInfo");

    if (!doc.head)
        throw new Error(`Head element is null.`);

    if (pageInfo.title)
        doc.title = pageInfo.title;

    if (pageInfo.keywords) {
        let keywordsMeta = doc.head.querySelector("meta[name='keywords']") as HTMLMetaElement;
        if (!keywordsMeta) {
            keywordsMeta = doc.createElement("meta");
            keywordsMeta.name = "keywords";
            doc.head.appendChild(keywordsMeta);
        }

        keywordsMeta.content = pageInfo.keywords;
    }

    if (pageInfo.description) {
        let descriptionMeta = doc.head.querySelector("meta[name='description']") as HTMLMetaElement;
        if (!descriptionMeta) {
            descriptionMeta = doc.createElement("meta");
            descriptionMeta.name = "description";
            doc.head.appendChild(descriptionMeta);
        }
        descriptionMeta.content = pageInfo.description;
    }
}

PS: 对于维护旧的系统,并且在不熟悉的情况下,增加一个中间件,是非常有必要的,这个中间件,对原来的系统做了隔离,避免因为修改代码出现的 BUG。事实上,这个中间件在后面一系列的新需求,发挥了很大的作用。