-
Notifications
You must be signed in to change notification settings - Fork 8
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
Demo Site: add concept multiple middlewares that get chained together #2728
base: main
Are you sure you want to change the base?
Conversation
This should avoid having one possibly large middleware file doing all sorts of different things. With this we can separete and structure better.
bd127a0
to
77021aa
Compare
|
||
return NextResponse.next(); | ||
} | ||
export default chain([withPredefinedPagesMiddleware, withAdminRedirectMiddleware, withDamRewriteMiddleware]); |
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.
Alternatively, we could use a behavior similar to the Next middleware where returning undefined
is the same as NextResponse.next()
:
export async function middleware(request: NextRequest, response: NextResponse) {
return await withPredefinedPagesMiddleware(request, response) ??
await withAdminRedirectMiddleware(request, response) ??
await withDamRewriteMiddleware(request, response)
}
The separate "middlewares" could then be simplified to:
async withAdminRedirectMiddleware(request: NextRequest, response: NextResponse) => {
if (request.nextUrl.pathname === "/admin" && process.env.ADMIN_URL) {
return NextResponse.redirect(new URL(process.env.ADMIN_URL));
}
}
What do you think?
return current(next); | ||
} | ||
|
||
return () => NextResponse.next(); |
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 default response should not be created here. For example in comet-starter we have to get the current scope here and create a rewrite. This logic does not belong to this helper function.
export function withDamRewriteMiddleware(middleware: CustomMiddleware) { | ||
return async (request: NextRequest) => { | ||
if (request.nextUrl.pathname.startsWith("/dam/")) { | ||
return NextResponse.rewrite(new URL(`${process.env.API_URL_INTERNAL}${request.nextUrl.pathname}`)); |
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're returning here a custom response without calling the "next" middleware. How would we solve this for the block-preview, which - if implemented as middleware - would also make this return but still needs to apply the CSP-Headers (which I guess is implemented as middleware).
return async (request: NextRequest) => { | ||
const { pathname } = new URL(request.url); | ||
|
||
const scope = { domain }; |
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.
In comet-starter getting the scope is more complicated and can possibly return a redirect: https://github.com/vivid-planet/comet-starter/blob/main/site/src/middleware.ts#L33
Should this be handled in every middleware which needs the scope?
E.g.:
const scope = getScopeFromHost(getHostByHeaders(request.headers);
if (scope instanceof Response) return response; // pseudocode
...
This should avoid having one possibly large middleware file doing all sorts of different things. With this we can separete and structure better.
based on https://www.58bits.com/blog/chaining-or-combining-nextjs-middleware
TODO