vue 單頁面應用SEO

一. 使用prerender-spa-plugin插件預渲染

  • vue-cli2.0版本
  1. 安裝插件
npm install prerender-spa-plugin -S
  1. 在webpack.prod.config.js 中添加
const PrerenderSPAPlugin =require('prerender-spa-plugin')
const Renderer=PrerenderSPAPlugin.PuppeteerRenderer
 //plugins 添加PrerenderSPAPlugin
    new PrerenderSPAPlugin({
      // 生成文件的路徑,也可以與webpakc打包的一致。
      staticDir: path.join(__dirname, '../dist'),         
      // 對應自己的路由文件
      routes: ['/','/news','/about','/contact'],
      renderer: new Renderer({
          inject: {
            foo: 'bar'
          },
          headless: false,
      })

3.修改main.js

new Vue({
  el: '#app',
  router,
  data:{
    hasShow:true,
  },
  render: h => h(App),
  mounted () {
    document.dispatchEvent(new Event('custom-render-trigger'))
  }
})

二. 優(yōu)化配置meta,使用vue-meta-info

  1. 安裝
npm install vue-meta-info -S

2.main.js引用

import MetaInfo from 'vue-meta-info'
Vue.use(MetaInfo)
  1. 相應路由頁面配置metaInfo
// about.vue
export default {
  metaInfo: {
    title: '關于我們', 
    meta: [{                 
      name: 'keywords',
      content: '關于我們'
    },
    {                
      name: 'description',
      content: '關于我們的頁面'
    }
    ],
  },
  name: "about",
  ....
}
  1. 配置 App.vue
  metaInfo(){
    return this.metaInfo
  },
  data(){
    return{
      metaInfo:{
        title:'首頁',
        meta: [{                 
                name: 'keywords',
                content: '首頁'
              },
              {                
                name: 'description',
                content: '首頁'
              }
          ],
        }
    }
  },
  watch:{
      $route(to){
          if(to.name==="about"){//根據(jù)不同路由配置不同
              this.metaInfo= {
                  title: '關于我們', 
                  meta: [{                 
                      name: 'keywords',
                      content: '關于我們'
                      },
                      {                
                      name: 'description',
                      content: '關于我們的頁面'
                      }
                  ],
              }
          }else{
            this.metaInfo={
                title:'首頁',
                meta: [{                 
                      name: 'keywords',
                      content: '首頁'
                    },
                    {                
                      name: 'description',
                      content: '首頁'
                    }
                ],
              }
          }
      }
  }
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容