-
-
Notifications
You must be signed in to change notification settings - Fork 353
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
+middleware example with @universal-middleware/router #2050
base: main
Are you sure you want to change the base?
Conversation
const middlewares = (await getMiddlewares()) as (RouteDefinition | MiddlewareDefinition)[] | ||
const router = new UniversalRouter() | ||
apply(router, middlewares) | ||
app.all('*', createMiddleware(() => router[universalSymbol])()) | ||
// TODO replace with UniversalExpressRouter once done. | ||
// It should look like this: | ||
// const router = new UniversalExpressRouter(app) | ||
// apply(router, middlewares) | ||
// So no need for manually calling app.*, and rely on express routing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved to vike-node
, which would be responsible to create the proper Universal*Router
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 And eventually vike-{express,hono,...}
so that one Vike extension = one tool (e.g. to fit the text next to the landing page's slot machine).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw. I'm currently working on the Vike CLI and one neat thing we could eventually do is to have vike-express
have a built-in Express.js server boilerplate, so that the user doesn't have to write a single line of server code (but the user can eject that server boilerplate if he needs to).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be simple enough, and I like the idea.
I can see that in Bati we would then just add a comment somewhere or a paragraph in the README to explain that it can be ejected.
We could even allow the user to retrieve the built-in express like this:
import { app } from "vike-express";
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
We could even allow the user to retrieve the built-in express like this:
Neat idea ✨
I wonder whether/how we can add a name to a universal middleware, so that users can selectively remove middlewares. Use case: user is using |
I was thinking about that with the same use case in mind. IMO the best solution would be to be able to just eject the +middleware, but I think we're not there yet. |
Yea, I think a |
const middlewareTelefunc = decorate(telefuncUniversalMiddleware, { | ||
method: 'POST', | ||
path: '/_telefunc' | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
withRoute
replaced by a more generic decorate
helper, to make things more explicit, and simplify adding props in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to keep it generic. (Name idea: enhance()
instead of decorate()
, but I'm fine with decorate()
if you prefer it.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enhance
was my first name idea, but I preferred decorate
. I'll let that macerate a little and see how I feel about it when we're closer to a finished implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 The thing I prefer about enhance()
is that it's more visual and concrete, whereas decorate()
is more abstract and vague (or maybe it's subjective), thus more difficult to remember.
Bonus: in the docs we can mention "enhanced middlewares", for example:
-
Enhanced middlewares are middlewares that define a route
-
Enhanced middlewares must have a name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I guess we could also say "decorated middlewares".
] | ||
const middlewareVike = decorate(vikeUniversalMiddleware, { | ||
method: 'GET', | ||
path: '/**' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path
property is kinda tricky to handle.
Most servers have the same basic syntax to declare a path (e.g. /
, /:name
).
But there are also some discrepancies even with some basic syntax like /**
and /*
. Some servers will match /a/b/c
with both, others will only match such path with the /**
syntax (and some do not understand what /**
means).
We could have a translation layer for those syntax, but this seems a bit overkill.
My current stance on this subject is as follows:
When using decorate
in a lib, use a syntax that works with ALL servers if possible (we could have a really minimal translation layer if we discover some recurring use cases that would benefit from it).
When using decorate
in user-land, the user is able to use a syntax specific to the server in use, and when one ever needs to migrate to another server, this should be trivial to manually migrate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm missing something, we could make it that internally all universal middlewares are catch-all middlewares but we wrap a if (!matches(url, path)) return
condition. This would allow us to use any path syntax we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 that could work.
Currently I'm relying on servers themselves to handle the routing, and fallback to using rou3
for the UniversalRouter
. But this is the cause of what I mentioned above.
If I use rou3
for all servers in catch-all, I think that would also simplify some other future ideas I had for handling deployments.
For now I chose rou3
because it's tree-shakable and minimal: (it would be directly bundled into universal-middleware
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Sounds good.
Another idea: we only support a trivial path syntax and we tell users to define a catch-all with a if (!url...) return
condition if they want to define advanced routing logic.
Kinda like Vike: Route Strings are very basic while Route Functions enable programmatically defined routes. This strategy has been working well: no one's complaining about the limitations of Route Strings.
But using rou3
sounds good to me as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rou3
also have the benefit of prioritizing nested routes.
For instance, with the following patterns /**
and /a/**
, the path /a/b/c
will always match the second pattern.
- This is usually what the user will expect
- This does not require the user to register routes in a specific order
- This does not require a lib to specify an
order
for middlewares with apath
WIP around how we can move some logic used in
+middleware.ts
process touniversal-middleware
.@brillout This is mostly to discuss what we should port to
universal-middleware
and create a proper API.universal-middleware
in progress PR: magne4000/universal-middleware#95