美文网首页
学习记录(只为方便自己回顾)

学习记录(只为方便自己回顾)

作者: lk儒家 | 来源:发表于2019-08-28 17:30 被阅读0次

webpack.server.config.js

const webpack = require('webpack');
const merge = require('webpack-merge');

const base = require('./webpack.base.conf');
module.exports = merge(base,{
    target:"node",
    entry:"./src/entry-server.js",
    output:{
        filename:"bundle.server.js",
        libraryTarget:"commonjs2"
    },
    plugins:[
        
    ]
})

webpack.client.config.js

const webpack = require("webpack");
const vueLoaderConfig = require('./vue-loader.conf');
const utils = require('./utils')
const path  = require("path");
function resolve(dir){
    return path.join(__dirname,"..",dir);
}
module.exports = {
    entry: {
        app: './src/entry-client.js'
    },
    output: {
        path: path.resolve(__dirname,'../dist'),
        filename: 'bundle.client.js',
        publicPath: '/dist/'
    },
    plugins:[
        // new VueSSRServerPlugin();
    ],
    resolve: {
        extensions: ['.js', '.vue', '.json'],
        alias: {
        'vue$': 'vue/dist/vue.esm.js',
        '@': resolve('src')
        }
    },
    module: {
        rules: [
        {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: vueLoaderConfig
        },
        {
            test: /\.js$/,
            loader: 'babel-loader',
            include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
        },
        {
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            loader: 'url-loader',
            options: {
            limit: 10000,
            name: utils.assetsPath('img/[name].[hash:7].[ext]')
            }
        },
        {
            test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
            loader: 'url-loader',
            options: {
            limit: 10000,
            name: utils.assetsPath('media/[name].[hash:7].[ext]')
            }
        },
        {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url-loader',
            options: {
            limit: 10000,
            name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
            }
        }
        ]
    }
}

entry-server.js

import { createApp } from './main';

export default context => {
    
    return new Promise((resolve,reject) =>{
        const { app,router,store } = createApp()
        const url = context.url;
        router.push(url);
        router.onReady(()=>{
            const matchedComponents = router.getMatchedComponents();
            if(!matchedComponents.length){
                reject({code:404})
            }
            let asyncDataPromiseFns = [];
            matchedComponents.map( component => {
                if(component.serverRequest){
                    asyncDataPromiseFns.push(component.serverRequest(store));
                }   
            })
            //遍历路由下所有的组件,如果有需要服务端渲染的请求,则进行请求
            Promise.all(asyncDataPromiseFns).then(()=>{
                context.state = store.state;
                resolve(app);
            }).catch(reject)
        },reject)
    })
}

entry-client.js

//客户端的入口文件
import { createApp } from './main';

const { app,router,store } = createApp();

if(window.__INITIAL_STATE__){
    store.replaceState(window.__INITIAL_STATE__);
}

window.onload = function(){
    app.$mount('#app');
}

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// import router from './router'
import createRouter from "./router";
import createStore from './store'
Vue.config.productionTip = false

/* eslint-disable no-new */
export function createApp () {
  const router = createRouter();
  const store = createStore();
  const app = new Vue({
   router,
   store,
   // 根实例简单的渲染应用程序组件。
   render: h => h(App)
  })
  return { app,router,store,App }
 }

路由配置文件

import Vue from 'vue'
import Router from 'vue-router'
import home from '@/components/home'
import about from '@/components/about'


Vue.use(Router)

export default () => {
  return new Router({
    mode: 'history', //一定要是history模式
    routes: [
      {
        path: '/',
        name: 'home',
        component: home
      },
      {
        path: '/about',
        name: 'about',
        component: about
      }
    ]
  })
} 

Vuex配置文件

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
Vue.use(Vuex);

export default function createStore(){
    let store = new Vuex.Store({
        state:{
            homeInfo:''
        },
        actions:{
            getHomeInfo({commit}){
                return new Promise((resolve,reject)=>{
                    axios.get("api地址").then(res=>{
                        commit('setHomeInfo',res.data);
                        resolve();
                    }).catch(reject);
                })
            }
        },
        mutations:{
            setHomeInfo(state,data){
                state.homeInfo = data;
            }
        }
    })
    return store;
}

请求数据的页面

<template>
    <div class="home">
        <h1>这是首页</h1>
        <div>{{homeInfo.message}}</div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
            
            }
        },
        serverRequest(store){
            return store.dispatch('getHomeInfo')
        },
        computed:{
            homeInfo(){
                return this.$store.state.homeInfo;
            }
        }
    }
</script>

package.json的scripts配置信息

 "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js",
    "server": "webpack --config build/webpack.server.config.js",
    "client": "webpack --config build/webpack.client.config.js"
  }

服务器文件

const Vue = require('vue');
const Koa = require('koa');
const server = new Koa();
const path = require("path");
const koaStatic = require('koa-static');
const router = require('koa-router')();


//创建服务端渲染器
const renderer = require("vue-server-renderer").createRenderer();
const createApp = require('./dist/bundle.server.js')['default'];

//设置静态资源目录
server.use(koaStatic(path.resolve(__dirname,"./dist")))

const  clientBundleFileUrl = "/bundle.client.js";


router.get('/text',async ctx=>{
    ctx.body={
        code:1,
        message:"我是服务器返回的测试内容!"
    }
})

router.get('*',async ctx=>{
    const context = { url:ctx.request.url };
    try{
        const app = await createApp(context);
        let state = JSON.stringify(context.state);
        const resHtml = await renderer.renderToString(app);
        ctx.body=`
            <!DOCTYPE html>
            <html>
            <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width,initial-scale=1.0">
                <title>Vue2.0 SSR渲染页面</title>
                <script>window.__INITIAL_STATE__=${state}</script>
                <script src="${clientBundleFileUrl}"></script>
            </head>
            <body>
                ${resHtml}
            </body>
            </html>        
        `;
    }catch(err){
        ctx.body="程序运行出错,请联系管理员进行处理!"
    }
    
})

server.use(router.routes());
server.use(router.allowedMethods());

server.listen(3000,()=>{
    console.log("服务已启动!");
});

利用到的插件

vue-server-renderer

相关文章

  • 学习记录(只为方便自己回顾)

    webpack.server.config.js webpack.client.config.js entry-s...

  • 自定义绘制-基本绘制

    写在前面 本文只为记录自己的学习过程,以方便复习,内容定位为知识点,并非详细过程介绍。 学习站 Doc:https...

  • jvm学习

    本系列文章主要是对学习《深入理解java虚拟机》的记录,以加深自己的理解,也方便自己后续复习回顾 前言 之前学习j...

  • openCV3+VS2017学习笔记(一)

    从今天开始记录下学习openCV3+vs2017的过程吧,记录一些学习代码(大部分是书上的)和自己的理解,方便回顾...

  • 2018-09-05 写在前面的话

    emmm,这个相当于是自言自语,就是自己记录一下学习的心得,方便以后自己回顾。如果看到这些文章,有兴趣的朋友...

  • 搭建ELK日志系统

    一、环境准备 最近学习了下ELK系统,这里把自己搭建ELK的流程步骤记录下,以方便后面进行回顾。 因为本地资源原因...

  • 我的第一次简书

    主要是总结和记录用,方便回顾。 20180427

  • 机器学习

    本学习记录是记录我在参加知识圈的机器学习训练营的整个过程,旨在记录总结,同时方便以后查阅回顾。 第一周: 1. 随...

  • 中日同形异议词(1)勉强

    2020年,继续从事着汉语教学,辗转来到第四个国度,坚持记录,坚持分享。 学习语言,只为自己。为了让生活更方便一点...

  • 2018-06-11

    开始以笔记的形式记录自己在学习上的点点滴滴,目的是方便自己的日后回顾,同时也督促自己不要懈怠。我经常说生活需要些仪...

网友评论

      本文标题:学习记录(只为方便自己回顾)

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