title: Detect when a user leaves page in Next JS
body: <p>I would like to detect when the user leaves the page Next JS. I count 3 ways of leaving a page:</p>
  <ol>
  <li>by clicking on a link</li>
  <li>by doing an action that triggers router.back, router.push, etc...</li>
  <li>by closing the tab (i.e. when beforeunload event is fired</li>
  </ol>
  <p>Being able to detect when a page is leaved is very helpful for example, alerting the user some changes have not been saved yet.</p>
  <p>I would like something like:</p>
  <pre><code>router.beforeLeavingPage(() =&gt; {
      // my callback
  })
  </code></pre>