美文网首页
vue SEO 优化

vue SEO 优化

作者: Pluto_7a23 | 来源:发表于2021-08-15 12:09 被阅读0次

prerender-spa-plugin使用

npm install prerender-spa-plugin --save

vue.config.js页面

const PrerenderSPAPlugin = require('prerender-spa-plugin')
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer
// eslint-disable-next-line no-unused-vars
const webpack = require('webpack')
const path = require('path')

module.exports = {
    lintOnSave: false,
    publicPath: '/',
  configureWebpack: config => {
    if (process.env.NODE_ENV !== 'production') return
    return {
      plugins: [
        new PrerenderSPAPlugin({
          // 生成文件的路径,也可以与webpakc打包的一致。
          // 这个目录只能有一级,如果目录层次大于一级,在生成的时候不会有任何错误提示,在预渲染的时候只会卡着不动。
          staticDir: path.join(__dirname, 'dist'),
          // outputDir: path.join(__dirname, './'),
          // 对应自己的路由文件,比如a有参数,就需要写成 /a/param1。
          routes: ['/about',  '/Home'],
          // 这个很重要,如果没有配置这段,也不会进行预编译
          renderer: new Renderer({
              inject: { //默认挂在window.__PRERENDER_INJECTED对象上,可以通过window.__PRERENDER_INJECTED.foo在预渲染页面取值
              foo: 'bar'
            },
            headless: false,
            // 在 main.js 中 document.dispatchEvent(new Event('render-event')),两者的事件名称要对应上。
            renderAfterDocumentEvent: 'render-event'//等到事件触发去渲染,此处我理解为是Puppeteer获取页面的时机
          })
        })
      ]
    }
  },
}

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false
import MetaInfo from 'vue-meta-info'

Vue.use(MetaInfo)


new Vue({
  router,
  store,
  render: h => h(App),
//添加到这里,这里的render-event和vue.config.js里面的renderAfterDocumentEvent配置名称一致
  mounted () {
    document.dispatchEvent(new Event('render-event'))
  }
}).$mount('#app')

npm run build 之后


image.png

seo优化还是有许多不明白的地方。希望道友可有交流一下 互相学习

转载 https://blog.csdn.net/kang_k/article/details/100514042
如侵告知删。

相关文章

网友评论

      本文标题:vue SEO 优化

      本文链接:https://www.haomeiwen.com/subject/yqkubltx.html