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

gloo-history based yew-router #2239

Merged
merged 18 commits into from Dec 19, 2021
Merged

Conversation

futursolo
Copy link
Member

@futursolo futursolo commented Dec 5, 2021

Description

This pull request consists of the following changes:

  • Histories are now provided by gloo-history.
  • HashRouter for hash-based routing.
  • Navigator API which supersedes the History API and shields location() & listen() access (users should use hooks & RouterScopeExt instead).
  • Navigator hooks and contexts only update when History is swapped with a different one or basename changes.
  • Lints on hashs in routes and relative paths in routes.

Checklist

  • I have run cargo make pr-flow
  • I have reviewed my own code
  • I have added tests

@futursolo
Copy link
Member Author

futursolo commented Dec 5, 2021

This PR is still blocked by: rustwasm/gloo#178

So I am using a branch that resides on my fork of gloo.

But other than that, this PR is good to go.

I will update this PR when gloo-history is released.

@github-actions
Copy link

github-actions bot commented Dec 5, 2021

Visit the preview URL for this PR (updated for commit 27beca5):

https://yew-rs--pr2239-gloo-history-quxda239.web.app

(expires Sat, 25 Dec 2021 13:14:16 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

@futursolo
Copy link
Member Author

I have updated this PR to use the latest version of gloo.

Also reverted most of #2214, sorry.

@Follpvosten
Copy link

Can confirm that this works for me (and would fix my main blocker for migration to yew 0.19, which is hash routing).

A minor thing I noticed (which is not a deal breaker for me) is that <Link>s don't have a hash in the href; I know this might be hard to fix and it's not really of much concern to me personally (because the component catches the onclick event anyways), but it's something that caught my eye.

@Follpvosten
Copy link

Oh, I discovered another issue: Reloading the page via Ctrl-R always causes a not found. Still not a dealbreaker for me though.

@futursolo
Copy link
Member Author

Oh, I discovered another issue: Reloading the page via Ctrl-R always causes a not found. Still not a dealbreaker for me though.

I cannot reproduce this by swapping BrowserRouter with HashRouter in the router example.

Could you please provide a reproducible example?

@Follpvosten
Copy link

Could you please provide a reproducible example?

I will look into it. It might have to do with a JS library I use - namely, keycloak's JS library, which I mostly treat as a black box that handles authentication for me. It does this thing where it redirects to a different page and appends a bunch of query parameters; it removes them again when it's done with its things, but maybe this confuses the HashRouter somehow (which I only create after all the auth stuff is handled, but who knows).

Then again, as I said, this is not a big issue for me; having my users always start on the default page is not really an issue for me. So I'm likely to postpone this until I have some free time.

Copy link
Member

@hamza1311 hamza1311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for the most part, just a few comments. Also, please fix the conflicts.

packages/yew-router/src/history.rs Outdated Show resolved Hide resolved
packages/yew-router/src/navigator.rs Show resolved Hide resolved
packages/yew-router/src/navigator.rs Show resolved Hide resolved
packages/yew-router/src/router.rs Show resolved Hide resolved
hamza1311
hamza1311 previously approved these changes Dec 14, 2021
Copy link
Member

@hamza1311 hamza1311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

If any issues come up, those can be dealt with in future PRs

@voidpumpkin voidpumpkin added A-yew-router Area: The yew-router crate A-yew-router-macro Area: The yew-router-macro crate labels Dec 15, 2021
Copy link
Member

@voidpumpkin voidpumpkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, would be good to resolve them on this PR so we dont forget in the future

Also Im seeing that the router page is becoming overloaded, we should split it in the future or it could possible be done in this PR if you want to

website/docs/concepts/router.md Outdated Show resolved Hide resolved
@@ -45,7 +45,7 @@ matched, the router navigates to the path with `not_found` attribute. If no rout
a message is logged to console stating that no route was matched.

Finally, you need to register the `<Router />` component as a context.
`<Router />` provides session history information to its children.
`<Router />` provides location information and navigator to its children.

When using `yew-router` in browser environment, `<BrowserRouter />` is recommended.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit could be expanded about BrowserRouter vs HashRouter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a deliberate choice that I made.
My rationale is that we should document the good design in the documentation.

If you really want to know everything, you should look at the API Reference (docs.rs).

It's almost never a good idea to use hash based routing and it's really a last resort when you have ran out of options.
(Poor SEO, Query string is emulated (which is also the cause of problem @Follpvosten is having), Collides with anchors, etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@futursolo Even though I agree with you, i am afraid of always having to answer this question in discord.
Perhaps we could just emphasize more that there are others. like the suggestion in other comment

website/docs/concepts/router.md Show resolved Hide resolved
@Follpvosten
Copy link

Follpvosten commented Dec 16, 2021

Update on my problem: I've verified that it works if I only create my HashRouter once auth has been fully handled. I still can't provide a reproducing example, but since this has fixed the issue for me, I can't really justify investing more time.

I'm assuming it's because there is a Query on the route at the start, somehow causing a NotFound which is handled and redirects to the default page once my Switch is created (which is the part that was previously only done once auth has been fully handled).

I think while writing this, I realized why the query causes a NotFound: Likely because it's after the hash route, it's interpreted to be a part of the route, not a separate thing.

This is how the URL looks while Keycloak is doing its thing:
Screenshot_20211216_130332

EDIT: So, looking at that URL, it's pretty clear that Keycloak is at fault here, because that's not really a valid query as it doesn't start with ?. I'm keeping my solution for now and will look into reporting that at Keycloak.

Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com>
Copy link
Member

@voidpumpkin voidpumpkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, just one comment regarding the title of the PR
And it can be done by whoever ends up merging this, probaly you @hamza1311 from the looks of it.
Since we use commit text when generating the changelog, the commit should have mentions that it adds basename and Hash router.
Title is important because default commit name when merging gets generated based on title.

We could just make it like Add HashRouter, basename and use gloo-history

@voidpumpkin voidpumpkin enabled auto-merge (squash) December 18, 2021 13:38
@voidpumpkin
Copy link
Member

or i can just go ahead and do it with the auto merge :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-router Area: The yew-router crate A-yew-router-macro Area: The yew-router-macro crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants