Coder Social home page Coder Social logo

Comments (9)

mjackson avatar mjackson commented on April 25, 2024

The goal of this library never was to support the pushState spec. Since we only support using pathnames, we don't ever need to check to make sure you're using the same origin, so we don't ever need to throw an exception.

I often run apps on the same host but different base paths. I worked around this by parsing the pathname out of the URL before calling pushState.

I'm not sure I understand your problem here. Can't you just use window.location.pathname?

from history.

jkk avatar jkk commented on April 25, 2024

Not supporting the pushState spec makes sense; I made an assumption based on the similarity in naming and arguments.

Background on my problem: I have an app installed at:

http://localhost/projects/myapp/

and I have a Link component (I am not using React Router) which turns this:

<Link href="/foobar">Foobar</Link>

into:

<a href="http://localhost/projects/myapp/foobar" onClick={...}>Foobar</a>

where the onClick handler interrupts the click and handles it with history -- passing the value of the href attribute to pushState -- instead of actually letting the browser follow the link. I want a valid href for server-side rendering, opening in a new tab, etc.

This worked when I used native HTML5 history, but not when I switched to your lib. Everything else about the transition was easy.

I adapted my code to pass pathnames to pushState, so it's not the end of the world if this library doesn't support absolute URLs. But the behavior -- browser navigating correctly but the listen callback returning bad info -- caught me by surprise and took some time to debug.

from history.

mjackson avatar mjackson commented on April 25, 2024

browser navigating correctly but the listen callback returning bad info

What bad info did you get in the listen callback? Did you expect location.pathname to be /foobar?

from history.

jkk avatar jkk commented on April 25, 2024

If I was at the following URL:

http://localhost/projects/myapp/

And ran the code in my original post, location.pathname would be "http://localhost/projects/myapp/" instead of "/projects/myapp/". I.e., the absolute URL that was passed.

from history.

jkk avatar jkk commented on April 25, 2024

I can understand if your position is that, if you pass an absolute URL (bad input), things will act strangely (window.location.pathname will be different from the callback location.pathname).

I would argue, though, that if the lib does not support absolute URLs, an exception should be thrown if one is passed. There may be others, like me, that are transitioning their code from native HTML5 history, and will be confused by the strange behavior.

from history.

mjackson avatar mjackson commented on April 25, 2024

Ah, I see now. Yes, that is surprising behavior.

Instead of throwing, what if we just silently do the right thing and give people the correct location.pathname?

from history.

jkk avatar jkk commented on April 25, 2024

Works for me! I would submit a patch but I'm short on time right now.

from history.

mjackson avatar mjackson commented on April 25, 2024

Fixed and released in 1.6.0.

from history.

jkk avatar jkk commented on April 25, 2024

Perfect, thank you! This lib has been super helpful.

from history.

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.