1. Developer guides

Subscribe to page events

Version: 0.4
Before you begin

When you query the page context, you retrieve details about the current page and site. You can also subscribe to page-related events. This way, your app receives real-time updates when the SitecoreAI user changes to a different version or language of the current page, or opens a different page.

To subscribe to page events:

  1. Find the code where you queried the page context, typically client.query("pages.context"), and replace it with the following code, then save your changes:

    client.query("pages.context", {
      subscribe: true,
      onSuccess: (data) => {
        console.log("Page has been updated:", data);
      },
    })

    This script adds a second parameter to your query to enable subscriptions. Every time a current page detail changes, the updates are logged to the console.

  2. In the SitecoreAI Page builder, open your web browser's console, refresh the page, then open your Marketplace app.

  3. In the console, filter your logs for Page has been updated.

  4. In the site tree, click a different page. A new log appears in the console, containing the details of the page you opened.

If you have suggestions for improving this article, let us know!