Giter VIP home page Giter VIP logo

Comments (9)

dmichulke avatar dmichulke commented on September 28, 2024 1

@SevereOverfl0w

Yes, of course. I was considering only empty paths before that hash. But the general solution is to use js/location.hash (didn't know this one) which returns everything beginning with the hash.

As stated above, I agree you shouldn't change the bidi behavior, just maybe mention how it can be nicely resolved in a SPA.
I posted the workaround because it's probably a usual problem people programming SPAs will have yet it wasn't immediately obvious (at least to me on a Monday after lunch) how to resolve it.

from bidi.

HaskellZhangSong avatar HaskellZhangSong commented on September 28, 2024 1

BTW, this issue also trapped me.

from bidi.

NOBLES5E avatar NOBLES5E commented on September 28, 2024

I would suggest match-route function assumes the path provided is just the path we want to match without modification. If we want to remove the # or query parameters etc we should call just-path function by ourselves.

from bidi.

dmichulke avatar dmichulke commented on September 28, 2024

As a "workaround" (not meaning to say this is a bug in bidi because on the server side this well-defined behavior) - when using accountant, you can just

(defn- unprefixed
  [path]
  (if (clojure.string/starts-with? path "/#")
    (subs path 2)
    path))

and then, e.g.,

  (accountant/configure-navigation!
   {:nav-handler (comp (fn [path] 
     ;; here goes your old nav handler
     ) unprefixed)
    :path-exists? (comp (fn [path] 
     ;; here goes your old exists handler
     ) unprefixed})

You must then rewrite the routes without the leading /#

from bidi.

SevereOverfl0w avatar SevereOverfl0w commented on September 28, 2024

Depending on what you're trying to achieve, there's different solutions here. js/location.hash gives the result "#/about-me", meaning that the solution is to do (subs js/location.hash 1) for the URL.

However, if you have intentions of doing URLs like /about#/me as well as /#/foo, then doing this won't let you distinguish between the two. In this case you will need a scheme for distinguishing between them, I suppose you could drop the # in most cases, unless you had both /about#/me and /#/about/me and you wanted them to be different.

The intention of just-path was to prevent tripping over URLs with query params & other transient artifacts, particularly with ring. The correct behavior here is quite hard to get right. Ultimately bidi isn't intending to work on location hashes, but the ideal is to work on pure paths.

One could argue that if a location hash is found in a URL then use it. But that falls down because:

  • Location hashes can be used for in-page navigation (linking within elements on a page)
  • Users could break routing by adding a location hash to the URL

from bidi.

SevereOverfl0w avatar SevereOverfl0w commented on September 28, 2024

Documentation may well be the best way to resolve this. We're having a focus on documentation at JUXT right now, so bidi may be a good candidate for improving as a result of that.

from bidi.

Folcon avatar Folcon commented on September 28, 2024

Personally I'm a little puzzled by this, wouldn't a url fragment caused by an #section-name also be preserved on the server side? Or does that already occur?

Anyway, what is the expected way to resolve this?

from bidi.

SevereOverfl0w avatar SevereOverfl0w commented on September 28, 2024

Url fragments aren't sent to the server, ever.

from bidi.

Folcon avatar Folcon commented on September 28, 2024

Yea, my mistake, I've seen enough url fragment laden urls get routed that it never occurred to me to check if the browser was actually forwarding them...

Looking at the js/location.hash approach, not certain how to make this work, here's my example:

(def routes ["/"
             {"" :home
              "#/login"  :login
              "#/logout" :logout
              true :not-found}])

So I route to :login

(bidi/path-for routes :login)
#_=> "/#/login"

window.location.hash gives me "#/login" which when I check for matches gives me:

(bidi/match-route routes "#/login")
#_=> nil
(bidi/match-route routes "/login")
#_=> {:handler :not-found}
(bidi/match-route routes "/#/login")
#_=> {:handler :home}

So not quite clear how that works...

from bidi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.