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

Is there a way to bypass the base of the Router in a Link? #431

Closed
jeerbl opened this issue Apr 1, 2024 · 9 comments
Closed

Is there a way to bypass the base of the Router in a Link? #431

jeerbl opened this issue Apr 1, 2024 · 9 comments

Comments

@jeerbl
Copy link

jeerbl commented Apr 1, 2024

I tried <Link href="~/path">...</Link> but the ~ does not seem to bypass the base of the router. On top of that, when specifying an HrefFormatter to the router to bypass manually in the Router configuration, the ~ character is not included in the link.

Is there any other way to achieve this? Let me know if this need more clarification.

Jérôme

@molefrog
Copy link
Owner

molefrog commented Apr 1, 2024

Hi @jeerbl, could you share a piece of code? This is supposed to work.

@jeerbl
Copy link
Author

jeerbl commented Apr 2, 2024

Hi @molefrog, I made a CodeSandbox. I've actually seen something strange, looking deeper into the problem.

What I'm trying to do is to have the same component in two different contexts, go to the same URL, and Wouter actually does, but the href set on the <a> element is for some reason wrong and different than the path Wouter actually navigates to.

In that example, if you access /sub/url, you'll see the link prefixed with ~ go to the actually root URL /url. When you're in /url, the <Link> still goes to /url (which is what I want), but the URL displayed in href attribute of the <a> element is actually /sub/url.

First thing is, what should happen here in my opinion is to have the <a> href element to be the same as where Wouter navigates to.

Second thing is, should Wouter actually ignore the hrefs when navigating? Is this what's expected? It benefits my case but I'm not sure if that's what's expected here.

@molefrog
Copy link
Owner

molefrog commented Apr 2, 2024

but the URL displayed in href attribute of the <a> element is actually /sub/url.

That's because you have a custom hrefs formatter, if you remove it you the href will be /url isn't that something you want?

@molefrog
Copy link
Owner

molefrog commented Apr 2, 2024

The hrefs only affects the href attribute rendered and it is mostly needed for server-side rendering (e.g. when you use hash-based routing, the html rendered must be <a href="#/" /> and not <a href="/" />). It does not affect the actual client-side navigation.

@jeerbl
Copy link
Author

jeerbl commented Apr 2, 2024

I see, thank you. So just formatting and nothing else.

Closing this.

@jeerbl jeerbl closed this as completed Apr 2, 2024
@jeerbl
Copy link
Author

jeerbl commented Apr 2, 2024

Actually reopening this for advice. I updated the CodeSandbox.

Here, in this new piece of code, the component LinkComponent is going to /url so, in the <Router base="/sub"> this link will be /sub/url and in the first <Router> line 17, it will go to /url.

My question is—if I want the LinkComponent to always go to /sub/url, even when used on line 17, what would you suggest?

I would want all links under the <Router> line 14 to always add /sub to the route.

@jeerbl jeerbl reopened this Apr 2, 2024
@molefrog
Copy link
Owner

molefrog commented Apr 3, 2024

My question is—if I want the LinkComponent to always go to /sub/url, even when used on line 17, what would you suggest?

Just use ~/sub/url. The base path for default router is always /

@molefrog
Copy link
Owner

molefrog commented May 4, 2024

Hi @jeerbl is this issue still relevant?

@jeerbl
Copy link
Author

jeerbl commented May 4, 2024

Hi @molefrog, actually it isn't! Closing this issue :)

@jeerbl jeerbl closed this as completed May 4, 2024
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

2 participants