草案
此页面不完整。

add() 方法在 ContentIndex interface registers an item with the content index .

句法

ContentIndex.add(ContentDescription).then(...);
					

参数

ContentDescription
The item registered is an 对象 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
If the service worker's registration is not present or the service worker does not contain a 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.
未知 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

No compatibility data found. Please contribute data for "api.ContentIndex.add" (depth: 1) to the MDN 兼容性数据存储库 .

另请参阅:

元数据

  • 最后修改: