Skip to content

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

Merged
molefrog merged 2 commits intomolefrog:v3from
dswbx:fix/allow-link-without-href
Aug 28, 2024
Merged

Fix wouter link to prevent crash without href/to#485
molefrog merged 2 commits intomolefrog:v3from
dswbx:fix/allow-link-without-href

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).

@bolt-new-by-stackblitz
Copy link

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

@codecov
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.

// 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
@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