Skip to content

Commit

Permalink
Merge pull request #48 from DrTom/master
Browse files Browse the repository at this point in the history
Fix loss of query-params and fragment on browser navigation
  • Loading branch information
David Jarvis authored Oct 28, 2017
2 parents 7f4cca7 + 1be8d62 commit 5eef51f
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/accountant/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
goog.history.Html5History
goog.Uri))

(defonce history (Html5History.))
(defn- transformer-create-url
[token path-prefix location]
(str path-prefix token))

(defn- transformer-retrieve-token
[path-prefix location]
(str (.-pathname location) (.-search location) (.-hash location)))

(defonce history (let [transformer (goog.history.Html5History.TokenTransformer.)]
(set! (.. transformer -retrieveToken) transformer-retrieve-token)
(set! (.. transformer -createUrl) transformer-create-url)
(Html5History. js/window transformer)))

(defn- dispatch-on-navigate
[history nav-handler]
Expand All @@ -27,21 +38,6 @@
(when-let [parent (.-parentNode e)]
(recur parent))))

(defn- get-url
"Gets the URL for a history token, but without preserving the query string
as Google's version incorrectly does. (See https://goo.gl/xwgUos)"
[history token]
(str (.-pathPrefix_ history) token))

(defn- set-token!
"Sets a history token, but without preserving the query string as Google's
version incorrectly does. (See https://goo.gl/xwgUos)"
[history token title]
(let [js-history (.. history -window_ -history)
url (get-url history token)]
(.pushState js-history nil (or title js/document.title "") url)
(.dispatchEvent history (Event. token))))

(defn- uri->query [uri]
(let [query (.getQuery uri)]
(when-not (empty? query)
Expand Down Expand Up @@ -86,10 +82,10 @@
(= button 0)
(= host current-host)
(or (not port)
(= (str port) (str current-port)))
(= (str port) (str current-port)))
(path-exists? path))
(when (not= current-relative-href relative-href) ;; do not add duplicate html5 history state
(set-token! history relative-href title))
(. history (setToken relative-href title)))
(.preventDefault e))))))

(defonce nav-handler nil)
Expand Down

0 comments on commit 5eef51f

Please sign in to comment.