Create PWA web browser warning while user click on back button

We have made some PWAs which are in production. The problem we face is to handle browser back button in web browsers.

How can we give the user a warning while the user is clicking on the back button? When the user will click on “OK”, the control will go back. When the user will click on “cancel”, the control should stay in the same page.


private agreedToExit: boolean = false



      const confirm = this.alertCtrl.create({
        title: 'Confirm',
        message: 'Are you sure you want to exit?',
        buttons: [
            text: 'No'
            text: 'Yes',
            handler: () => {
              this.agreedToExit = true
      return false
      return true