Skip to content

Conversation

@aanders
Copy link

@aanders aanders commented Sep 23, 2024

The NavItem "url" attribute causes the browser to make a new request for the named page. Our website is (currently) set up as a Single Page Application (SPA) using the Yew router, and can't support direct server requests for the subpages*. In order to have the WASM application handle the request, the "onclick" attribute has to be used instead.

*unless the server is explicitly configured to serve index.html for unrecognized paths, which we can't do for GitHub Pages

References:

There were two additional details that needed to be fixed:

  • NavDropdownItemProps did not support the onclick attribute until yew-bootstrap version 0.11
  • The MyNavBar component needed to be placed beneath the BrowserRouter node in order to enable use of the navigator API.

Tested using Apache as a static web server. Attempts to navigate with the navbar items from the existing code returned 404 errors. The same attempts to navigate with the code from this commit correctly return the appropriate page content.

There is one remaining issue. Switching from the "url" attribute to the "onclick" attribute has caused the cursor shape on mouseover to change: it is now a text selection icon rather than the typical hand icon. This is a poor behavior for UX and needs to be fixed.

The NavItem "url" attribute causes the browser to make a new request for
the named page.  Our website is (currently) set up as a Single Page
Application (SPA) using the Yew router, and can't support direct server
requests for the subpages*.  In order to have the WASM application
handle the request, the "onclick" attribute has to be used instead.

*unless the server is explicitly configured to serve index.html for
unrecognized paths, which we can't do for GitHub Pages

References:
- https://yew.rs/docs/concepts/router
- https://yew.rs/docs/concepts/router#navigation
- https://yew.rs/docs/more/deployment#serving-indexhtml-as-a-fallback

There were two additional details that needed to be fixed:
- NavDropdownItemProps did not support the onclick attribute until
  yew-bootstrap version 0.11
- The MyNavBar component needed to be placed beneath the BrowserRouter
  node in order to enable use of the navigator API.

Tested using Apache as a static web server.  Attempts to navigate with
the navbar items from the existing code returned 404 errors.  The same
attempts to navigate with the code from this commit correctly return the
appropriate page content.

There is one remaining issue.  Switching from the "url" attribute to the
"onclick" attribute has caused the cursor shape on mouseover to change:
it is now a text selection icon rather than the typical hand icon.  This
is a poor behavior for UX and needs to be fixed.
@aanders
Copy link
Author

aanders commented Sep 23, 2024

I'm not sure this is the best way to do it, but it does at least seem to work. It sounds like the Link component described in the documentation would be a better choice than using an onclick callback directly, but I'm not sure how to use that in conjunction with the yew-bootstrap components.

The solution is to put a URL attribute that simply points at "#".
Found in the yew-bootstrap example code at
https://github.com/isosphere/yew-bootstrap/blob/main/examples/basics/src/main.rs#L50
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

Successfully merging this pull request may close these issues.

3 participants