Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set URI fragment/hash #117

Open
hkeide opened this issue Mar 12, 2021 · 7 comments
Open

Set URI fragment/hash #117

hkeide opened this issue Mar 12, 2021 · 7 comments

Comments

@hkeide
Copy link

hkeide commented Mar 12, 2021

Hello, thanks for a great library, finally something sane and simple.

Can I set the hash part of a URL in a link? I've looked everywhere and I can't find it mentioned anywhere.

Also, is there a good way to navigate to a completely custom full URL without having the library build the href? For example I get a sent a full URL by the server and I want to navigate to it without knowing which route it goes to. Should I just use the browser history API directly for this use case?

@nareshbhatia
Copy link
Owner

Hi @hkeide, this router was designed with the assumption of push state. May I know why you need to go for hash part?

For your second question, the router only knows about the specified routes sent to it in the constructor. I think it should be ok to use the history API outside of the router. Let me know how this works out for you.

@hkeide
Copy link
Author

hkeide commented Mar 14, 2021

Hi, I have a long feed page, and I want to be able to scroll to a specific location on the page using a link without having to reload the SPA

@nareshbhatia
Copy link
Owner

If I am not mistaken, you should be able to render links on that page with associated hashes and they should jump to the right location. For example: <a href="/departments/electronics#cannon">Go to Cannon</a>. If it causes a page refresh, you can encapsulate this in a component and prevent the refresh, something like this.

@hkeide
Copy link
Author

hkeide commented Mar 17, 2021

Doesn't a raw a tag like that always cause a page reload? It seems I have to override routerStateToUrl() so that routerStore.goToState() will do the right thing in the onClick handler? I can't see a way to do this without forking the library. Please correct me if I'm wrong. I can create a PR for adding fragment capability if you're interested.

@laustdeleuran
Copy link

Doesn't a raw a tag like that always cause a page reload?

@hkeide, I believe that is correct.

If it causes a page refresh, you can encapsulate this in a component and prevent the refresh, something like this.

@nareshbhatia, it's somewhat discouraging that I need to basically rewrite that entire component in order to create hash-links. Is there no better solution?

@nareshbhatia
Copy link
Owner

Hi @laustdeleuran, please feel free to submit a PR for this.

@laustdeleuran
Copy link

@nareshbhatia I actually looked into forking the RouterLink component to support hashes, but I don't see how I could even do that. The RouterLink component uses RouterState.goToState under the hood, which also doesn't support hashes, so forking the code in that component seems like it wouldn't work at all.

Effectively, it seems like I'd have to extend RouterState.goToState, but that is just a shallow wrapper on top of the TransitionState class. And I don't see any event hooks or public API that would allow me to extend that.

So, in other words, I'd have to submit a PR modifying the core functionality of this library, which I'm honestly down for, but I would love some more input on where this functionality should live. It feels clear to me that it should not live in the RouterLink component, but it feels pretty opaque where would be a more appropriate place for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants