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(router): fallback for method-shadowed routes #461

Merged
merged 8 commits into from
Jul 26, 2023
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jul 25, 2023

πŸ”— Linked issue

Resolves #460

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR resolves router issue with multiple routes matching shadowing their method by using a (lazy initialized) multi-matcher.

Performance note: While this adds small overhead, the prefix matcher is really fast specially for static routes (without params) which are usually the usecase for this feature for implementing renderer fallback routes. Regardless it is super fast and used for nitro route rules matcher as well (src).

Performance: After first resolution of method-shadowed match, we update main match with missing method. This means that, only once (in server lifecycle) the resolution will happen.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Merging #461 (0d49698) into main (8c73933) will increase coverage by 0.09%.
The diff coverage is 87.87%.

@@            Coverage Diff             @@
##             main     #461      +/-   ##
==========================================
+ Coverage   78.38%   78.47%   +0.09%     
==========================================
  Files          26       26              
  Lines        2803     2834      +31     
  Branches      408      414       +6     
==========================================
+ Hits         2197     2224      +27     
- Misses        606      610       +4     
Files Changed Coverage Ξ”
src/router.ts 96.29% <87.87%> (-2.18%) ⬇️

@pi0 pi0 marked this pull request as ready for review July 25, 2023 21:25
Copy link
Member

@Hebilicious Hebilicious left a comment

Choose a reason for hiding this comment

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

This looks great !
I was thinking if the name catchAll wouldn't be more fitting than shadow, but maybe there's a use case that doesn't involve ** that I'm not aware of ?

test/router.test.ts Show resolved Hide resolved
@Hebilicious Hebilicious added the bug Something isn't working label Jul 26, 2023
@pi0 pi0 marked this pull request as draft July 26, 2023 09:38
@pi0
Copy link
Member Author

pi0 commented Jul 26, 2023

I was thinking if the name catchAll wouldn't be more fitting than shadow, but maybe there's a use case that doesn't involve ** that I'm not aware of ?

All nitro route rules use this feature to catch matching paths :)


Marking PR as draft since I'm not still sure if this is best approach for fixing the issue

@pi0
Copy link
Member Author

pi0 commented Jul 26, 2023

Update: Spent couple of minutes trying different data structures and methods for radix3 and router implementations. There was to experiments:

  1. Use methods in radix routes to directly match by method

This can cause side effects, especially for routes being registered with /**$ or /*$. We need to add another segment like {path}/?{method}.

  1. Support filter function for radix3 lookup

It adds a function calling overhead per-request per-tree-node. Ideally this would be the best if radix3 natively supports methods.

Therefore i think we can move forward with this (dirty) workaround as fix until shipping native method matching for radix3 (unjs/rou3#58)

@pi0 pi0 marked this pull request as ready for review July 26, 2023 17:27
@pi0 pi0 merged commit 7764b99 into main Jul 26, 2023
6 checks passed
@pi0 pi0 deleted the fix/router-shadow branch July 26, 2023 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multiple routes matching path with different methods shadow each other
2 participants