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

Fix wouter link to prevent crash without href/to #485

Merged
merged 2 commits into from
Aug 28, 2024

Conversation

dswbx
Copy link
Contributor

@dswbx dswbx commented Aug 28, 2024

<Link /> components are mainly used together with href and/or to attributes, but there are cases where you'd only rely on the onClick property to navigate. In these cases, wouter crashes when it tries to check for absolute paths.

This PR fixes it by making sure the absolute path test is pointed against a string and not undefined.


(Context: I noticed this issue when using wouter together with plasmic. Plasmic allows to specify the link component being used to take advantage of client-side navigation. In some cases though you'd use the interactions (which is triggered inside onClick) to navigate in response to e.g. an API call. In these cases, wouter crashed).

Copy link

stackblitz bot commented Aug 28, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

codecov bot commented Aug 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (ab46b00) to head (23873a1).
Report is 2 commits behind head on v3.

Additional details and impacted files
@@            Coverage Diff            @@
##                v3      #485   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines            1         1           
=========================================
  Hits             1         1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -265,7 +265,9 @@ export const Link = forwardRef((props, ref) => {

// handle nested routers and absolute paths
const href = router.hrefs(
targetPath[0] === "~" ? targetPath.slice(1) : router.base + targetPath,
(targetPath ?? "")[0] === "~"
Copy link
Owner

Choose a reason for hiding this comment

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

Thank you for the PR!
As a suggestion, this could also be done by providing the default value for to when destructuring props:

  const {
    to = "",
    href: targetPath = to,

(above in this file)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's better, thanks! Will change it to your suggestion

@molefrog molefrog merged commit 3466ef0 into molefrog:v3 Aug 28, 2024
4 checks passed
@molefrog
Copy link
Owner

Released in v3.3.5

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.

2 participants