feat!: integrate router with app and app.fetch
#822
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a major upgrade for h3! Since the initial version, h3's core was modeled as a stack runner, using an array of middleware (path prefixes and event handlers) matching them one by one, and stopping as soon as the first middleware responded.
We then added a new router system to h3 using radix3 which uses route pattern matching.
With advancements of rou3, we can actually have a much smarter combined app.
In order to keep compatibility with previous versions, the h3 app smartly chooses between 3 strategies (in order):
app.use(handler)
app.use("/prefix", {})
(this is not encouraged due to perf overhead!)app.all(handler
orapp.[method](handler)
Any of the middleware can still return a response to stop the process (this is to keep more backward compatibility) but there are new behavior changes:
app.use("/path", handler)
only match/path
route. For matching all subpaths like before, it should be updated toapp.use("/path/**", handler)
event.path
received in each handler will have a full path without omitting the prefixes.useBase(base, handler)
wrapper can be used if old behavior is desired.match
function forapp.use
is not supported anymore (middleware can skip themselves based on path)Other changers:
app.options
(to access config) is renamed toapp.config
(app.options
registers an event handler for OPTIONS HTTP method)Migration guide: #787