Last active
March 21, 2019 03:10
-
-
Save andrejewski/3887f205fd834eea1b506a908db76e38 to your computer and use it in GitHub Desktop.
goog.HTML5History with query params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| A fix for HTML5History with query parameters. | |
| Hey, this wasted my time so here is an explanation. | |
| By default, Closure's goog.HTML5History does the stupid | |
| thing of adding query params of the current location to | |
| whatever new path you are navigating to. I did not need | |
| that so here is the fix: | |
| */ | |
| const {HTML5History} = goog; | |
| const {TokenTransformer} = HTML5History; | |
| function SaneTokenTransformer () { | |
| TokenTransformer.call(this) | |
| this.createUrl = (token, pathPrefix, location) => ( | |
| pathPrefix + token | |
| ) | |
| this.retrieveToken = (pathPrefix, location) => ( | |
| location.pathname.substr(pathPrefix_.length) | |
| ) | |
| } | |
| const myHistory = new HTML5History(null, new SaneTokenTransformer()) | |
| /* | |
| Supplying your own transformer which does not add the | |
| location's search string to the end fixes the issue. | |
| Links: | |
| Source code: https://github.com/google/closure-library/blob/master/closure/goog/history/html5history.js#L272 | |
| Existing issues: | |
| https://groups.google.com/d/topic/closure-library-discuss/jY4yzKX5HYg/discussion | |
| https://github.com/google/closure-library/issues/582 | |
| Hopefully this saves you time. | |
| */ |
I had to modify cljs version a bit to work.
(def token-transformer (Html5History.TokenTransformer.))
(set! (.-createUrl token-transformer)
(clj->js (fn [token path-prefix location]
(str path-prefix token))))
(set! (.-retrieveToken token-transformer)
(clj->js (fn [path-prefix location]
(.-pathname location))))
Another example of cljs implementation https://github.com/venantius/accountant/pull/48/files
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this! For anyone that comes across this using ClojureScript, here's the appropriate code: