Auto Audio

Single-page apps

Load the player on sites that change articles without a full page load.

The widget loads the player once, when the page loads. If your site changes articles without a full page load (client-side routing), call Everlit.autoAudio.load() each time you show an article — the same pattern as googletag.display(). The script tag stays as it is. Requires widget 3.4.0 or later.

Setup

Create the command queue before the script tag, so your code can call the widget whether or not the script has finished loading:

<script>
  window.Everlit = window.Everlit || {};
  Everlit.autoAudio = Everlit.autoAudio || {};
  Everlit.autoAudio.cmd = Everlit.autoAudio.cmd || [];
</script>
<script
  defer
  src="https://cdn.everlit.audio/libs/everlitAutoAudio.js"
  type="text/javascript"
></script>

Each Time You Show an Article

  1. Render the article, including a new everlit-auto-audio-widget div.
  2. Update <head> for the new article:
    • <link rel="canonical"> (or og:url): which article this is.
    • <title>, og:title and og:description: the title and summary.
    • og:image, article:published_time, article:modified_time, article:author and any NewsArticle JSON-LD.
  3. If you pass ad targeting key-values with setTargeting, update them for the new article. Targeting stays set across load() calls until you change or clear it, so start with clearTargeting().
  4. Call load():
Everlit.autoAudio.cmd.push(function () {
  Everlit.autoAudio.load().then(function (result) {
    if (result.status === 'ready') {
      console.log('Player ready', result.articleId, result.element);
    }
  });
});

Leftover Metadata

Many routers rewrite the page body and <title> but leave the rest of <head> as it was on the first article. From widget 3.10.0, when load() runs for a new article, the widget ignores every tag that is unchanged since the previous article and works from what the article itself shows. The audio still gets made, but its title and summary can be plainer until a reader opens the article with a full page load, which picks up the real metadata. Updating the tags above avoids that.

If the page moves to a new path but <link rel="canonical"> still names the previous article, load() doesn't load anything. It resolves with status: 'error' and reason: 'canonical_not_updated', and the same reason fires on everlit:error. Loading would file the new article under the old one's address.

It's safe to call load() for every article, including the first: a call for the page the widget has already loaded (or is loading) returns the same result without another request. Pages without a widget div resolve with status: 'none' and make no request.

What load() Does

  • Removes the player or click-to-create button from the previous article if it is still on the page, along with the audio metadata it added to <head>.
  • Finds the widget div and loads the player for the current article, exactly as on a full page load. The usual JavaScript events fire.
  • If you call load() for a new article before the previous one has finished, the earlier call stops without touching the page and resolves with status: 'superseded'.
  • After an error, calling load() again for the same page retries.

Result

The Promise always resolves — it never rejects — with an object whose status is one of:

status Meaning Other fields
ready The player is on the page element, articleId, playerUrl
clickToCreate The click-to-create button is on the page element, processing
processing Audio for this article is being created; nothing was placed —
none No widget div on the page, or no audio to show —
superseded A newer load() started first, or the widget div was removed —
error The request failed, or the canonical URL wasn't updated (canonical_not_updated) reason

Frameworks

The widget replaces the widget div with its own element. If a framework (React, Vue, Svelte…) rendered that div, it will later try to update or remove a node that is no longer there. Render the div as raw HTML inside an element the framework owns, and give that element a key tied to the article so each article gets a fresh one:

// React
<div
  key={article.url}
  dangerouslySetInnerHTML={{
    __html: '<div id="everlit-auto-audio-widget" data-publication-id="YOUR-PUBLICATION-ID"></div>'
  }}
/>