Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. ContentIndex
  4. add()

ContentIndex: add() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Note: This feature is available inWeb Workers.

Theadd() method of theContentIndex interface registers an item with thecontent index.

Syntax

js
add(contentDescription)

Parameters

contentDescription

AnObject containing the following data:

id

A uniqueString identifier.

title

AString title for the item. Used inuser-visible lists of content.

description

AString description of the item. Usedin user-visible lists of content.

url

AString containing the URL of the correspondingHTML document. Needs to be under the scope of the currentservice worker.

categoryOptional

AString defining thecategory of content. Can be:

  • '' An emptyString, this is the default.
  • homepage
  • article
  • video
  • audio
iconsOptional

AnArray of imageresources, defined as anObject with the following data:

src

A URLString of the source image.

sizesOptional

AString representation of the image size.

typeOptional

TheMIME type of the image.

labelOptional

A string representing the accessible name of the icon.

Return value

Returns aPromise that resolves withundefined.

Exceptions

TypeError

This exception is thrown in the following conditions:

  • The service worker's registration is not present or the service worker does not contain aFetchEvent.
  • One ofid,title,description orurl parameter are missing, not of typeString, or an emptyString.
  • Theurl parameter is notsame-origin policy with theservice worker.
  • One of the items inicons are not an image type, or fetching one of the items inicons failed with a network error or decode error.

Examples

Here we're declaring an item in the correct format and creating an asynchronousfunction which uses theadd method to register it with thecontent index.

js
// our contentconst 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 contentasync 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);  }}

Theadd method can also be used within theservice worker scope.

js
// our contentconst 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);

Specifications

Specification
Content Index
# content-index-add

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp