BackgroundFetchRegistration: match() 方法
注意:此功能在 Web Workers 中可用。
BackgroundFetchRegistration 介面的 match() 方法會返回第一個匹配的 BackgroundFetchRecord。
語法
js
match(request)
match(request, options)
引數
請求options可選-
一個設定
match操作選項的物件。可用選項包括:ignoreSearch可選-
一個布林值,指定是否忽略 URL 中的查詢字串。例如,如果設定為
true,在執行匹配時將忽略http://foo.com/?value=bar中的?value=bar部分。預設值為false。 ignoreMethod可選-
一個布林值。當設定為
true時,將阻止匹配操作驗證Request的http方法。如果為false(預設值),則只允許GET和HEAD。 ignoreVary可選-
一個布林值。當設定為
true時,表示應忽略Vary頭部。預設為false。
返回值
一個 Promise,它會解析為與請求匹配的第一個 BackgroundFetchRecord,如果未找到匹配項,則解析為 undefined。
注意:BackgroundFetchRegistration.match() 與 BackgroundFetchRegistration.matchAll() 基本相同,只是它解析為只返回第一個匹配的記錄,而不是所有匹配記錄的陣列。
異常
InvalidStateErrorDOMException-
當您在沒有正在進行的 fetch 時呼叫
match()時返回此值。此狀態將反映在BackgroundFetchRegistration.recordsAvailable設定為false。
示例
在此示例中,我們查詢 URL 為“/ep-5.mp3”的記錄。如果找到 BackgroundFetchRecord,我們就可以返回有關它的資訊。
js
bgFetch.match("/ep-5.mp3").then(async (record) => {
if (!record) {
console.log("No record found");
return;
}
console.log(`Here's the request`, record.request);
const response = await record.responseReady;
console.log(`And here's the response`, response);
});
規範
| 規範 |
|---|
| Background Fetch # background-fetch-registration-match |
瀏覽器相容性
載入中…