You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The webpack config for an SSR project will be similar to a client-only project. If you're not familiar with configuring webpack, you can find more information in the documentation for [Vue CLI](https://cli.vuejs.org/guide/webpack.html#working-with-webpack)or [configuring Vue Loader manually](https://vue-loader.vuejs.org/guide/#manual-setup).
1.We need to create a [webpack manifest](https://webpack.js.org/concepts/manifest/)for our server-side code. This is a JSON file that webpack keeps to track how all the modules map to the output bundles.
2.We should [externalize application dependencies](https://webpack.js.org/configuration/externals/). This makes the server build much faster and generates a smaller bundle file. When doing this, we have to exclude dependencies that need to be processed by webpack (like `.css`. or`.vue`files).
3.We need to change webpack [target](https://webpack.js.org/concepts/targets/)to Node.js. This allows webpack to handle dynamic imports in a Node-appropriate fashion, and also tells `vue-loader`to emit server-oriented code when compiling Vue components.
4.When building a server entry, we would need to define an environment variable to indicate we are working with SSR. It might be helpful to add a few `scripts` to the project's `package.json`:
13
+
4.サーバエントリをビルドする際には、 SSR で動作することを示す環境変数を定義する必要があります。プロジェクトの `package.json` にいくつか `scripts` を追加すると便利かもしれません:
14
14
15
15
```json
16
16
"scripts": {
@@ -20,9 +20,9 @@ The webpack config for an SSR project will be similar to a client-only project.
20
20
}
21
21
```
22
22
23
-
## Example Configuration
23
+
## 設定例
24
24
25
-
Below is a sample `vue.config.js`that adds SSR rendering to a Vue CLI project, but it can be adapted for any webpack build.
25
+
以下は Vue CLI プロジェクトに SSR を追加する `vue.config.js`の例ですが、どのような webpack ビルドにも対応できます。
Notice that in the `externals`option we are whitelisting CSS files. This is because CSS imported from dependencies should still be handled by webpack. If you are importing any other types of files that also rely on webpack (e.g. `*.vue`, `*.sass`), you should add them to the whitelist as well.
If you are using `runInNewContext: 'once'`or`runInNewContext: true`, then you also need to whitelist polyfills that modify `global`, e.g. `babel-polyfill`. This is because when using the new context mode, **code inside a server bundle has its own `global`object.**Since you don't really need it on the server, it's actually easier to just import it in the client entry.
In addition to the server bundle, we can also generate a client build manifest. With the client manifest and the server bundle, the renderer now has information of both the server _and_ client builds. This way it can automatically infer and inject [preload / prefetch directives](https://css-tricks.com/prefetching-preloading-prebrowsing/),`<link>` and `<script>`tags into the rendered HTML.
100
+
サーバ用のバンドルに加えて、クライアント用のビルドマニフェストも生成できます。クライアント用マニフェストとサーバ用バンドルによって、レンダラーは _サーバとクライアント両方の_ ビルドの情報を持つことになります。これによりレンダリングされた HTML に [preload / prefetch ディレクティブ](https://css-tricks.com/prefetching-preloading-prebrowsing/)、`<link>`、 `<script>`タグを自動的に推測して注入することができます。
101
101
102
-
The benefits are two-fold:
102
+
利点は 2 つあります:
103
103
104
-
1.It can replace`html-webpack-plugin`for injecting the correct asset URLs when there are hashes in your generated filenames.
2.When rendering a bundle that leverages webpack's on-demand code splitting features, we can ensure the optimal chunks are preloaded / prefetched, and also intelligently inject `<script>`tags for needed async chunks to avoid waterfall requests on the client, thus improving TTI (time-to-interactive).
0 commit comments