Skip to content

Japanese Translation: ja/head.md #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions ja/head.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# ヘッドの管理

アセットの挿入と同様に、ヘッドの管理も同じ考えに追従しています。つまり、コンポーネントのライフサイクルのレンダリング `context` に動的にデータを付随させ、そして `template` 内にデータを挿入できるという考えです。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to asset injection, head management follows the same idea: we can dynamically attach data to the render context in a component's lifecycle, and then interpolate those data in template.

ここ... 自信がないです。意味としてが正しいかも自信がないし、日本語として読みやすいかも自信がない。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍


そうするためには、ネストしたコンポーネントの内側で SSR コンテキストへアクセスできる必要があります。単純に `context` を `createApp()` へ渡し、これをルートインスタンスの `$options` で公開することができます。

```js
// app.js
export function createApp (ssrContext) {
// ...
const app = new Vue({
router,
store,
// this.$root.$options.ssrContext というように、すべての子コンポーネントは this にアクセスできます
ssrContext,
render: h => h(App)
})
// ...
}
```

これと同様のことが `provide/inject` 経由でも可能ですが、そうすると context が `$root` 上に存在することになるため、インジェクションを解決するコストを避けたほうが良いでしょう。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be done via provide/inject, but since we know it's going to be on $root, we can avoid the injection resolution costs.

ここ、自信が無いので、重点的にみていただきたいです。特に後半の

but since we know it's going to be on $root, we can avoid the injection resolution costs.

が自信ないです。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍


インジェクトされた context を用いて、タイトルを管理する単純な mixin を書くことができます:

```js
// title-mixin.js
function getTitle (vm) {
// components can simply provide a `title` option
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのコメントの翻訳もお願いします! 🙏

// which can be either a string or a function
const { title } = vm.$options
if (title) {
return typeof title === 'function'
? title.call(vm)
: title
}
}
const serverTitleMixin = {
created () {
const title = getTitle(this)
if (title) {
this.$root.$options.ssrContext.title = title
}
}
}
const clientTitleMixin = {
mounted () {
const title = getTitle(this)
if (title) {
document.title = title
}
}
}
// VUE_ENV は webpack.DefinePlugin を使って挿入できます
export default process.env.VUE_ENV === 'server'
? serverTitleMixin
: clientTitleMixin
```

このようにすれば、ルートコンポーネントはドキュメントのタイトルをコントロールするために context を利用することができます。

```js
// Item.vue
export default {
mixins: [titleMixin],
title () {
return this.item.title
}
asyncData ({ store, route }) {
return store.dispatch('fetchItem', route.params.id)
},
computed: {
item () {
return this.$store.state.items[this.$route.params.id]
}
}
}
```

そしてタイトルは `template` 内でバンドルレンダラーに渡されます:

```html
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
...
</body>
</html>
```

**メモ:**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes:

他の箇所ではどのように訳しているか確認して、合わせます。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他の箇所では特になかったので、このまま行きます。


- XSS 攻撃を防ぐために double-mustache(HTML エスケープした挿入)を使うこと。
- レンダリング中にタイトルをセットするコンポーネントがない場合に備えて、`context` オブジェクトを作成する際にはデフォルトのタイトルをセットするようにすべきです。

---

同様のやり方で、この mixin を包括的にヘッドを管理するユーティリティに容易に拡張できます。