Giter Club home page Giter Club logo

vue-ssr's Introduction

Vue SSR

Vue.js 服务器端渲染指南

配合文章食用更佳 带你五步学会 Vue SSR 文章里有几个地方没处理,新同学容易踩坑,下面列出来了,但这依然是一篇很不错的 vue ssr 入门教程

  • 坑 1(第三步) 服务器端需要await异步等待 entry-server.js里的 promise.all 执行完所有组件asyncData方法异步获取数据后,再返回结果 不然就会出现:html 已经返回给浏览器,但是没数据
// 后端Server
backendRouter.get('/index', async (ctx, next) => {
    try {
        // 坑1、这里await异步等待 entry-server 里的 promise.all 执行完所有组件asyncData异步获取数据并返回结果
        const html = await new Promise((resolve, reject) => {
            renderer.renderToString((err, html) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(html);
                }
            });
        });
        ctx.type = 'html';
        ctx.status = 200;
        ctx.body = html;
    } catch (error) {
        console.error(error);
        ctx.status = 500;
        ctx.body = '服务器内部错误';
    }
});
// src/components/bar.vue
const fetchInitialData = ({ store }) => {
    // 注意组件里的fetchInitialData方法要return返回promise 不然上面entry-server里的 promise.all将毫无意义
    return store.dispatch('fetchBar');
};
<div id="“app”" data-server-rendered="“true”"></div>

data-server-rendered 特殊属性,让客户端 Vue 知道这部分 HTML 是由 Vue 在服务端渲染的,并且应该以激活模式进行挂载。注意,这里并没有添加 id=“app”,而是添加 data-server-rendered 属性:你需要自行添加 ID 或其他能够选取到应用程序根元素的选择器,否则应用程序将无法正常激活。

这里我们添加id="app"App.vue的根元素上(文章中没加)

<template>
	<div class="app-container" id="app">
		<!-- 其他dom元素或组件 -->
	</div>
</template>
  • 坑 3(第四步) 加上vue-ssr-client-manifest.json后,需要将template/index.ssr.html里面占位给html-webpack-plugin注入bundlescript去删掉,不然页面就会加载两份 client的代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>服务端渲染</title>
    </head>
    <body>
        <!--vue-ssr-outlet-->

        <!-- 删掉这一行 -->
        <script type="text/javascript" src="<%= htmlWebpackPlugin.options.files.js %>"></script>
    </body>
</html>
  • 坑 4(第 5 步) 在接入 vue-router 后,server/server.js 也要做些改造,文中没有讲到的

    // server/server.js的路由改造
    const handleBackendRoute = (ctx, next) => {
        console.log('ctx', ctx);
        console.log('url', ctx.url);
    
        let context = {
            url: ctx.url,
        };
    
        const ssrStream = renderer.renderToStream(context);
        ctx.status = 200;
        ctx.type = 'html';
        ctx.body = ssrStream;
    };
    
    // koa-router 9.0 不能使用通配符 * , 主要为了 `path-to-regexp` 为了兼容 Express <= 4.x
    // https://github.com/koajs/router/issues/76
    // https://github.com/pillarjs/path-to-regexp#compatibility-with-express--4x
    backendRouter.get('/', handleBackendRoute);
    backendRouter.get('/bar', handleBackendRoute);
    backendRouter.get('/foo', handleBackendRoute);
    
    // 前端路由也是同理

    koa-router 9+版本,为了兼容express--4X, 不能使用通配符* , 具体原因看 issue Using * wildcard no longer works

vue-ssr's People

Contributors

harukama avatar

Watchers

James Cloos avatar LGC avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.