diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/fetchevent | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/fetchevent')
-rw-r--r-- | files/zh-cn/web/api/fetchevent/index.html | 164 | ||||
-rw-r--r-- | files/zh-cn/web/api/fetchevent/respondwith/index.html | 141 |
2 files changed, 305 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/fetchevent/index.html b/files/zh-cn/web/api/fetchevent/index.html new file mode 100644 index 0000000000..2b33e9de41 --- /dev/null +++ b/files/zh-cn/web/api/fetchevent/index.html @@ -0,0 +1,164 @@ +--- +title: FetchEvent +slug: Web/API/FetchEvent +translation_of: Web/API/FetchEvent +--- +<p>{{APIRef("Service Workers API")}}{{ SeeCompatTable() }}</p> + +<p>使用<code>ServiceWorker</code>技术时,页面的提取动作会在ServiceWorker作用域(<code>ServiceWorkerGlobalScope</code>)中触发fetch事件.</p> + +<p>使用 {{domxref("ServiceWorkerGlobalScope.onfetch")}}或addEventListener监听.<br> + 该事件回调会注入<code>FetchEvent</code>参数.它携带了有关请求和结果响应的信息以及方法{{domxref("FetchEvent.respondWith", "FetchEvent.respondWith()")}} ,允许我们向受控页面提供任意响应.</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("FetchEvent.FetchEvent()")}}</dt> + <dd>Creates a new <code>FetchEvent</code> object.</dd> +</dl> + +<h2 id="属性">属性</h2> + +<p><em>Inherits properties from its ancestor, {{domxref("Event")}}</em>.</p> + +<dl> + <dt>{{domxref("FetchEvent.isReload")}} {{readonlyInline}}</dt> + <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the event was dispatched with the user's intention for the page to reload, and <code>false</code> otherwise. Typically, pressing the refresh button in a browser is a reload, while clicking a link and pressing the back button is not.</dd> + <dt>{{domxref("FetchEvent.request")}} {{readonlyInline}}</dt> + <dd>Returns the {{domxref("Request")}} that triggered the event handler.</dd> + <dt>{{domxref("FetchEvent.clientId")}} {{readonlyInline}}</dt> + <dd>Returns the id of the client that the current service worker is controlling.</dd> +</dl> + +<h3 id="Deprecated_properties">Deprecated properties</h3> + +<dl> + <dt>{{domxref("FetchEvent.client")}} {{readonlyInline}}</dt> + <dd>Returns the {{domxref("Client")}} that the current service worker is controlling.</dd> +</dl> + +<h2 id="方法">方法</h2> + +<p><em>Inherits methods from its parent, </em><em>{{domxref("ExtendableEvent")}}</em>.</p> + +<dl> + <dt>{{domxref("FetchEvent.respondWith()")}}</dt> + <dd>Resolves by returning a {{domxref("Response")}} or a <a href="http://fetch.spec.whatwg.org/#concept-network-error">network error</a> to <code style="font-style: normal;"><a href="http://fetch.spec.whatwg.org/#concept-fetch">Fetch</a></code>.</dd> + <dt>{{domxref("ExtendableEvent.waitUntil", "ExtendableEvent.waitUntil()")}}</dt> + <dd> + <p>Extends the lifetime of the event. It is intended to be called in the {{event("install")}} {{domxref("EventHandler")}} for the {{domxref("ServiceWorkerRegistration.installing", "installing")}} worker and on the {{event("active")}} {{domxref("EventHandler")}} for the {{domxref("ServiceWorkerRegistration.active", "active")}} worker.</p> + </dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>This code snippet is from the <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/prefetch/service-worker.js">service worker fetch sample</a> (<a href="https://googlechrome.github.io/samples/service-worker/prefetch/">run the fetch sample live</a>). In an earlier part of the code, an {{domxref("InstallEvent")}} controls caching of a number of resources. The {{domxref("ServiceWorkerGlobalScope.onfetch")}} event handler then listens for the {{event("fetch")}} event. When fired, {{domxref("FetchEvent.respondWith()")}} returns a promise back to the controlled page. This promise resolves to the first matching URL request in the {{domxref("Cache")}} object. If no match is found (i.e. that resource wasn't cached in the install phase), the code fetches a response from the network.</p> + +<p>The code also handles exceptions thrown from the {{domxref("ServiceWorkerGlobalScope.fetch()")}} operation. Note that a HTTP error response (e.g., 404) doesn't trigger an exception. It returns a normal response object that has the appropriate error code set.</p> + +<pre class="brush: js">self.addEventListener('fetch', function(event) { + console.log('Handling fetch event for', event.request.url); + + event.respondWith( + caches.match(event.request).then(function(response) { + if (response) { + console.log('Found response in cache:', response); + + return response; + } + console.log('No response found in cache. About to fetch from network...'); + + return fetch(event.request).then(function(response) { + console.log('Response from network is:', response); + + return response; + }).catch(function(error) { + console.error('Fetching failed:', error); + + throw error; + }); + }) + ); +});</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Service Workers', '#fetch-event-section', 'FetchEvent')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(40.0)}}</td> + <td>{{ CompatGeckoDesktop("44.0") }}[<sup>1]</sup></td> + <td>{{CompatNo}}</td> + <td>24</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{ CompatGeckoMobile("44.0") }}</td> + <td>{{ CompatVersionUnknown }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(44.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 and 52 Extended Support Releases</a> (ESR.)</p> + +<h2 id="请参见">请参见</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li><a href="/en-US/docs/Web/API/Fetch_API">Fetch API</a></li> +</ul> diff --git a/files/zh-cn/web/api/fetchevent/respondwith/index.html b/files/zh-cn/web/api/fetchevent/respondwith/index.html new file mode 100644 index 0000000000..0840832a0f --- /dev/null +++ b/files/zh-cn/web/api/fetchevent/respondwith/index.html @@ -0,0 +1,141 @@ +--- +title: FetchEvent.respondWith() +slug: Web/API/FetchEvent/respondWith +translation_of: Web/API/FetchEvent/respondWith +--- +<p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p> + +<p>{{domxref("FetchEvent")}} 接口的 <strong><code>respondWith()</code></strong> 方法旨在包裹代码,这些代码为来自受控页面的request生成自定义的response。这些代码通过返回一个 {{domxref("Response")}} 、 <a class="external external-icon" href="http://fetch.spec.whatwg.org/#concept-network-error">network error</a> 或者 <code><a class="external external-icon" href="http://fetch.spec.whatwg.org/#concept-fetch">Fetch</a>的方式</code>resolve。</p> + +<p>有关跨域内容污染的渲染端安全检测与 {{domxref("Response")}} 体的透明度(或者不透明度)相关联,而不是URL。如果request是一个顶级的导航,而返回值是一个类型属性不透明的 {{domxref("Response")}}(即不透明响应体),一个 <a class="external external-icon" href="http://fetch.spec.whatwg.org/#concept-network-error">network error</a> 将被返回给 <code><a class="external external-icon" href="http://fetch.spec.whatwg.org/#concept-fetch">Fetch</a>。所有成功(非网络错误)响</code>应的最终URL是请求的URL。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">FetchEvent.respondWith( + //Promise that resolves to a Response or a network error. +)</pre> + +<h3 id="返回值">返回值</h3> + +<p>Void.</p> + +<h3 id="参数">参数</h3> + +<p>任何自定义的响应生成代码。</p> + +<h2 id="示例">示例</h2> + +<p>该代码片段来自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/prefetch/service-worker.js">service worker fetch sample</a> (<a class="external external-icon" href="https://googlechrome.github.io/samples/service-worker/prefetch/">run the fetch sample live</a>)。 {{domxref("ServiceWorkerGlobalScope.onfetch")}} 事件处理程序侦听 {{event("fetch")}} 事件。当触发时,{{domxref("FetchEvent.respondWith", "FetchEvent.respondWith(any value)")}} 返回一个promise给受控页面。该promise在 {{domxref("Cache")}} 对象中查询第一个匹配URL请求。如果没有发现匹配项,该代码将转而从网络获取响应。</p> + +<p>该代码也处理从 {{domxref("ServiceWorkerGlobalScope.fetch")}} 操作中抛出的异常。请注意,HTTP错误响应(例如404)不会触发异常。 它将返回具有相应错误代码集的正常响应对象。</p> + +<pre class="brush: js">self.addEventListener('fetch', function(event) { + console.log('Handling fetch event for', event.request.url); + + event.respondWith( + caches.match(event.request).then(function(response) { + if (response) { + console.log('Found response in cache:', response); + + return response; + } + console.log('No response found in cache. About to fetch from network...'); + + return fetch(event.request).then(function(response) { + console.log('Response from network is:', response); + + return response; + }).catch(function(error) { + console.error('Fetching failed:', error); + + throw error; + }); + }) + ); +});</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Service Workers', '#fetch-event-respondwith-method', 'respondWith()')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(40.0)}}</td> + <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>24</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{ CompatGeckoMobile("44.0") }}</td> + <td>{{ CompatVersionUnknown }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(44.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 and 52 Extended Support Releases</a> (ESR.)</p> + +<h2 id="请参见">请参见</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> + <li><a class="external external-icon" href="https://github.com/mdn/sw-test">Service workers basic code example</a></li> + <li><a class="external external-icon" href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li> + <li>{{jsxref("Promise")}}</li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a></li> +</ul> |