How do I run a function on each route navigation of the SPA used by Frappe?

What I want is to run a function say X every time the current route changes