Search code examples
javascriptsapui5url-routing

SAPUI5 routing reset after browser refresh


I have developed a SAPUI5 master detail app. When I'm clicking on a master item, the corresponding detail page is shown (so far so good). The problem now is, that if I hit refresh (F5) in the browser, the last selected item is loaded (because of the URL parameter).

What I want to achieve is, to display the master list, but no item is selected. Instead a "select an item" page should be shown instead of the item detail page. I've tried many things such as manipulate the routing, but none of this works. Any ideas on how to achieve this?


Solution

  • By doing that you give up the whole idea of deep-linking which is imho one of the major benefits of the routing. So think twice before doing so.

    Anyways you could just reset the hash before initializing the router in your Component like this:

    sap.ui.define([
      "sap/ui/core/UIComponent",
      "sap/ui/core/routing/HashChanger"
    ], function (UIComponent, HashChanger) {
    
      "use strict";
    
      return UIComponent.extend("sap.ui.demo.nav.Component", {
    
        metadata: {
          manifest: "json"
        },
    
        init: function () {
          // reset the routing hash
          HashChanger.getInstance().replaceHash("");
    
          // call the init function of the parent
          UIComponent.prototype.init.apply(this, arguments);
    
          // create the views based on the url/hash
          this.getRouter().initialize();
        }
      });
    });
    

    BR Chris