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

+middleware example with @universal-middleware/router #2050

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

magne4000
Copy link
Member

@magne4000 magne4000 commented Jan 7, 2025

WIP around how we can move some logic used in +middleware.ts process to universal-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

Comment on lines 37 to 45
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
Copy link
Member Author

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.

Copy link
Member

@brillout brillout Jan 7, 2025

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

Copy link
Member

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

Copy link
Member Author

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";

Copy link
Member

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 ✨

@brillout
Copy link
Member

brillout commented Jan 8, 2025

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 vike-react-authjs but wants a custom server integration. The answer could be that he has to fully eject vike-react-authjs or he replaces the universal middleware with a custom middleware. I guess both are okay, although if it's easy to implement then it would be nice to have some kind of name or other means to select a universal middleware.

@magne4000
Copy link
Member Author

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 vike-react-authjs but wants a custom server integration. The answer could be that he has to fully eject vike-react-authjs or he replaces the universal middleware with a custom middleware. I guess both are okay, although if it's easy to implement then it would be nice to have some kind of name or other means to select a universal middleware.

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.
So adding a name onto them could do the trick for now (and it doesn't hurt).

@brillout
Copy link
Member

brillout commented Jan 8, 2025

Yea, I think a name is always a good thing to have. I'm just wondering how it could fit with the current DX with withRoute() and withOrder(). Maybe merging these into a single withMeta() could do the trick. Actually, I feel like naming that it withRoute() instead of withMeta() could be a better name. We could then require a name when using withRoute() (and iff. using withRoute() because in the user land setting a name isn't that useful I guess).

@magne4000
Copy link
Member Author

Some documentation around the enhance and apply helpers: https://d80b2041.universal-middleware.pages.dev/helpers/enhance (PR: magne4000/universal-middleware#97)

@brillout
Copy link
Member

Neat 😍

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