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
Copy file name to clipboardExpand all lines: src/guide/ssr/structure.md
+25-25Lines changed: 25 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
# Source Code Structure
1
+
# ソースコードの構造
2
2
3
-
## Avoid Stateful Singletons
3
+
## ステートフルなシングルトンの回避
4
4
5
-
When writing client-only code, we can assume that our code will be evaluated in a fresh context every time. However, a Node.js server is a long-running process. When our code is first imported by the process, it will be evaluated once and then stay in memory. This means that if you create a singleton object, it will be shared between every incoming request, with the risk of cross-request state pollution.
Therefore, we need to **create a new root Vue instance for each request.**In order to do that, we need to write a factory function that can be repeatedly executed to create fresh app instances for each request:
The same rule applies to other instances as well (such as the router or store). Instead of exporting the router or store directly from a module and importing it across your app, you should create a fresh instance in `createApp`and inject it from the root Vue instance.
-We need to process the server code with webpack. For example, `.vue`files need to be processed with `vue-loader`, and many webpack-specific features such as importing files via `file-loader`or importing CSS via `css-loader`do not work directly in Node.js.
-Similarly, we need a separate client-side build because although the latest version of Node.js fully supports ES2015 features, older browsers will require the code to be transpiled.
So the basic idea is that we will use webpack to bundle our app for both client and server. The server bundle will be required on the server and used to render static HTML, while the client bundle will be sent to the browser to hydrate the static markup.
62
+
基本的な考え方は、 webpack でアプリケーションをクライアントとサーバの両方にバンドルすることです。サーバ用のバンドルは、サーバ上で静的な HTML をレンダリングするために使われ、クライアント用のバンドルは、静的なマークアップを Hydrate(ハイドレート)するためにブラウザに送信されます。
We will discuss the details of the setup in later sections - for now, let's just assume we've got the build setup figured out and we can write our Vue app code with webpack enabled.
Now that we are using webpack to process the app for both server and client, the majority of our source code can be written in a universal fashion, with access to all the webpack-powered features. At the same time, there are a number of things you should keep in mind when [writing universal code](./universal.html).
`app.js`is the universal entry to our app. In a client-only app, we would create the Vue application instance right in this file and mount directly to DOM. However, for SSR that responsibility is moved into the client-only entry file. `app.js`instead creates an application instance and exports it:
87
+
`app.js`は、アプリケーションの共通のエントリです。クライアント専用のアプリケーションでは、このファイルの中で Vue アプリケーションのインスタンスを作成して、 DOM に直接マウントします。しかし SSR では、その責務はクライアント専用のエントリファイルに移されます。代わりに `app.js`でアプリケーションのインスタンスを作成して、エクスポートします:
88
88
89
89
```js
90
90
import { createSSRApp } from'vue'
91
91
importAppfrom'./App.vue'
92
92
93
-
//export a factory function for creating a root component
The client entry creates the application using the root component factory and mounts it to the DOM:
105
+
クライアント用のエントリは、ルートコンポーネントのファクトリを使ってアプリケーションを作成し、 DOM にマウントします:
106
106
107
107
```js
108
108
importcreateAppfrom'./app'
109
109
110
-
//client-specific bootstrapping logic...
110
+
//クライアント固有の初回起動ロジック
111
111
112
112
const { app } =createApp({
113
-
//here we can pass additional arguments to app factory
113
+
//ここでアプリケーションのファクトリに追加の引数を渡すことが可能
114
114
})
115
115
116
-
//this assumes App.vue template root element has `id="app"`
116
+
//これは App.vue テンプレートのルート要素に `id="app"` が前提
117
117
app.mount('#app')
118
118
```
119
119
120
120
### `entry-server.js`
121
121
122
-
The server entry uses a default export which is a function that can be called repeatedly for each render. At this moment, it doesn't do much other than returning the app instance - but later we will perform server-side route matching and data pre-fetching logic here.
0 commit comments