# Debugging the page change API after migration

**URL:** <https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132>\
**Category:** Product Support\
**Created:** [March 11, 2026, 6:15am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132 "2026-03-11T06:15:12Z")\
**Posts on this page:** 5\
**Page:** 1

## AI-generated summary

In a support discussion titled "Debugging the page change API after migration," user [mattyoung](/t/-/398132/1) sought assistance moving a `onPageChange` script from theme customization to the JavaScript tab in the admin interface. Initially, [mattyoung](/t/-/398132/1) struggled to adapt the new `apiInitializer` pattern for this specific event, as existing examples primarily covered `api.renderInOutlet`. User [NateDhaliwal](/t/-/398132/2) suggested a direct migration of the code block, which [mattyoung](/t/-/398132/3) confirmed had already failed. [mattyoung](/t/-/398132/3) subsequently shared the working solution, which involved importing `apiInitializer` and handling the URL argument by checking if it starts with "http" to extract the pathname, ensuring compatibility whether the callback provides a full URL or just a path. [NateDhaliwal](/t/-/398132/4) apologized for the misunderstanding and asked for clarification on scenarios where `onPageChange` provides a full URL, noting that it typically returns Ember paths. Additionally, user [darkpixlz](/t/-/398132/5) noted that the topic was moved from a release notes thread to a new discussion to improve organization and visibility.

<div class="post-metadata">

**Author:** ![mattyoung](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattyoung/32/170562_2.png) [@mattyoung](https://meta.discourse.org/u/mattyoung)\
**Post date:** [March 11, 2026, 12:37am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132/1 "2026-03-11T00:37:02Z")

</div>

please help take this from area in theme admin customization to the js tab in admin interface:

```plaintext
<script type="text/discourse-plugin" version="0.8.19">
api.onPageChange(() => {
	if ( window.location.href === "https://apple.com/123" ) {
		window.location.replace( "https://dell.com/234" );
	}
});
</script>

```

I am looking at the example of

```plaintext
export default apiInitializer((api) => {
  // Your code here
});

```

and this has worked great for like api.renderInOutlet but this onPageChange does not have updated examples that I could find.

---

<div class="post-metadata">

**Author:** ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)\
**Post date:** [March 11, 2026, 2:16am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132/2 "2026-03-11T02:16:38Z")

</div>

What if you just moved this:

```js
api.onPageChange(() => {
	if ( window.location.href === "https://apple.com/123" ) {
		window.location.replace( "https://dell.com/234" );
	}
});

```

into

> [@mattyoung](#):
>
> `// Your code here`

?

---

<div class="post-metadata">

**Author:** ![mattyoung](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattyoung/32/170562_2.png) [@mattyoung](https://meta.discourse.org/u/mattyoung)\
**Post date:** [March 11, 2026, 3:42am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132/3 "2026-03-11T03:42:06Z")

</div>

ha. you are being sarcastic right? yes i tried that immediately. failed.

here is what worked:

```plaintext
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer("1.0", (api) => {
api.onPageChange((url) => {
// url is typically the path
// but in some contexts it can be a full URL, so we handle both.
const path = url?.startsWith("http") ? new URL(url).pathname : url;
if (path === "/123") {
      window.location.replace("https://dell.com/234");
}
});

```

---

<div class="post-metadata">

**Author:** ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)\
**Post date:** [March 11, 2026, 6:15am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132/4 "2026-03-11T06:15:12Z")

</div>

> [@mattyoung](#):
>
> ha. you are being sarcastic right? yes i tried that immediately. failed.

My apologies. I misunderstood your question.

> [@mattyoung](#):
>
> `but in some contexts it can be a full URL, so we handle both.`

I’m curious - what are these situations? I’ve never seen the `https` in the url before, because IIRC `onPageChange` is for Ember’s paths.

---

<div class="post-metadata">

**Author:** ![darkpixlz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/darkpixlz/32/549896_2.png) [@darkpixlz](https://meta.discourse.org/u/darkpixlz)\
**Post date:** [March 11, 2026, 6:27am UTC](https://meta.discourse.org/t/debugging-the-page-change-api-after-migration/398132/5 "2026-03-11T06:27:48Z")

</div>

Hi, just as some housekeeping I moved this from the original release notes thread to a new topic to keep the thread a little more tidy and to improve visibility. Good luck 👍

However I did notice in your later example that you are no longer checking `window.location.href`. Is that the problem?
