# 预渲染

## 预渲染简介

SEO和首屏加载速度慢的问题,社区讨论最多的解决方案是同构 SSR,即首屏使用服务端渲染,之后的交互逻辑交给客户端处理,解决了单页应用带来的两个问题,但是也带来了服务器压力增大,学习成本高,对老项目入侵性过强等问题。

## 版本信息

vue: 2.5.2

webpack: 3.6.0

vue-router: 3.0.1

prerender-spa-plugin: 3.4.0

## 使用

这里我们按照官方 github 的例子敲一下

const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin') module.exports = {
plugins: [
...
new PrerenderSPAPlugin({
// 这里选择文件生成目录.
staticDir: path.join(__dirname, 'dist'),
// 这里选择你要预加载的 router 路径,要与在 router 文件里面定义保持一致
routes: [ '/login' ],
})
]
} 还有一些注意事项,假设你的 vue 工程是 vue-cli 2-x 版本的 在 `config/index.js` 里面 `build/assetsPublicPath` 的值为 '/', 这里不能使用相对路径了
const path = require("path")

module.exports = {
build: {
index: path.resolve(__dirname, "../base/index.html"),
assetsRoot: path.resolve(__dirname, "../dist"),
assetsSubDirectory: "static",
assetsPublicPath: "/",
}
}
还有个配置要注意下在`build/utils.js` 中的 `ExtractTextPlugin.extract` 的 `publicPath` ,否则一些vue中引用的资源会找不到
ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
// publicPath: '../../'
})

然后在 `router/index.js` 的配置,预渲染要求是 history 模式,如果不声明,则生成的页面都是同一个 html,
import Vue from 'vue'
import Router from 'vue-router' Vue.use(Router) export default new Router({
mode: 'history',
routes: [...]
})
这个时候可以执行 `npm run build` 打包我们的项目了,一切正常的话,dist目录应该是这个样子的

├── index.html
├── login
│   └── index.html
└── static
├── css
├── fonts
├── images
├── img
└── js 看到 login 文件夹里面有 index.html,就代表你成功了, ## nginx 配置 这里不需要对这个插件做特殊处理,但是需要对 history 路由做处理,这里贴一下我的ngnix.config配置
server{
listen 8000;
server_name localhost;
root /dist;
error_page 500 502 503 504 /50x.html; location ~^/declaring/ {
try_files $uri $uri/ /index.html;
}
location = /50x.html {
root /dist;
}
}

  

到这里,重启 nginx, 应该就可以看到效果了

## 遇到的问题:

**生成的 html 里面不是我们想要的页面 html,或者里面的css,js引用失效。**

检查 `config/index` 里面的 `build/assetsPublicPath` 是不是根目录,

检查 路由是否是 history 模式

检查  `build/utils.js` 中的 `ExtractTextPlugin.extract` 的 `publicPath` 字段是否为空,

**加载页面会有闪屏的效果**

因为预加载是把当前页面,提到构建的时候加载,请求的时候,就会先加载html,然后加载 vue 实例,当 vue 加载好了,vue 会 render 并 push 到 #app 的 DOM 节点上,效果就是闪屏,而且如果页面是动态的,千人千面的,那么用户第一次看到的页面将会是你 build 时获取的数据,然后闪一下,vue 接管页面后正常渲染,

这个问题,RRS也是遇到,vue 也提供了相应的解决方案,我们移植过来就可以解决了,想了解更详细的,请参考[客户端激活(client-side hydration)](https://ssr.vuejs.org/zh/guide/hydration.html)
postProcess(context) {
context.html = context.html.replace('id="app"', 'id="app" data-server-rendered="true"');
return context;
},

  

下面是全部更改:
// build/webpack.prod.conf.js
...
const PrerenderSpaPlugin = require('prerender-spa-plugin') const Renderer = PrerenderSpaPlugin.PuppeteerRenderer
...
plugins: [
new PrerenderSpaPlugin({
// 编译后html需要存放的路径
staticDir: config.build.assetsRoot,
outputDir: config.build.assetsRoot,
indexPath: config.build.index,
// 列出需要预渲染的路由
routes: ['/', '/login'],
postProcess(context) {
context.html = context.html.replace('id="app"', 'id="app" data-server-rendered="true"');
return context;
},
renderer: new Renderer({
headless: false,
renderAfterTime: 5000,
// renderAfterDocumentEvent: 'render-event' // document.dispatchEvent(new Event('render-event'))
})
})
] // build/utils
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
// publicPath: '../../'
})
} // router/indes.js
let router = new Router({
mode: 'history',
routes: [...]
}) // config/index.js
build: {
...
assetsPublicPath: '/'
} // nginx.conf server{
listen 8000;
server_name localhost;
root /dist;
error_page 500 502 503 504 /50x.html; location /api/ {
#rewrite ^/api/(.*)$ /$1 break;
proxy_pass xxx.xxx.xxx;
} location ~^/declaring/ {
try_files $uri $uri/ /index.html;
}
location = /50x.html {
root /dist;
}
}
												

vue预渲染实践总结的更多相关文章

  1. vue 预渲染遇到的坑

    前言: 最近公司项目需要增加seo搜索引擎优化,到网上找了下资料,有预渲染和服务端渲染两种方式,考虑到只需要渲染首页所以我选择了先启用比较简单的预渲染方式来做seo! 步骤: 1.安装 prerend ...

  2. vue预渲染及其cdn配置

    VUE SEO方案一 - 预渲染及其cdn配置 项目接入VUE这样的框架后,看起来真是太漂亮了,奈何与MCV框架比起来,单页应用程序却满足不了SEO的业务需求,首屏渲染时间也是个问题.总不能白学VUE ...

  3. prerender-spa-plugin Vue预渲染配合meta-info优化seo

    记录一下解决方案的过程 先安装prerender和puppeteer插件  这个国外大神写的  github地址就不附上了(百度有) cnpm install prerender-spa-plugin ...

  4. vue 预渲染 prerender-spa-plugin

    1.预渲染说明 https://ssr.vuejs.org/zh/#为什么使用服务器端渲染-ssr-? 如果你调研服务器端渲染(SSR)只是用来改善少数营销页面(例如 /, /about, /cont ...

  5. prerender-spa-plugin预渲染踩坑

    为什么要使用预渲染? 为了应付SEO(国内特别是百度)考虑在网站(vue技术栈系列)做一些优化.大概有几种方案可以考虑: 服务端做优化: 第一,ssr,vue官方文档给出的服务器渲染方案,这是一套完整 ...

  6. Vue项目预渲染机制引入实践

    周末想顺便把已经做好静态页面的webApp项目做一下SEO优化,由于不想写蹩脚的SSR代码,所以准备采用预渲染,本来想着网上有这么多预渲染的文章,随便找个来跟着做不就完了嘛,结果年轻的我付出了整个周末 ...

  7. 前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)

    一.背景:页面为何会卡? 1.1 等待时间长(性能) 项目本身包/第三方脚本比较大. JavaScript 执行阻塞页面加载. 图片体积大且多. 特别是对于首屏资源加载中的白屏时间,用户等待的时间就越 ...

  8. vue项目使用 prerender-spa-plugin 预渲染

    由于项目要做seo优化,而用vue写成的spa页面谷歌浏览器等是抓取不到数据的.介于ssr和预渲染来说,后者相对来说要简单许多.所以采用了预渲染方式.采用插件prerender-spa-plugin使 ...

  9. vue(初探预渲染)

    ---恢复内容开始--- 一.前言 1.简介预渲染                     2.案例演示(不配置预渲染)                     3.配置预渲染, 二.主要内容 1.简 ...

随机推荐

  1. 并发编程之 Exchanger 源码分析

    前言 JUC 包中除了 CountDownLatch, CyclicBarrier, Semaphore, 还有一个重要的工具,只不过相对而言使用的不多,什么呢? Exchange -- 交换器.用于 ...

  2. [转]论magento1和magento2的速度性能优化问题

    本文转自:http://www.360magento.com/blog/magento-speed-up/ magento从2007年发展至今,也经历了十余年的磨练,如今也迎来了magento的换代产 ...

  3. [转]Microsoft SQL SERVER 2008 R2 REPORT SERVICE 匿名登录

    本文转自:https://www.cnblogs.com/Zouzhe/p/5736070.html SQL SERVER 2008 R2 是微软目前最新的数据库版本,在之前的SQL SERVER 2 ...

  4. Redis 缓存设计原则

    基本原则 只应将热数据放到缓存中 所有缓存信息都应设置过期时间 缓存过期时间应当分散以避免集中过期 缓存key应具备可读性 应避免不同业务出现同名缓存key 可对key进行适当的缩写以节省内存空间 选 ...

  5. C++ vector 排序

    C++ vector 排序 C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结 ...

  6. 常见hash算法的原理(转)

    常见hash算法的原理   散列表,它是基于快速存取的角度设计的,也是一种典型的“空间换时间”的做法.顾名思义,该数据结构可以理解为一个线性表,但是其中的元素不是紧密排列的,而是可能存在空隙. 散列表 ...

  7. django rest_framework Serializers 序列化组件

    为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成json格式,那就需要对我们从数据库拿 ...

  8. 在学习使用webpack中遇到的错误

    1.局部安装webpack之后使用node_modules/.bin/webpack app/main.js public/bundle.js命令报错      解决方法:      webpack版 ...

  9. layui table 行号

    {type: 'numbers', title: '序号', width: '80'}

  10. 洛谷P4198 楼房重建(线段树)

    题意 题目链接 Sol 别问我为什么发两遍 就是为了骗访问量 这个题的线段树做法,,妙的很 首先一个显然的结论:位置\(i\)能被看到当且仅当\(\frac{H_k}{k} < \frac{H_ ...