|
| 1 | +# ルーティングとコード分割 |
| 2 | + |
| 3 | +## `vue-router` によるルーティング |
| 4 | + |
| 5 | +サーバーコードが任意の URL を受け入れる `*` ハンドラを使用していることに気付いたかもしれません。これにより訪れた URL を Vue アプリケーションに渡し、クライアントとサーバーの両方に同一のルーティング設定を再利用することが可能になります! |
| 6 | + |
| 7 | +この目的のために公式の `vue-router` を使用することが推奨されています。まずはルーターを作成するファイルを作成しましょう。 `createApp` に似ていますが、 リクエストごとに新たなルーターインスタンスも必要となるため、 `createRouter` 関数をエクスポートします。 |
| 8 | + |
| 9 | +```js |
| 10 | +// router.js |
| 11 | +import Vue from 'vue' |
| 12 | +import Router from 'vue-router' |
| 13 | +Vue.use(Router) |
| 14 | +export function createRouter () { |
| 15 | + return new Router({ |
| 16 | + mode: 'history', |
| 17 | + routes: [ |
| 18 | + // ... |
| 19 | + ] |
| 20 | + }) |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +そして `app.js` を更新します。 |
| 25 | + |
| 26 | +```js |
| 27 | +// app.js |
| 28 | +import Vue from 'vue' |
| 29 | +import App from './App.vue' |
| 30 | +import { createRouter } from './router' |
| 31 | +export function createApp () { |
| 32 | + // create router instance |
| 33 | + const router = createRouter() |
| 34 | + const app new Vue({ |
| 35 | + // inject router into root Vue instance |
| 36 | + router, |
| 37 | + render: h => h(App) |
| 38 | + }) |
| 39 | + // return both the app and the router |
| 40 | + return { app, router } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +`entry-server.js` にサーバー側のルーティングロジックを実装する必要があります。 |
| 45 | + |
| 46 | +```js |
| 47 | +// entry-server.js |
| 48 | +import { createApp } from './app' |
| 49 | +export default context => { |
| 50 | + // since there could potentially be asynchronous route hooks or components, |
| 51 | + // we will be returning a Promise so that the server can wait until |
| 52 | + // everything is ready before rendering. |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + const { app, router } = createApp() |
| 55 | + // set server-side router's location |
| 56 | + router.push(context.url) |
| 57 | + // wait until router has resolved possible async components and hooks |
| 58 | + router.onReady(() => { |
| 59 | + const matchedComponents = router.getMatchedComponents() |
| 60 | + // no matched routes, reject with 404 |
| 61 | + if (!matchedComponents.length) { |
| 62 | + reject({ code: 404 }) |
| 63 | + } |
| 64 | + // the Promise should resolve to the app instance so it can be rendered |
| 65 | + resolve(app) |
| 66 | + }, reject) |
| 67 | + }) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +サーバーバンドルがすでにビルドされていると仮定すると(再度になりますが、今はビルド設定は無視します)、サーバーでの使用方法は次のようになります。 |
| 72 | + |
| 73 | +```js |
| 74 | +// server.js |
| 75 | +const createApp = require('/path/to/built-server-bundle.js') |
| 76 | +server.get('*', (req, res) => { |
| 77 | + const context = { url: req.url } |
| 78 | + createApp(context).then(app => { |
| 79 | + renderer.renderToString(app, (err, html) => { |
| 80 | + if (err) { |
| 81 | + if (err.code === 404) { |
| 82 | + res.status(404).end('Page not found') |
| 83 | + } else { |
| 84 | + res.status(500).end('Internal Server Error') |
| 85 | + } |
| 86 | + } else { |
| 87 | + res.end(html) |
| 88 | + } |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
| 92 | +``` |
| 93 | + |
| 94 | +## コード分割 |
| 95 | + |
| 96 | +コード分割やアプリケーションの部分的な遅延ローディングは初期レンダリングのためにブラウザがダウンロードする必要のあるアセットの量を減らすのに役立ち、巨大なバンドルを持つアプリケーションの TTI (操作可能になるまでの時間)を大幅に改善します。重要なことは初期画面では"必要なものだけを読み込む"ということです。 |
| 97 | + |
| 98 | +Vue は非同期コンポーネントを最重要コンセプトとして提供しており、 [webpack 2の動的インポートをコード分割点として使用することへのサポート](https://webpack.js.org/guides/code-splitting-async/) と組み合わせることも可能です。そのためにすべきことは以下です。 |
| 99 | + |
| 100 | +```js |
| 101 | +// changing this... |
| 102 | +import Foo from './Foo.vue' |
| 103 | +// to this: |
| 104 | +const Foo = () => import('./Foo.vue') |
| 105 | +``` |
| 106 | + |
| 107 | +純粋なクライアントサイドの Vue アプリケーションを構築する場合、これはどんなシナリオでも機能するでしょう。ただし、これをサーバーサイドレンダリングで使用する場合はいくつかの制限があります。まず、レンダリングを開始する前にサーバー上のすべての非同期コンポーネントを先に解決する必要があります。そうしなければ、マークアップ内に空のプレースホルダが表示されます。クライアント側では、ハイドレーションを開始する前にこれを行う必要があります。そうしなければ、クライアントはコンテンツの不一致エラーに陥ります。 |
| 108 | + |
| 109 | +アプリケーション内の任意の場所で非同期コンポーネントを使用するのは少し難解です(これは将来的に改善される可能性があります)。 ただし、**ルートレベルで行うとシームレスに動作します**(すなわち、ルート設定で非同期コンポーネントを使用する)。ルートを解決する際に、 `vue-router` は一致した非同期コンポーネントを自動的に解決するためです。 必要なことは、サーバーとクライアントの両方で `router.onReady` を使用することです。すでにサーバーのエントリーで行ったので、クライアントのエントリーを更新するだけです。 |
| 110 | + |
| 111 | +```js |
| 112 | +// entry-client.js |
| 113 | +import { createApp } from './app' |
| 114 | +const { app, router } = createApp() |
| 115 | +router.onReady(() => { |
| 116 | + app.$mount('#app') |
| 117 | +}) |
| 118 | +``` |
| 119 | + |
| 120 | +非同期ルートコンポーネントを使用したルート設定の例: |
| 121 | + |
| 122 | +```js |
| 123 | +// router.js |
| 124 | +import Vue from 'vue' |
| 125 | +import Router from 'vue-router' |
| 126 | +Vue.use(Router) |
| 127 | +export function createRouter () { |
| 128 | + return new Router({ |
| 129 | + mode: 'history', |
| 130 | + routes: [ |
| 131 | + { path: '/', component: () => import('./components/Home.vue') }, |
| 132 | + { path: '/item/:id', component: () => import('./components/Item.vue') } |
| 133 | + ] |
| 134 | + }) |
| 135 | +} |
| 136 | +``` |
0 commit comments