venantius / accountant

ClojureScript navigation for single-page applications, made simple.
Eclipse Public License 1.0
250 stars 39 forks source link

accountant/navigate! reuses existing query params, instead of resetting it #19

Closed tolgap closed 6 years ago

tolgap commented 8 years ago

Here's a small scenario:

I have a search input. The input changes the URL in its on-change attribute. The URL is changed from:

localhost:3449/ to localhost:3449/?q=t

But when I keep tying the word test into the search input, the URL changes to:

localhost:3449/?q=t?q=te?q=tes?q=test.

I have managed to track the issue down to this line in core.cljs line 108:

(let [token (.getToken history)])

It seems the Html5History API from the Google Closure Library does not keep track/return the query string when calling getToken on it, because it always returns "/", instead of "/?q=t" etc.

Is there any way I can work around this for now? I don't know if the issue is in the GCL Html5History API, or Accountant, or that I'm doing something wrong.

venantius commented 8 years ago

Huh. That is pretty weird. I'm not totally sure where the bug here is either - could also be in Secretary. Will have to do some debugging.

logaan commented 8 years ago

I'm hitting this bug as well.

bago2k4 commented 8 years ago

I use a custom transformer for the Html5History object: https://github.com/open-company/open-company-web/blob/mainline/src/open_company_web/router.cljs inspired by this: https://github.com/Sparrho/supper-demo/blob/master/src-cljs/supper/history.cljs

philjackson commented 8 years ago

Wondering if anyone figured this out?

venantius commented 8 years ago

I'd welcome a PR to fix this.

pharcosyle commented 7 years ago

I solved this problem by adding the following code above the accountant/configure-navigation! call:

(set! accountant/history.transformer_
      (let [transformer (goog.history.Html5History.TokenTransformer.)]
        (set! (.. transformer -retrieveToken)
              (fn [path-prefix location]
                (str (.-pathname location) (.-search location))))
        (set! (.. transformer -createUrl)
              (fn [token path-prefix location]
                (str path-prefix token)))
        transformer))

bago2k4's links were very helpful.

acron0 commented 7 years ago

@pharcosyle 's code fixes it. Thank you!

venantius commented 6 years ago

I believe this has been resolved by #48.