Trigger javascript on clicking any page link, but before the page content loads?

Couple of reasons that might not be working:

  1. putting actions in a hash is an old syntax which we no longer use in Discourse. Theoretically it should work, but the interop with the new syntax might not be perfect. The modern way would be

    api.modifyClass("route:application", (Superclass) => class extends Superclass {
      @action
      willTransition(){
        console.log("do something");
        super.willTransition(...arguments);
      }
    });
    

    standard modifyClass disclaimer applies: this is risky and could break at any time

  2. I don’t think willTransition is guaranteed to fire on the application route in all situations. (e.g. if a child route has a willTransition action, it won’t necessarily bubble up)

I’d recommend using the events on the Ember router service instead: RouterService | 5.12.0 | Ember API Documentation

e.g. something like

const router = api.container.lookup("service:router");
router.on("routeWillChange", () => {
  console.log("route will change fired");
});