diff --git a/src/guide/ssr.md b/src/guide/ssr.md index c46021f43..d36403175 100644 --- a/src/guide/ssr.md +++ b/src/guide/ssr.md @@ -1,41 +1,41 @@ --- -title: Server-Side Rendering +title: サーバサイドレンダリング type: guide order: 23 --- -## Do You Need SSR? +## SSR が必要ですか ? -Before diving into SSR, let's explore what it actually does for you and when you might need it. +SSR について知る前に、それが一体何をするもので、どのようなケースにおいて必要になるのか考えてみましょう。 ### SEO -Google and Bing can index synchronous JavaScript applications just fine. _Synchronous_ being the key word there. If your app starts with a loading spinner, then fetches content via Ajax, the crawler will not wait for you to finish. +Google や Bing は、同期的な JavaScript のアプリケーションを上手にインデックスしてくれます。 _同期的な_ というのが重要で、もしあなたのアプリケーションが、ローディングのスピナーから始まり、 Ajax 経由でコンテンツを取得しようとするならば、クローラはローディングの完了を待ってくれないでしょう。 -This means if you have content fetched asynchronously on pages where SEO is important, SSR might be necessary. +非同期に取得されるページ上のコンテンツが SEO 上で重要な意味をもつ場合、 SSR が必要になるでしょう。 -### Clients with a Slow Internet +### 低速なインターネット環境 -Users might come to your site from a remote area with slow Internet - or just with a bad cell connection. In these cases, you'll want to minimize the number and size of requests necessary for users to see basic content. +あなたのサイトを訪れるユーザの中には、低速なインターネット回線を利用する人や、不安定なモバイル回線を利用している人がいるかもしれません。このようなケースでは、基本的なコンテンツを見せるために必要なリクエストを、数、量共に減らしたいと考えるでしょう。 -You can use [Webpack's code splitting](https://webpack.github.io/docs/code-splitting.html) to avoid forcing users to download your entire application to view a single page, but it still won't be as performant as downloading a single, pre-rendered HTML file. +[ Webpack のコード分割](https://webpack.github.io/docs/code-splitting.html) を利用して、ユーザが単一のページを閲覧するためにアプリケーションの全体をダウンロードしなければならないという事情を解消することも出来ます。が、サーバサイドで事前に描画された HTML ファイルのダウンロードはそれよりももっと大きなパフォーマンスを導くことが出来ます。 -### Clients with an Old (or Simply No) JavaScript Engine +### 古い JavaScript 環境、または JavaScript の利用できない環境 -For some demographics or areas of the world, using a computer from 1998 to access the Internet might be the only option. While Vue only works with IE9+, you may still want to deliver basic content to those on older browsers - or to hipster hackers using [Lynx](http://lynx.browser.org/) in the terminal. +一部の人々やとある地域では、 1998 年から稼働するコンピュータを用いてインターネットにアクセスする、という様なケースも考えられるでしょう。 Vue は IE9+ 以上の環境でのみ動作しますが、それよりも古いブラウザ環境にコンテンツを配信したいケースや、ターミナルで [Lynx](http://lynx.browser.org/) を利用するような、最先端の技術者にコンテンツを配信したいケースなども考えられます。 -### SSR vs Prerendering +### SSR vs プリレンダリング -If you're only investigating SSR to improve the SEO of a handful of marketing pages (e.g. `/`, `/about`, `/contact`, etc), then you probably want __prerendering__ instead. Rather than using a web server to compile HTML on-the-fly, prerendering simply generates static HTML files for specific routes at build time. The advantage is setting up prerendering is much simpler and allows you to keep your frontend as a fully static site. +もしあなたが 一部の商用ページ(例えば `/`, `/about`, `/contact`, など)の SEO を改善するために、 SSR を調べようとしているなら、 おそらく __プリレンダリング__ が代わりに役立つでしょう。 レスポンス発行前に HTML をコンパイルするためにWeb サーバを利用するのに比べ、プリレンダリングは単にビルド時に特定のルートで静的な HTML を生成するだけです。プリレンダリングの利便性は、準備がシンプルな点とフロントエンドを完全に静的な構成で保つことが出来るという点にあります。 -If you're using Webpack, you can easily add prerendering with the [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin). It's been extensively tested with Vue apps - and in fact, the creator is a member of the Vue core team. +もし Webpack を使用しているなら、 [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin) を用いてプリレンダリングの構成を整えることが出来ます。 Vue アプリでの動作も広くテストされていて - 実際のところ、 Vue のコアチームのメンバーが、 prerender-spa-plugin の制作を行っていたりします。 ## Hello World -If you've gotten this far, you're ready to see SSR in action. It sounds complex, but a simple node script demoing the feature requires only 3 steps: +ようやく SSR を始める準備が出来ました。複雑に聞こえるかもしれませんが、デモで使用する基本的な node のスクリプトはたった3つのステップでできています: ``` js -// Step 1: Create a Vue instance +// Step 1: Vue インスタンスの生成 var Vue = require('vue') var app = new Vue({ render: function (h) { @@ -43,10 +43,10 @@ var app = new Vue({ } }) -// Step 2: Create a renderer +// Step 2: renderer の生成 var renderer = require('vue-server-renderer').createRenderer() -// Step 3: Render the Vue instance to HTML +// Step 3: Vue instance を描画し HTML に変換 renderer.renderToString(app, function (error, html) { if (error) throw error console.log(html) @@ -54,22 +54,22 @@ renderer.renderToString(app, function (error, html) { }) ``` -Not so scary, right? Of course, this example is much simpler than most applications. We don't yet have to worry about: +どうです?怖くないでしょう?もちろんこの例は大半のアプリケーションより随分とシンプルなものです。まだこの段階では、次のような事は考えなくても良いでしょう: -- A Web Server -- Response Streaming -- Component Caching -- A Build Process -- Routing -- Vuex State Hydration +- Web サーバについて +- Response ストリーミング +- コンポーネントのキャッシュ +- ビルドの仕組みについて +- ルーティング +- Vuex State とのハイドレーション(Hydration) -In the rest of this guide, we'll walk through how to work with some of these features. Once you understand the basics, we'll then direct you to more detailed documentation and advanced examples to help you handle edge cases. +このガイドの残りの節で、これらの機能をどのように実装していくかを解説していきます。基本が理解できた所で、より詳しい解説へと応用的な例へと進み、特殊なケースへの対応を理解していきましょう。 -## Simple SSR with the Express Web Server +## Express を利用したシンプルなサーバサイドレンダリング -It's kind of a stretch to call it "server-side rendering" when we don't actually have a web server, so let's fix that. We'll build a very simple SSR app, using only ES5 and without any build step or Vue plugins. +Web サーバもなしにサーバサイドレンダリングというのはおかしな気もするので、まずはそこから解決していきましょう。特別なビルドの手続も Vue のプラグインも使わず ES5 のスクリプトのみで書かれたシンプルな SSR のアプリケーションを構築していきます。 -We'll start off with an app that just tells the user how many seconds they've been on the page: +まずは、ページに滞在している秒数を知らせてくれるだけのアプリケーションから考えてみます。 ``` js new Vue({ @@ -86,24 +86,24 @@ new Vue({ }) ``` -To adapt this for SSR, there are a few modifications we'll have to make, so that it will work both in the browser and within node: +これを SSR に適用する場合、ブラウザと node の両方で動作させられるよう、少しの修正が必要になります。 -- When in the browser, add an instance of our app to the global context (i.e. `window`), so that we can mount it. -- When in node, export a factory function so that we can create a fresh instance of the app for every request. +- ブラウザでは、操作可能なようにアプリケーションをグローバルのコンテキスト(例えば `window` )に配置したりします。 +- node では、各リクエスト毎に新しいアプリケーションのインスタンスを作成するよう、ファクトリ関数をエクスポートします。 -Accomplishing this requires a little boilerplate: +このような実装のために次のような例が必要になります: ``` js // assets/app.js (function () { 'use strict' var createApp = function () { // --------------------- - // BEGIN NORMAL APP CODE + // 通常のアプリケーションコード // --------------------- - // Main Vue instance must be returned and have a root - // node with the id "app", so that the client-side - // version can take over once it loads. + // クライアントサイドのコードが読み込み後にそれを引き継げるよう、 + // id "app"をルートノードにもつメインの Vue インスタンスが + // 返却されなければなりません。 return new Vue({ template: '