|
1 | | -# Getting Started with Service Workers |
| 1 | +# Service Workerを始める |
2 | 2 |
|
3 | | -#### Prerequisites |
| 3 | +#### 前提条件 |
4 | 4 |
|
5 | | -A basic understanding of the following: |
6 | | -* [Introduction to Angular service workers](guide/service-worker-intro). |
| 5 | +次の基本的理解があること |
| 6 | +* [Angular Service Worker イントロダクション](guide/service-worker-intro) |
7 | 7 |
|
8 | 8 | <hr /> |
9 | 9 |
|
10 | 10 |
|
11 | | -Beginning in Angular 5.0.0, you can easily enable Angular service worker support in any CLI project. This document explains how to enable Angular service worker support in new and existing projects. It then uses a simple example to show you a service worker in action, demonstrating loading and basic caching. |
| 11 | +Angular 5.0.0から、どのCLIプロジェクトでもAngular Service Workerのサポートを簡単に有効にすることができます。このドキュメントでは、新規および既存のプロジェクトでAngular Service Workerのサポートを有効にする方法について説明します。次に、単純な例を使用して、Service Workerが実際に動作していることを確認し、読み込みとキャッシングの基本をデモで行います。 |
12 | 12 |
|
13 | | -## Adding a service worker to a new application |
| 13 | +## 新しいアプリケーションにService Workerを追加する |
14 | 14 |
|
15 | | -If you're generating a new CLI project, you can use the CLI to set up the Angular service worker as part of creating the project. To do so, add the `--service-worker` flag to the `ng new` command: |
| 15 | +新しくCLIプロジェクトを作る場合は、CLIを使用して、プロジェクト作成の一環としてAngular Service Workerを設定できます。これを行うには、`--service-worker`フラグを`ng new`コマンドに追加します。 |
16 | 16 |
|
17 | 17 | ```sh |
18 | 18 | ng new my-project --service-worker |
19 | 19 | ``` |
20 | 20 |
|
21 | | -The `--service-worker` flag takes care of configuring your app to |
22 | | -use service workers by adding the `service-worker` package along |
23 | | -with setting up the necessary files to support service workers. |
24 | | -For information on the details, see the following section |
25 | | -which covers the process in detail as it shows you how to add a |
26 | | -service worker manually to an existing app. |
| 21 | +`--service-worker`フラグにより、`service-worker`パッケージを追加して、Service Workerを使うために必要なファイルをアプリケーションに設定します。詳細については、次のセクションを参照してください。Service Workerを既存のアプリに手動で追加するために、プロセスを詳しく説明しています。 |
27 | 22 |
|
28 | 23 |
|
29 | 24 |
|
30 | | -## Adding a service worker to an existing app |
| 25 | +## 既存のアプリケーションにService Workerを追加する |
31 | 26 |
|
32 | | -To add a service worker to an existing app: |
| 27 | +既存のアプリケーションにService Workerを追加するためには、次のステップになります。 |
33 | 28 |
|
34 | | -1. Add the service worker package. |
35 | | -2. Enable service worker build support in the CLI. |
36 | | -3. Import and register the service worker. |
37 | | -4. Create the service worker configuration file, which specifies the caching behaviors and other settings. |
38 | | -5. Build the project. |
| 29 | +1. Service Workerのパッケージを追加します。 |
| 30 | +2. CLIでService Workerのビルドサポートを有効にします。 |
| 31 | +3. Service Workerをインポートして登録します。 |
| 32 | +4. キャッシングビヘイビアなどの設定を指定するService Workerの設定ファイルを作成します。 |
| 33 | +5. プロジェクトをビルドします。 |
39 | 34 |
|
40 | | -### Step 1: Add the service worker package |
| 35 | +### ステップ1: Service Workerのパッケージを追加します |
41 | 36 |
|
42 | | -Add the package `@angular/service-worker`, using the yarn utility as shown here: |
| 37 | +次のように、yarnを使用して、`@angular/service-worker`パッケージを追加してください。 |
43 | 38 |
|
44 | 39 | ```sh |
45 | 40 | yarn add @angular/service-worker |
46 | 41 | ``` |
47 | 42 |
|
48 | | -### Step 2: Enable service worker build support in the CLI |
| 43 | +### ステップ2: CLIでService Workerのビルドサポートを有効にします |
49 | 44 |
|
50 | | -To enable the Angular service worker, the CLI must generate an Angular service worker manifest at build time. To cause the CLI to generate the manifest for an existing project, set the `serviceWorker` flag to `true` in the project's `.angular-cli.json` file as shown here: |
| 45 | +AngularにおけるService Workerを有効にするために、CLIはビルド時にAngular Service Workerマニフェストを生成する必要があります。CLIが既存のプロジェクトでマニフェストを生成するようにするためには、プロジェクトの`.angular-cli.json`ファイルで`serviceWorker`フラグを`true`に設定します。 |
51 | 46 |
|
52 | 47 | ```sh |
53 | 48 | ng set apps.0.serviceWorker=true |
54 | 49 | ``` |
55 | 50 |
|
56 | | -### Step 3: Import and register the service worker |
| 51 | +### ステップ3: Service Workerをインポートして登録します |
57 | 52 |
|
58 | | -To import and register the Angular service worker: |
| 53 | +AngularにおけるService Workerをインポートして登録するために |
59 | 54 |
|
60 | | -At the top of the root module, `src/app/app.module.ts`, import `ServiceWorkerModule` and `environment`. |
| 55 | +ルートモジュール`src/app/app.module.ts`のトップで、`ServiceWorkerModule`と`environment`をインポートしてください。 |
61 | 56 |
|
62 | 57 | <code-example path="service-worker-getting-started/src/app/app.module.ts" linenums="false" title="src/app/app.module.ts" region="sw-import"> </code-example> |
63 | 58 |
|
64 | 59 |
|
65 | | -Add `ServiceWorkerModule` to the `@NgModule` `imports` array. Use the `register()` helper to take care of registering the service worker, taking care to disable the service worker when not running in production mode. |
| 60 | +`ServiceWorkerModule`を`@NgModule`の`imports`配列に追加してください。プロダクションモードで実行していないときに、Service Workerを無効にするように登録するためには、`register()`ヘルパーを使用してください。 |
66 | 61 |
|
67 | 62 | <code-example path="service-worker-getting-started/src/app/app.module.ts" linenums="false" title="src/app/app.module.ts" region="sw-module"> </code-example> |
68 | 63 |
|
69 | | -The file `ngsw-worker.js` is the name of the prebuilt service worker script, which the CLI copies into `dist/` to deploy along with your server. |
| 64 | +`ngsw-worker.js`ファイルは、CLIがあなたのサーバーに一緒にデプロイするために`dist/`にコピーする、事前構築されたService Workerのスクリプト名称です。 |
70 | 65 |
|
71 | | -### Step 4: Create the configuration file, `ngsw-config.json` |
| 66 | +### ステップ4: 設定ファイル`ngsw-config.json`を作成します |
72 | 67 |
|
73 | | -The Angular CLI needs a service worker configuration file, called `ngsw-config.json`. The configuration file controls how the service worker caches files and data |
74 | | -resources. |
| 68 | +Angular CLIには、`ngsw-config.json`というService Workerの設定ファイルが必要です。設定ファイルは、Service Workerがファイルおよびデータリソースをどのようにキャッシュするかを制御します。 |
75 | 69 |
|
76 | | -You can begin with the boilerplate version from the CLI, which configures sensible defaults for most applications. |
| 70 | +CLIからボイラープレート版を始めることができます。このボイラープレート版では、多くのアプリケーションで適切であろう推奨設定が行われています。 |
77 | 71 |
|
78 | | -Alternately, save the following as `src/ngsw-config.json`: |
| 72 | +代わりに、以下を`src/ngsw-config.json`として保存してください。 |
79 | 73 |
|
80 | 74 | <code-example path="service-worker-getting-started/src/ngsw-config.json" linenums="false" title="src/ngsw-config.json"> </code-example> |
81 | 75 |
|
82 | | -### Step 5: Build the project |
| 76 | +### ステップ5: プロジェクトをビルドします |
83 | 77 |
|
84 | | -Finally, build the project: |
| 78 | +最後にプロジェクトをビルドします。 |
85 | 79 |
|
86 | 80 | ```sh |
87 | 81 | ng build --prod |
88 | 82 | ``` |
89 | 83 |
|
90 | | -The CLI project is now set up to use the Angular service worker. |
| 84 | +CLIプロジェクトはAngularにおけるService Workerを使用するように設定されました。 |
91 | 85 |
|
92 | 86 |
|
93 | | -## Service worker in action: a tour |
| 87 | +## Service Workerを動かす: ツアー |
94 | 88 |
|
95 | | -This section demonstrates a service worker in action, |
96 | | -using an example application. |
| 89 | +このセクションでは、サンプルアプリケーションを使用して、Service Workerが実際に動かします。 |
97 | 90 |
|
98 | | -### Serving with `http-server` |
| 91 | +### `http-server`を供給する |
99 | 92 |
|
100 | | -Because `ng serve` does not work with service workers, you must use a seperate HTTP server to test your project locally. You can use any HTTP server. The example below uses the [http-server](https://www.npmjs.com/package/http-server) package from npm. To reduce the possibility of conflicts, test on a dedicated port. |
| 93 | +`ng serve`はService Workerでは機能しないので、プロジェクトをローカルでテストするためには、別個のHTTPサーバーを使用する必要があります。任意のHTTPサーバーを使用できます。次の例では、npmから[http-server](https://www.npmjs.com/package/http-server)パッケージを使用しています。競合する可能性を減らすために、専用ポートでテストしてください。 |
101 | 94 |
|
102 | | -To serve with `http-server`, change to the directory containing your web files and start the web server: |
| 95 | +`http-server`で供給するために、Webファイルが入っているディレクトリ(訳注:`ng build --prod`で作成されたフォルダ。通常なら`dist`)に移動し、Webサーバーを起動してください。 |
103 | 96 |
|
104 | 97 | ```sh |
105 | 98 | cd dist |
106 | 99 | http-server -p 8080 |
107 | 100 | ``` |
108 | 101 |
|
109 | | -### Initial load |
| 102 | +### 最初の読み込み |
110 | 103 |
|
111 | | -With the server running, you can point your browser at http://localhost:8080/. Your application should load normally. |
| 104 | +サーバーが稼動している状態なら、ブラウザーでhttp://localhost:8080/を指定できます。アプリケーションは正常に読み込まれるでしょう。 |
112 | 105 |
|
113 | | -**Tip:** When testing Angular service workers, it's a good idea to use an incognito or private window in your browser to ensure the service worker doesn't end up reading from a previous leftover state, which can cause unexpected behavior. |
| 106 | +**Tip:** Angular Service Workerをテストするときは、ブラウザでシークレットウィンドウまたはプライベートウィンドウを使うことをお勧めします。以前の状態が残存していると、Service Workerが読み取ったときに予期しない動作を引き起こし終了する可能性があるからです。 |
114 | 107 |
|
115 | | -### Simulating a network issue |
| 108 | +### ネットワークの問題をシミュレートする |
116 | 109 |
|
117 | | -To simulate a network issue, disable network interaction for your application. In Chrome: |
| 110 | +ネットワークの問題をシミュレートするには、アプリケーションのネットワーク操作を無効にします。Chromeの場合は次になります。 |
118 | 111 |
|
119 | | -1. Select **Tools** > **Developer Tools** (from the Chrome menu located at the top right corner). |
120 | | -2. Go to the **Network tab**. |
121 | | -3. Check the **Offline box**. |
| 112 | +1. ツールバーの右にあるメニューから**その他のツール** > **デベロッパー ツール**を選びます。 |
| 113 | +2. **Networkタブ**を選びます。 |
| 114 | +3. **Offlineボックス**をチェックしてください。 |
122 | 115 |
|
123 | 116 | <figure> |
124 | 117 | <img src="generated/images/guide/service-worker/offline-checkbox.png" alt="The offline checkbox in the Network tab is checked"> |
125 | 118 | </figure> |
126 | 119 |
|
127 | | -Now the app has no access to network interaction. |
| 120 | +これで、アプリケーションはネットワークにアクセスできなくなりました。 |
128 | 121 |
|
129 | | -For applications that do not use the Angular service worker, refreshing now would display Chrome's Internet disconnected page that says "There is no Internet connection". |
| 122 | +AngularにおけるService Workerを使用しないアプリケーションの場合、リフレッシュすると「インターネット接続がありません」と記載されたChromeのインターネット切断ページが表示されます。 |
130 | 123 |
|
131 | | -With the addition of an Angular service worker, the application behavior changes. On a refresh, the page loads normally. |
| 124 | +AngularにおけるService Workerを追加すると、アプリケーションの動作が変更されます。リフレッシュ時に、ページが正常にロードされます。 |
132 | 125 |
|
133 | | -If you look at the Network tab, you can verify that the service worker is active. |
| 126 | +networkタブを見ると、Service Workerがアクティブであることを確認できます。 |
134 | 127 |
|
135 | 128 | <figure> |
136 | 129 | <img src="generated/images/guide/service-worker/sw-active.png" alt="Requests are marked as from ServiceWorker"> |
137 | 130 | </figure> |
138 | 131 |
|
139 | | -Notice that under the "Size" column, the requests state is `(from ServiceWorker)`. This means that the resources are not being loaded from the network. Instead, they are being loaded from the service worker's cache. |
| 132 | +「サイズ」列の下にある要求状態は、(from ServiceWorker)となっていることに注目してください。これは、リソースがネットワークからロードされていないことを意味します。代わりに、Service Workerのキャッシュからロードされています。 |
140 | 133 |
|
141 | 134 |
|
142 | | -### What's being cached? |
| 135 | +### 何がキャッシュされているのか? |
143 | 136 |
|
144 | | -Notice that all of the files the browser needs to render this application are cached. The `ngsw-config.json` boilerplate configuration is set up to cache the specific resources used by the CLI: |
| 137 | +ブラウザがこのアプリケーションをレンダリングするために必要なすべてのファイルがキャッシュされていることに注意してください。`ngsw-config.json`のボイラープレート設定は、CLIが使用する特定のリソースをキャッシュするように設定されています。 |
145 | 138 |
|
146 | | -* `index.html`. |
147 | | -* `favicon.ico`. |
148 | | -* Build artifacts (JS and CSS bundles). |
149 | | -* Anything under `assets`. |
| 139 | +* `index.html` |
| 140 | +* `favicon.ico` |
| 141 | +* ビルド成果物(JSおよびCSSバンドル) |
| 142 | +* `assets`の下にあるもの(訳注:assets下にさらに階層を作っている場合は、`ngsw-config.json`の設定を階層に合わせて`/assets/**/**`などのように変えてください。) |
150 | 143 |
|
151 | | -### Making changes to your application |
| 144 | +### アプリケーションを変更する |
152 | 145 |
|
153 | | -Now that you've seen how service workers cache your application, the |
154 | | -next step is understanding how updates work. |
| 146 | +Service Workerがアプリケーションをキャッシュする方法を見てきました。 |
| 147 | +次のステップは、アップデートの仕組みを理解することです。 |
155 | 148 |
|
156 | | -1. If you're testing in an incognito window, open a second blank tab. This will keep the incognito and the cache state alive during your test. |
| 149 | +1. シークレットウィンドウでテストする場合は、2つ目のタブを空白で開きます。これにより、テスト中にシークレットとキャッシュの状態が維持されます。 |
157 | 150 |
|
158 | | -2. Close the application tab, but not the window. This should also close the Developer Tools. |
| 151 | +2. アプリケーションタブを閉じますが、ウィンドウは閉じません。これにより、開発者ツールも閉じる必要があります。 |
159 | 152 |
|
160 | | -3. Shut down `http-server`. |
| 153 | +3. `http-server`を落とします。 |
161 | 154 |
|
162 | | -4. Next, make a change to the application, and watch the service worker install the update. |
| 155 | +4. 次に、アプリケーションを変更して、Service Workerが更新プログラムをインストールするのを確認します。 |
163 | 156 |
|
164 | | -5. Open `src/app/app.component.html` for editing. |
| 157 | +5. 編集するために`src/app/app.component.html`を開きます。 |
165 | 158 |
|
166 | | -6. Change the text `Welcome to {{title}}!` to `Bienvenue à {{title}}!`. |
| 159 | +6. `Welcome to {{title}}!`のテキストを`Bienvenue à {{title}}!`に変えます。 |
167 | 160 |
|
168 | | -7. Build and run the server again: |
| 161 | +7. もう一度ビルドしてサーバーを起動します。 |
169 | 162 |
|
170 | 163 | ```sh |
171 | 164 | ng build --prod |
172 | 165 | cd dist |
173 | 166 | http-server -p 8080 |
174 | 167 | ``` |
175 | 168 |
|
176 | | -### Updating your application in the browser |
| 169 | +### ブラウザでアプリケーションを更新する |
177 | 170 |
|
178 | | -Now look at how the browser and service worker handle the updated application. |
| 171 | +ブラウザとService Workerが、更新されたアプリケーションをどのように処理するかを見てみましょう。 |
179 | 172 |
|
180 | | -1. Open http://localhost:8080 again in the same window. What happens? |
| 173 | +1. 同じウィンドウでもう一度http://localhost:8080を開きます。何が起こりましたか? |
181 | 174 |
|
182 | 175 | <figure> |
183 | 176 | <img src="generated/images/guide/service-worker/welcome-msg-en.png" alt="It still says Welcome to Service Workers!"> |
184 | 177 | </figure> |
185 | 178 |
|
186 | | -What went wrong? Nothing, actually. The Angular service worker is doing its job and serving the version of the application that it has **installed**, even though there is an update available. In the interest of speed, the service worker doesn't wait to check for updates before it serves the application that it has cached. |
| 179 | +何が駄目だったのでしょうか?実際には何も悪いことは起こっていません。AngularにおけるService Workerは、利用可能なアップデートがあるにもかかわらず、**インストールされている**アプリケーションのバージョンを提供しています。速度の観点から、Service Workerは、キャッシュされたアプリケーションを提供する前に、更新の確認を待つことはありません。 |
187 | 180 |
|
188 | | -If you look at the `http-server` logs, you can see the service worker requesting `/ngsw.json`. This is how the service worker checks for updates. |
| 181 | +`http-server`ログを見ると、`/ngsw.json`を要求しているService Workerを見ることができます。これは、Service Workerが更新をチェックする方法です。 |
189 | 182 |
|
190 | | -2. Refresh the page. |
| 183 | +2. ページを再読み込みします。 |
191 | 184 |
|
192 | 185 | <figure> |
193 | 186 | <img src="generated/images/guide/service-worker/welcome-msg-fr.png" alt="The text has changed to say Bienvenue à app!"> |
194 | 187 | </figure> |
195 | 188 |
|
196 | | -The service worker installed the updated version of your app *in the background*, and the next time the page is loaded or reloaded, the service worker switches to the latest version. |
| 189 | +Service Workerは、あなたのアプリケーションの更新版を*バックグラウンドで*インストールし、次にページを読み込んだりリロードしたりすると、Service Workerは最新のバージョンに切り替わります。 |
197 | 190 |
|
198 | 191 | <hr /> |
199 | 192 |
|
200 | | -## More on Angular service workers |
| 193 | +## もっとAngularにおけるService Workerを知りたい |
201 | 194 |
|
202 | | -You may also be interested in the following: |
203 | | -* [Communicating with service workers](guide/service-worker-communications). |
| 195 | +次の記事がお勧めです。 |
| 196 | +* [Service Workerと通信する](guide/service-worker-communications) |
0 commit comments