Debugging the page change API after migration

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

<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

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.

What if you just moved this:

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

into

?

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

here is what worked:

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");
}
});

My apologies. I misunderstood your question.

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.

1 Like

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 :+1:

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