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.
In this article
Syntax
add(contentDescription)Parameters
contentDescriptionAn
Objectcontaining the following data:idA unique
Stringidentifier.titleA
Stringtitle for the item. Used inuser-visible lists of content.descriptionA
Stringdescription of the item. Usedin user-visible lists of content.urlA
Stringcontaining the URL of the correspondingHTML document. Needs to be under the scope of the currentservice worker.categoryOptionalA
Stringdefining thecategory of content. Can be:''An emptyString, this is the default.homepagearticlevideoaudio
iconsOptionalAn
Arrayof imageresources, defined as anObjectwith the following data:srcA URL
Stringof the source image.sizesOptionalA
Stringrepresentation of the image size.typeOptionalTheMIME type of the image.
labelOptionalA string representing the accessible name of the icon.
Return value
Returns aPromise that resolves withundefined.
Exceptions
TypeErrorThis exception is thrown in the following conditions:
- The service worker's registration is not present or the service worker does not contain a
FetchEvent. - One of
id,title,descriptionorurlparameter are missing, not of typeString, or an emptyString. - The
urlparameter is notsame-origin policy with theservice worker. - One of the items in
iconsare not an image type, or fetching one of the items iniconsfailed with a network error or decode error.
- The service worker's registration is not present or the service worker does not contain a
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.
// 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.
// 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> |