Skip to content

Commit ce10c32

Browse files
committed
fix moduslabs#7, which contains 2 div element with same id;
make the name of file more impressive; add activation of client side in ssr; add compiler config of client side in ssr;
1 parent 7c4e147 commit ce10c32

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"serve": "vue-cli-service serve",
77
"build": "vue-cli-service build",
8-
"pressr": "cross-env SSR=true vue-cli-service build",
8+
"pressr-client": "cross-env SSR=true CLIENT=true vue-cli-service build",
9+
"pressr-server": "cross-env SSR=true vue-cli-service build",
10+
"pressr": "pnpm run pressr-server && pnpm run pressr-client",
911
"ssr": "node src/server.js",
1012
"lint": "vue-cli-service lint"
1113
},
File renamed without changes.

src/App.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div id="app">
2+
<div id="main">
33
<img alt="Vue logo" src="./assets/logo.png">
44
<HelloWorld msg="Welcome to Your Vue.js App"/>
55
</div>
@@ -17,7 +17,7 @@ export default {
1717
</script>
1818

1919
<style>
20-
#app {
20+
#main {
2121
font-family: Avenir, Helvetica, Arial, sans-serif;
2222
-webkit-font-smoothing: antialiased;
2323
-moz-osx-font-smoothing: grayscale;

src/server.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const express = require("express");
33
const { createSSRApp } = require("vue");
44
const { renderToString } = require("@vue/server-renderer");
55
const manifest = require("../dist/ssr-manifest.json");
6+
const clientManifest = require("../client-dist/ssr-manifest.json");
67

78
const server = express();
89

910
const appPath = path.join(__dirname, "../dist", manifest["app.js"]);
11+
const clientAppPath = path.join(__dirname, "../client-dist", clientManifest["client.js"]);
1012
const App = require(appPath).default;
1113

1214
server.use("/img", express.static(path.join(__dirname, "../dist", "img")));
@@ -16,6 +18,10 @@ server.use(
1618
"/favicon.ico",
1719
express.static(path.join(__dirname, "../dist", "favicon.ico"))
1820
);
21+
server.use(
22+
"/client.js",
23+
express.static(clientAppPath)
24+
);
1925

2026
server.get("*", async (req, res) => {
2127
const app = createSSRApp(App);
@@ -26,9 +32,10 @@ server.get("*", async (req, res) => {
2632
<head>
2733
<title>Hello</title>
2834
<link rel="stylesheet" href="${manifest["app.css"]}" />
35+
<script src="/client.js"></script>
2936
</head>
3037
<body>
31-
${appContent}
38+
<div id="app">${appContent}</div>
3239
</body>
3340
</html>
3441
File renamed without changes.

src/ssr-client.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createSSRApp } from "vue";
2+
import App from "./App.vue";
3+
4+
5+
createSSRApp(App).mount("#app")

vue.config.js

+62-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
const ManifestPlugin = require("webpack-manifest-plugin");
22
const nodeExternals = require("webpack-node-externals");
33

4+
const isClient = process.env.CLIENT === "true";
5+
const isSSR = process.env.SSR === "true";
6+
7+
/**
8+
* change the output dir for compiled files
9+
* https://github.com/vuejs/vue-cli/issues/2202
10+
*/
11+
if (isClient) {
12+
exports.outputDir = "client-dist";
13+
}
14+
415
exports.chainWebpack = webpackConfig => {
5-
if (!process.env.SSR) {
16+
if (!isSSR) {
617
// This is required for repl.it to play nicely with the Dev Server
718
webpackConfig.devServer.disableHostCheck(true);
819
return;
920
}
1021

22+
/**
23+
* By default, vue-cli-service uses "app" as entry point for webpack,
24+
* and the corresponding entry point file is "./src/main.js".
25+
*
26+
* When we compile the file on ssr client side, we just allow "client" as
27+
* the only entry point for webpack, and the corresponding entry point
28+
* file is "./src/ssr-client.js", so we should delete "app", otherwise,
29+
* vue-cli-service will be failed because of the lack of "./src/main.js"
30+
*/
1131
webpackConfig
12-
.entry("app")
13-
.clear()
14-
.add("./src/main.server.js");
32+
.entryPoints
33+
.delete("app");
34+
1535

16-
webpackConfig.target("node");
17-
webpackConfig.output.libraryTarget("commonjs2");
36+
if (isClient) {
37+
/**
38+
* Compile ./src/ssr-client.js, and we will response the compiled file
39+
* to browser in ./src/server.js.We won't import vue separately, so we
40+
* also need to bundle vue into the compiled file.
41+
*/
42+
webpackConfig
43+
.entry("client")
44+
.clear()
45+
.add("./src/ssr-client.js");
46+
} else if (isSSR) {
47+
/**
48+
* Compile ./src/App.js, so we could require it in ./src/server.js.
49+
*
50+
* We already require "vue" in ./src/server.js, so we don't need to
51+
* bundle it, but we still need to compile and bundle .css or .vue files,
52+
* so we add `nodeExternals` below.
53+
*/
54+
webpackConfig
55+
.entry("app")
56+
.clear()
57+
.add("./src/App.js");
1858

59+
webpackConfig.target("node");
60+
webpackConfig.output.libraryTarget("commonjs2");
61+
webpackConfig.externals(nodeExternals({ allowlist: /\.(css|vue)$/ }));
62+
63+
} else {
64+
/**
65+
* Compile for SPA
66+
*/
67+
webpackConfig
68+
.entry("app")
69+
.clear()
70+
.add("./src/spa-client.js");
71+
}
72+
1973
webpackConfig
2074
.plugin("manifest")
2175
.use(new ManifestPlugin({ fileName: "ssr-manifest.json" }));
2276

23-
webpackConfig.externals(nodeExternals({ allowlist: /\.(css|vue)$/ }));
77+
2478

2579
webpackConfig.optimization.splitChunks(false).minimize(false);
2680

@@ -31,4 +85,4 @@ exports.chainWebpack = webpackConfig => {
3185
webpackConfig.plugins.delete("friendly-errors");
3286

3387
// console.log(webpackConfig.toConfig())
34-
};
88+
};

0 commit comments

Comments
 (0)