Skip to main content
When readers move between pages, what happens to the widget, and to any conversation they’re having with your agent, depends on how your site loads pages. This guide covers the JavaScript SDK. When a reader moves from one article to another:

Multi-page sites

On a multi-page site, each link loads a new page, and the widget starts fresh on every page:
  • Audio stops.
  • An open text or voice conversation ends.
  • The widget on the new page starts with an empty chat.
Browsers can’t keep a voice conversation connected across a full page load, so there’s no setting that changes this.
Links the agent cites in its answers open in a new tab, so a voice conversation carries on in the original tab while the reader looks at the source. If the reader doesn’t speak for 30 seconds (60 if their mic is muted), though, the widget ends the conversation, and on phones the browser may pause or end it while its tab is in the background. Links on your own pages that open in the same tab, such as menus and related articles, end the conversation.

Single-page apps

In a single-page app, your router changes what’s on screen without loading a new page. The widget only changes content when you tell it to.

Use one widget for the whole app

We recommend one widget instance for the life of the app:
  1. Create the widget once, in part of your layout that stays on screen across routes, not in the area your router replaces.
  2. Pass a content identifier when you create it, for example sourceId.
  3. On each route change, set the same identifier to the new article’s value, for example player.sourceId = 'article-456'.
When you change the identifier, the widget loads the new content, and the conversation stays as it was:
  • Any agent conversation carries on, including a voice conversation in progress, and the chat history stays.
  • An open chat panel stays open. The main widget and the sticky widget each have their own panel, though: if your router scrolls the main widget back into view, the sticky widget slides away just as it does when a reader scrolls up, and the main widget’s panel stays closed until the reader opens it. The conversation is still there.
When the new content loads, whatever was playing stops, and the new content is ready to play from the start. Message and voice-time limits from your access tiers are counted per article. When new content loads, the counts start again, even if the conversation carries on. Setting an identifier to the value it already has does nothing, so you can set it on every navigation, including query-string changes. The sticky widget only appears once the reader scrolls past the main widget, so where you put the main widget in your layout decides when the sticky widget shows.

What the agent knows

The agent is given details of the loaded content, such as its title, when a conversation starts. A conversation that’s already open when the reader changes route continues with the previous content as its context. The agent gets the new content in the next conversation. For example, a new conversation starts after a voice conversation ends or when the reader switches between text and voice. A text conversation has no end button, so it keeps the previous content until it ends or the reader starts a voice conversation.

Choose one identifier type

  • Use the same identifier type every time. If more than one identifier is set, the widget loads them all as a playlist (see the note on identifiers). A widget created with sourceId should only ever have sourceId updated.
  • Prefer sourceId or contentId. They stay the same if an article’s URL changes.
  • If you identify content by URL, set sourceUrl to the article’s canonical URL, not location.href, which can include query strings and fragments.
  • Always set an identifier, and don’t clear it. A widget with no identifier sets sourceUrl to the full URL of the current page (window.location.href). That URL doesn’t follow later route changes, and it’s sent along with any identifier you set afterward.
  • Keep the same projectId. Changing it replaces the agent, which ends the conversation.
Pass any property overrides when you create the widget. When new content loads, settings from your project dashboard are applied again, replacing any you changed at runtime. See how overrides work.

Routes without BeyondWords content

If the identifier you set has no published content, or the request for it fails, the widget:
  • Hides itself and fires NoContentAvailable.
  • Ends any conversation and clears the chat.
When you next set an identifier that has content, the widget reappears with an empty chat. If you’ve turned on showAgentWithoutContent, the widget shows your agent on its own instead and fires AgentAvailableWithoutContent. For routes that have no identifier of their own, such as your home page, you can:
  • Leave the widget as it is. It keeps the last article, and any audio or conversation carries on. The examples below do this.
  • Destroy it. Call player.destroy(), then create a new widget on the next article route. This ends any conversation.

If the widget lives inside a route component

If you create the widget inside a component that your router mounts and unmounts, such as an article page, call destroy() when the component unmounts:
Removing the widget’s element from the page doesn’t stop the widget. Without destroy(), the instance keeps running and stays in BeyondWords.Player.instances(), and a voice conversation can continue with nothing on screen.
With this pattern, each route has its own widget, so each route starts a new conversation with an empty chat.

Examples

Each example creates the widget on the first article route, then updates sourceId on later article routes. On other routes, it leaves the widget as it is.
Call onRouteChange from your router after each route change.

Events

These widget events help when content changes: The widget hides its own interface when there’s no content, but your container stays on the page, along with any heading or spacing around it. To hide it too:
If you destroy the widget and create a new one, add your listeners to the new instance.

Get help

If you encounter issues or have questions, contact support. Include your initialization code, your router setup, and any console errors.