得得爱铺 - 开源项目组织

vue3自己实现SSR服务器端渲染

ssr通过服务器端渲染,优化SEO抓取,提示加载速度。

通过本文,我们一步一步完成SSR配置

先实现浏览器渲染

就是基于webpack+vue的常规开发配置

vue
import Vue from 'vue';
import App from './App.vue';
 
let app = new Vue({
  el: '#app',
  render: h => h(App)
});
webpack.config.js
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
 
module.exports = {
  mode: 'development',
 
  entry: './app.js',
 
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
 
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: ['vue-style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.(jpg|jpeg|png|gif|svg)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000    // 10Kb
          }
        }
      },
      {
        test: /\.vue$/,
        use: 'vue-loader'
      }
    ]
  },
 
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: './index.html'
    }),
  ]
};

启动 npm run build & npm run start

服务器端渲染,不包含AJAX数据

- node_modules
- config    // 新增
    - webpack.base.config.js
    - webpack.client.config.js
    - webpack.server.config.js
- src
    - components
        - Bar.vue
        - Foo.vue
    - App.vue
    - app.js
    - entry-client.js   // 新增
    - entry-server.js   // 新增
    - index.html
    - index.ssr.html    // 新增
- package.json
- yarn.lock
- postcss.config.js
- .babelrc
- .gitignore
上一篇 下一篇