IAmYourFaja IAmYourFaja - 3 years ago 242
Dart Question

Managing browser history in Dart

I'm building a single-page Dart web app that will essentially consist of 1 Dart file (cross-compiled to JS) and 1 HTML file that has several "views" (screens, pages, etc.). in it. Depending on what "view" the user is currently located at, I will hide/enable different DOM elements defined inside this HTML file. This way the user can navigate between views without triggering multiple page loads.

I would still like to use each browser's native history-tracking mechanism, so that the user click can the back- and forward-buttons in the browser, and I'll have a Dart

object figure out what view to load (again just hiding/enabling DOM elements) depending on what URL the browser has in its history.

I've pretty much figured everything out, with one exception:

Say the user is currently "at" View #3, which has a URL of, say,
. Then they click a button that should take them to View #4 at, say,
. I need a way, in Dart, to tell the browser to:

  1. Set
    in the browser URL bar

  2. Add
    to the browser's history

  3. If not already enabled, enable the browser's back button

I believe I can accomplish #1 above like so:

window.location.href = "http://myapp.example.com/#view3";

...but maybe not. Either way, how can I accomplish this (Dart code communicates with browser's history API)?

Answer Source

Check out the route library.

angular.dart also has it's own routing mechanism, but it's part of a much larger framework, so unless you plan on using the rest of it, I would recommend the stand-alone route library.

If you want to build your own solution, you can take a look at route's client.dart for inspiration.

There are two methods of history navigation supported:

  1. The page fragment method that you've used. Reassign the window location to the new page fragment: window.location.assign(newPathWithPageFragment). Doing this will automatically add a new item to the browser history (which will then enable the back button).

  2. The newer History API, which allows for regular URLs without fragments (e.g. http://myapp.example.com/view3. You can use window.history to control the history.The History API is only supported by newer browsers so that may be a concern (although given that dart2js also only supports newer browsers, there are probably not too many instances of a browser that dart2js supports that doesn't support the History API).

One issue you will have to handle if you support History API is the initial page load. When a user navigates to http://myapp.example.com/view3, the browser expects to find a resource at that location. You will have to setup your server to respond to any page request by serving your Dart application and then navigate to the correct view on the client-side. This issue will apply whether you use route, angular.dart, or build your own solution, since this is a general server-side issue and the above are all client-side libraries.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download