十八比特 十八比特
⚡ 18bit DNS (opens new window)
🏠 主页
  • 🌐 网站
  • 📦 资源
  • 📝 教程
  • 📐 制表符
  • 🗃️ 分类
  • 🏷️ 标签
  • 🗄️ 归档
📃 文章日志
ℹ️ 关于

十八比特

风带来故事的种子,时间使之发芽
⚡ 18bit DNS (opens new window)
🏠 主页
  • 🌐 网站
  • 📦 资源
  • 📝 教程
  • 📐 制表符
  • 🗃️ 分类
  • 🏷️ 标签
  • 🗄️ 归档
📃 文章日志
ℹ️ 关于
  • 文章使用CDN加载静态资源
  • 文章无法加载Webp格式图片解决方法
  • 给文章添加Waline评论系统和文章阅读量统计
  • 优化VuePress文章的SEO
    • 前言
    • 生成 sitemap
    • 添加 description、keywords
    • 添加 JSON-LD
  • 文章日志
  • 博客建设
秋澪冬安
2022-12-03
目录

优化VuePress文章的SEO

# 前言

为了提高VuePress的SEO,我用了下面几个方法优化SEO

  • 生成 sitemap (站点地图)
  • 为页面添加 description 和 keywords
  • 为页面添加 JSON-LD

# 生成 sitemap

使用插件 vuepress-plugin-sitemap (opens new window) 来生成站点地图

安装插件:

yarn add vuepress-plugin-sitemap -D

在 config.js 中配置:

module.exports = {
	plugins: [
		[
			"sitemap",
			{
				hostname: "https://www.meowpass.com",
			},
		],
	]
}

# 添加 description、keywords

编辑 config.js :

module.exports = {
    title: "title",
    description: 'description',
    head: [
        "meta",
            {
                name: "keywords",
                content: "前端,Vue,树莓派,Linux,秋澪,冬安,CSS,HTML",
            },
    ]
}

每个文章的 Front Matter 中也写上文章相关的 description、keywords:

---
meta:
  - name: description
    content: hello
  - name: keywords
    content: super,duper,SEO
---

# 添加 JSON-LD

因为没有现成的插件,所以需要自己写一个插件,参考 VuePress 文章之 SEO 优化(五)添加 JSON-LD 数据 (opens new window) 文章,对里面的代码做了些小小的修改

# 编写插件

在 .vuepress 目录下新建文件夹 vuepress-plugin-jsonld ,然后在新建的文件夹中执行 npm init

新建 index.js:

const { path } = require("@vuepress/shared-utils");

module.exports = (options) => ({
    name: "vuepress-plugin-jsonld",
    enhanceAppFiles() {
        return [path.resolve(__dirname, "enhanceAppFile.js")];
    },
    globalUIComponents: ["JSONLD"],
});

新建 enhanceAppFile.js:

import JSONLD from "./JSONLD.vue";

export default ({ Vue, options }) => {
    Vue.component("JSONLD", JSONLD);
};

新建 JSONLD.vue:

<template></template>

<script>
export default {
    created() {
        if (typeof this.$ssrContext !== "undefined") {
            this.$ssrContext.userHeadTags += `<script type='application/ld+json'>
            {
                "@context": "https://schema.org",
                "@type": "Article",
                "headline": "${this.$page.title || this.$title}",
                "url": "${"https://www.meowpass.com" + this.$page.path}",
                "image": [
                    "https://www.meowpass.com/assets/img/head-portrait.webp"
                ],
                "datePublished": "${
                    (this.$page.frontmatter.date && new Date(this.$page.frontmatter.date).toISOString()) ||
                    (this.$page.lastUpdated && new Date(this.$page.lastUpdated).toISOString()) ||
                    new Date().toISOString()
                }",
                "dateModified": "${(this.$page.lastUpdated && new Date(this.$page.lastUpdated).toISOString()) || new Date().toISOString()}",
                "author": [{
                    "@type": "Person",
                    "name": "秋澪冬安",
                    "url": "https://www.meowpass.com"
                }]
            }
            <\/script>`;
        }
    },
};
</script>

根据自己文章的信息,修改 JSONLD.vue 中的内容

# 使用插件

修改 config.js:

module.exports = {
	plugins: [
		require('./vuepress-plugin-jsonld')
	]
}
# 检验

发布后,使用 Google 的 富媒体搜索测试 (opens new window) 来检查是否生效

#文章#SEO
上次编辑: 2024/08/30, 16:21:31

← 给文章添加Waline评论系统和文章阅读量统计 文章日志→

最近更新
01
SSH跳板机访问内网服务
06-16
02
GPG 导出导入命令
06-16
03
从零开始的 All In One
06-14
更多文章>
Theme by Vdoing | Copyright © 2020-2025 十八比特 | 蜀ICP备2022002410号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式