談?wù)勎以趘ue-cli3中用預(yù)渲染遇到的坑
前言
在開發(fā)自己的個人網(wǎng)站的時候后,選擇了用vue來開發(fā),不可避免的遇到要對seo做優(yōu)化。鑒于目前頁面也不多,因此首先采用的是預(yù)渲染的方式。
本來以為把插件一裝,配置一配,咔咔咔就能搞定,結(jié)果發(fā)現(xiàn)并沒有想的那么簡單。因為首先就遇到了兩個報錯,折騰了半個晚上。
問題及解決方案
第一個報錯:
Unable to prerender all routes!
這個問題是在設(shè)置好配置之后,build的時候報出來的。主要癥狀就是打開了瀏覽器后卡主不動,然后就強制退出了。
解決方案:
參考了github上的issus https://github.com/chrisvfritz/prerender-spa-plugin/issues/196

很神奇的就好了!至于怎么神奇就后面再說👇
第二個報錯:
This relative module was not found:
這個報錯也是編譯的時候出現(xiàn)的。
解決方案:
routes里的前兩個路由不要用懶加載的方式來寫...🤷♀️
具體就是:
# vue.config.js
new PrerenderSPAPlugin({...
routes: ['/', '/pg1', '/pg2', '/pg3', '/pg4'],
...
# router/index.js
import Index from '../views/Index.vue'
import Pg1 from '../views/Pg1.vue'
const routes = [{
path: '/',
name: 'Index',
component: Index, // 不要用懶加載
},
{
path: '/pg1',
name: 'Pg1',
component: Pg1 // 不要用懶加載
},
{
path: '/pg2',
name: 'Pg2',
component: () => import('../views/Pg2.vue'), // 可以用懶加載
},
爬坑歷程
第一個問題是最神奇的,按照他說的來說,就是不能渲染所有的路由,但我目前也就只有五個路由在配置里面,然后我減到兩個路由還是報這個錯,所以肯定不是路由的原因。按照github上的方案改寫了之后,好像就好了。然后當(dāng)時我就盡想著先編譯成功再說,但等我回過頭來,想再復(fù)現(xiàn)出來研究一下的時候,怎么都復(fù)現(xiàn)不出來...🤷♀️
當(dāng)時時間也很晚了,沒辦法也就只好作罷了...
附上完整配置
# vue.config.js
const path = require('path');
const PrerenderSPAPlugin = require('prerender-spa-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
configureWebpack: () => {
if (process.env.NODE_ENV === 'production') {
console.log(process.env.NODE_ENV)
return {
plugins: [
new PrerenderSPAPlugin({
//要求-給的WebPack-輸出應(yīng)用程序的路徑預(yù)渲染。
staticDir: path.join(__dirname, './dist/fontend'),
//必需,要渲染的路線。
routes: ['/', '/Pg1', '/Pg2', '/Pg3', '/Pg4'],
//必須,要使用的實際渲染器,沒有則不能預(yù)編譯
renderer: new Renderer({
inject: {
foo: 'bar'
},
headless: false, //渲染時顯示瀏覽器窗口。對調(diào)試很有用。
//等待渲染,直到檢測到指定元素。
//例如,在項目入口使用`document.dispatchEvent(new Event('custom-render-trigger'))`
renderAfterDocumentEvent: "render-event",
renderAfterTime: 5000
})
})
]
}
} else {
return;
}
# main.js
new Vue({
router,
render: h => h(App),
mounted() {
document.dispatchEvent(new Event('render-event')) # 新增此句
}
}).$mount('#app')
# router/index.js
import Index from '../views/Index.vue'
import Pg1 from '../views/Pg1.vue'
const routes = [{
path: '/',
name: 'Index',
component: Index, // 不要用懶加載
},
{
path: '/Pg1',
name: 'Pg1',
component: Pg1 // 不要用懶加載
},
{
path: '/Pg2',
name: 'Pg2',
component: () => import('../views/Pg2.vue'), // 可以用懶加載
},
{
path: '/Pg3',
name: 'Pg3',
component: () => import('../views/Pg3.vue'), // 可以用懶加載
},
{
path: '/Pg4',
name: 'Pg4',
component: () => import('../views/Pg4.vue'), // 可以用懶加載
}]
參考鏈接
[1] vue-cli3.0預(yù)渲染 https://www.jianshu.com/p/813c21899540
[2] Unable to prerender all routes! https://github.com/chrisvfritz/prerender-spa-plugin/issues/196
到此這篇關(guān)于談?wù)勎以趘ue-cli3中用預(yù)渲染遇到的坑的文章就介紹到這了,更多相關(guān)vue-cli3 預(yù)渲染內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vscode配置vue下的es6規(guī)范自動格式化詳解
這篇文章主要介紹了vscode配置vue下的es6規(guī)范自動格式化詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Vue v2.4中新增的$attrs及$listeners屬性使用教程
這篇文章主要給大家介紹了關(guān)于Vue v2.4中新增的$attrs及$listeners屬性的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
vue-cli+axios實現(xiàn)文件上傳下載功能(下載接收后臺返回文件流)
這篇文章主要介紹了vue-cli+axios實現(xiàn)文件上傳下載功能(下載接收后臺返回文件流),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
詳解windows下vue-cli及webpack 構(gòu)建網(wǎng)站(二)導(dǎo)入bootstrap樣式
這篇文章主要介紹了詳解windows下vue-cli及webpack 構(gòu)建網(wǎng)站(二)導(dǎo)入bootstrap樣式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

