יצירת כותרת דף ואירוע page:changed

סיכום מבוסס בינה מלאכותית

In a discussion titled ‘Page title generation and page:changed event’ under the Support category, gar1t reports an inconsistency in Discourse’s onPageChange callback: sometimes the page title is not yet generated when the callback fires, leading to incomplete Matomo tracking logs. This issue occurs even with the upstream Matomo theme component. Additionally, onPageChange is occasionally triggered twice for the same page view, with one call providing a mismatched url argument — prompting gar1t to implement an early-exit workaround. simon tested the behavior and observed that newly created topic titles are not set during the immediate onPageChange call after topic creation, defaulting instead to the site title — suggesting this may be related to missing site settings. However, guildai (likely gar1t under a different handle) clarifies that their issue is different: titles are missing only on the first page load of the app, but appear correctly on subsequent navigations. A screen recording and code reference were offered to illustrate the behavior more clearly.

This question is in the weeds of the API.

I’m using a fork of the excellent Matomo theme component. This lets you track page views with Matomo. My fork is an attempt to work-around issues. It’s essentially identical. The behavior described below occurs with the upstream component as well.

The component uses the onPageChange callback to configure a Matomo API/facility.

The issue I’m having is that, at times, the onPageChange callback is made before the page title is generated. This is inconsistent. Sometimes the title is fully formed, sometimes it’s not.

The result is that the Matomo log history has a mix of fully-formed page titles and not.

Looking over the Discourse source code, this is called prior to the page:changed trigger:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/page-tracker.js#L31

This all looks like it should work - the title should be refreshed by the time onPageChange is called. It is not - at least not consistently.

There’s another issue that may be related. onPageChange is sometimes called twice. This is my work-around (and the motivation for the fork):

In the case when onPageChange is called called twice for the same page view, one of the two calls provides a url arg that’s different from the current window location. I check this and exit early.

(Note that I handle the page:changed event via onAppEvent rather than onPageChange but I see the same behavior in either case.)

I’ve tested seeing if the title gets set with the following code:

<script type="text/discourse-plugin" version="0.2">
api.onPageChange( (url, title) => {
    console.log('url', url);
    console.log('title', title)
})
</script>

What I’m finding is that if I create a new topic, the topic’s title is not set on the call to api.onPageChange that is made immediately after creating the topic. In that case, the site’s title is used instead. Does this seem to be what’s happening on your site? If you are seeing no title at all, make sure that you have filled in the title and site description site settings. Those settings are found on the “Required” tab of your site settings page.

Thanks for looking into this Simon. That is not what I’m seeing. I put together a screen recording and I’ll get you a link out-of-band along with a reference to the theme component source in play.

The gist though is that titles are not rendered on an initial call to a topic from a loaded app. On an entry page (i.e. the first time the app is loaded) I see the title in onPageChanged and on any page after the first. The recording makes this a lot clearer hopefully :slight_smile: