当前位置: 首页 > Web前端 > vue.js

说说我在vue-cli3做预渲染遇到的坑

时间:2023-03-31 22:14:13 vue.js

鍓嶈█寮€鍙戜簡鑷繁鐨勪釜浜虹綉绔欏悗锛岄€夋嫨浣跨敤vue寮€鍙戯紝浼樺寲s鈥嬧€媏o鏄笉鍙伩鍏嶇殑銆傞壌浜庣洰鍓嶉〉闈笉澶氾紝绗竴绉嶆柟娉曟槸棰勬覆鏌撱€傛湰浠ヤ负鎻掍欢瀹夎閰嶇疆濂藉悗锛岀偣鍑讳竴涓嬪氨鎼炲畾浜嗭紝缁撴灉鍙戠幇骞舵病鏈夋兂璞$殑閭d箞绠€鍗曘€傚洜涓哄厛閬囧埌浜嗕袱娆℃姤閿欙紝鎵€浠ユ姌鑵句簡鍗婂銆傞棶棰樺強瑙e喅鏂规绗竴涓敊璇細鏃犳硶棰勬覆鏌撴墍鏈夎矾鐢憋紒璁剧疆閰嶇疆鍚庡湪鏋勫缓鏈熼棿鎶ュ憡姝ら棶棰樸€備富瑕佺棁鐘舵槸鎵撳紑娴忚鍣ㄥ悗鍗′笉鍔紝鐒跺悗寮哄埗閫€鍑恒€傝В鍐虫柟娉曪細鍙傝€僩ithub涓婄殑issushttps://github.com/chrisvfritz/prerender-spa-plugin/issues/196銆傚お濂囧浜嗭紒鑷充簬鎬庝箞绁炲锛屽悗闈㈠啀璇答煈囩浜屼釜閿欒锛歍hisrelativemodulewasnotfound锛氱紪璇戞椂涔熷嚭鐜颁簡杩欎釜閿欒銆傝В鍐虫柟娉曪細鎳掑姞杞界殑routes涓笉瑕佸啓鍓嶄袱鏉outes...馃し鈥嶏笍鍏蜂綋锛?vue.config.jsnewPrerenderSPAPlugin({...routes:['/','/pg1','/pg2','/pg3','/pg4'],...#router/index.jsimportIndexfrom'../views/Index.vue'importPg1from'../views/Pg1.vue'constroutes=[{path:'/',name:'Index',component:Index,//涓嶈浣跨敤寤惰繜鍔犺浇},{path:'/pg1',name:'Pg1',component:Pg1//涓嶈浣跨敤寤惰繜鍔犺浇},{path:'/pg2',name:'Pg2',component:()=>import('../views/Pg2.vue'),//鍙互浣跨敤寤惰繜鍔犺浇},杩囩▼ofcrawling绗竴涓棶棰樻渶濂囪懇锛屾寜鐓т粬璇寸殑锛屼笉鍙兘鎶婃墍鏈夌殑璺敱閮芥覆鏌撳嚭鏉ワ紝浣嗘槸鎴戠殑閰嶇疆閲屽彧鏈?鏉¤矾鐢憋紝鐒跺悗缂╁噺涓?鏉¤矾鐢憋紝杩樻槸鎶ヨ繖涓敊锛屾墍浠ユ垜纭畾涓嶆槸璺敱鐨勫師鍥狅紝鎸夌収github涓婄殑scheme閲嶅啓鍚庯紝濂藉儚娌¢棶棰樹簡銆傜劧鍚庡綋鏃跺氨灏藉姏鍏堢紪璇戞垚鍔熶簡锛屼絾鏄洖澶存兂澶嶇幇鐮旂┒鐨勬椂鍊欏鐜颁笉浜嗐€傘€傘€傪煠封€嶏笍褰撴椂宸茬粡鏉ヤ笉鍙婁簡锛屼簬鏄氨鏈変簡娌″姙娉曟垜鍙兘鏀惧純...闄勪笂瀹屾暣鐨勯厤缃?vue.config.jsconstpath=require('path');constPrerenderSPAPlugin=require('prerender-spa-plugin');constRenderer=PrerenderSPAPlugin.PuppeteerRenderer;configureWebpack:()=>{if(process.env.NODE_ENV==='production'){console.log(process.env.NODE_ENV)return{plugins:[newPrerenderSPAPlugin({//require-givenWebpack-outputapplicationpathpre-rendering.staticDir:path.join(__dirname,'./dist/fontend'),//蹇呭~锛岃娓叉煋鐨勮矾鐢眗outes:['/','/Pg1','/Pg2','/pg3','/Pg4'],//蹇呭~锛屽疄闄呰浣跨敤鐨勬覆鏌撳櫒锛屽鏋滀笉鏄紝鍒欎笉鑳介缂栬瘧renderer:newRenderer({inject:{foo:'bar'},headless:false,//娓叉煋娴忚鍣ㄧ獥鍙f椂鏄剧ず銆傚璋冭瘯鏈夌敤銆?/绛夊緟娓叉煋锛岀洿鍒版娴嬪埌鎸囧畾鐨勫厓绱犮€?/渚嬪锛屽湪椤圭洰鍏ュ彛澶勪娇鐢╜document.dispatchEvent(newEvent('custom-render-trigger'))`renderAfterDocumentEvent:"render-event",renderAfterTime:5000})})]}}else{return;}#main.jsnewVue({router,render:h=>h(App),mounted(){document.dispatchEvent(newEvent('render-event'))#娣诲姞杩欏彞璇潁}).$mount('#app')#router/index.jsimportIndexfrom'../views/Index.vue'importPg1from'../views/Pg1.vue'constroutes=[{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棰勬覆鏌揾ttps://www.jianshu.com/p/813c21899540[2]鏃犳硶棰勬覆鏌撴墍鏈夎矾鐢憋紒https://github.com/chrisvfritz/prerender-spa-plugin/issues/196