草案
此页面不完整。
add()
方法在
ContentIndex
interface registers an item with the
content index
.
ContentIndex.add(ContentDescription).then(...);
对象
containing the following data:
id
: A unique
字符串
标识符。
title
: A
字符串
title for the item. Used in user-visible lists of content.
title
: A
字符串
title of the item. Used in user-visible lists of content.
description
: A
字符串
description of the item. Used in user-visible lists of content.
url
: A
字符串
containing the url of the corresponding HTML document. Needs to be under the scope of the current
service worker
.
category
:
可选
A
字符串
defining the category of content. Can be:
''
空
字符串
, this is the default.
homepage
文章
视频
audio
icons
:
可选
数组
of image resources, defined as an
对象
with the following data:
返回
Promise
that resolves with
undefined
TypeError
FetchEvent
.
id
,
title
,
description
or
url
are missing, not of type
字符串
, or an empty
字符串
.
icons
images are not of image type.
Here we're declaring an item in the correct format and creating an asynchronous function which uses the
add
method to register it with the
content index
.
// our content
const item = {
id: 'post-1',
url: '/posts/amet.html',
title: 'Amet consectetur adipisicing',
description: 'Repellat et quia iste possimus ducimus aliquid a aut eaque nostrum.',
icons: [{
src: '/media/dark.png',
sizes: '128x128',
type: 'image/png',
}],
category: 'article'
};
// our asynchronous function to add indexed content
async function registerContent(data) {
const registration = await navigator.serviceWorker.ready;
// feature detect Content Index
if (!registration.index) {
return;
}
// register content
try {
await registration.index.add(data);
} catch (e) {
console.log('Failed to register content: ', e.message);
}
}
add
method can also be used within the
service worker
scope.
// our content
const item = {
id: 'post-1',
url: '/posts/amet.html',
title: 'Amet consectetur adipisicing',
description: 'Repellat et quia iste possimus ducimus aliquid a aut eaque nostrum.',
icons: [{
src: '/media/dark.png',
sizes: '128x128',
type: 'image/png',
}],
category: 'article'
};
self.registration.index.add(item);
| 规范 | 状态 | 注释 |
|---|---|---|
|
未知
The definition of 'add' in that specification. |
未知 | 初始定义。 |
No compatibility data found. Please contribute data for "api.ContentIndex.add" (depth: 1) to the MDN 兼容性数据存储库 .