前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nuxt.js生成sitemap.xml、seo优化、robots.txt协议添加

Nuxt.js生成sitemap.xml、seo优化、robots.txt协议添加

作者头像
嘉美伯爵
发布2021-01-18 11:06:23
4.9K0
发布2021-01-18 11:06:23
举报

环境准备

注意生成sitemap依赖于@nuxtjs/sitemap,并且需要用axios进行请求,不要使用@nuxtjs/axios,不然会报错

代码语言:javascript
复制
npm install @nuxtjs/sitemap
npm install axios

sitemap.xml配置

  • 在nuxt.config.js中配置下面的内容
代码语言:javascript
复制
# 在第一行引入
const axios = require('axios')
# 在modules中添加@nuxtjs/sitemap
modules: [
    '@nuxtjs/sitemap'
  ],
# 在最后面添加
sitemap: {
    path: '/sitemap.xml', // sitemap path,不用改
    hostname: 'https://www.gaozhe.net/', // sitemap网址的前缀
    cacheTime: 60 * 60 * 6, //  更新频率,只在 generate: false有用
    gzip: true, // 生成 .xml.gz 压缩的 sitemap
    generate: false, // 允许使用 nuxt generate 生成
    // 排除不要页面
    exclude: [
      '/404',
      '/page',
      '/details',
      '/article',
      '/tags',
      '/category',
      '/search'
    ],
    // 页面路由
    routes (callback) {
      axios.all([
        // 文章分类
        axios.get('https://api.gaozhe.net/categories/', {
          params: {
            type: 2
          }
        }),
        // 遍历所有文章
        axios.get('https://api.gaozhe.net/posts', {
          params: {
            type: 2,
            page: 1,
            per_page: 100,
            _embed: true
          },
          data: { progress: false }
        }),
        // 文章标签
        axios.get('https://api.gaozhe.net/blog', {
          params: {
            type: 2
          }
        })

      ]).then(axios.spread(function (menu, posts, info) {
        let now = new Date();
        now.setHours(now.getHours(), now.getMinutes() - now.getTimezoneOffset());
        let indexRoutes = [
          {
            url: '/',
            changefreq: 'daily',
            priority: 1,
            lastmodISO: now.toISOString()
          }
        ]
        let menuRoutes = menu.data.map((data) => {
          let url = ''
          if (data.ctype === 1) {
            url = '/category/1?type=' + data.id + '&title=' + data.cname
          }
          if (data.ctype === 2) {
            url = '/page/' + data.id
          }
          return {
            url: url,
            changefreq: 'monthly',
            priority: 0.8,
            lastmodISO: data.add_time
          }
        });
        let postsRoutes = posts.data.results.map((data) => {
          return {
            url: '/details/' + data.id,
            changefreq: 'daily',
            priority: 0.9,
            lastmodISO: data.update_at
          }
        });
        let tagsRoutes = info.data[0].blog_tag.map((data) => {
          return {
            url: `/tags/1?type=${data.id}&title=${data.tname}`,
            changefreq: 'weekly',
            priority: 0.7,
            lastmodISO: data.add_time
          }
        })
        //  用concat进行数据合并
        callback(null, indexRoutes.concat(menuRoutes, postsRoutes, tagsRoutes));
      }), function (err) {
        throw (err);
      });
    }
  }

seo优化

  • 全局seo

在nuxt.config.js的meta中添加网站的字符编码、语言、响应式标签、网站描述、网站关键字等信息;在link中添加全局的css、网站logo等信息。

代码语言:javascript
复制
  head: {
    title: pkg.name,
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },
  • 页面seo

在nuxt.js项目pages路由页面的script中添加head方法,该方法将随nuxt运行时自动载入

代码语言:javascript
复制
head () {
    return {
      title: `${this.info.blogName} | ${this.info.blogDescription}`,
      meta: [
        { name: 'keywords', content: this.info.keywords },
        { name: 'description', content: this.info.description }
      ]
    }
  }

robots.txt协议

在nuxt项目的static文件夹下,配置项目的静态文件,直接在static新建robots.txt即可,nuxt运行时会自动装配到根路由

参考文档

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境准备
  • sitemap.xml配置
  • seo优化
  • robots.txt协议
  • 参考文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档