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

Demo Site: add concept multiple middlewares that get chained together #2728

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nsams
Copy link
Member

@nsams nsams commented Nov 13, 2024

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

This should avoid having one possibly large middleware file doing all sorts of different things. With this we can separete and structure better.

return NextResponse.next();
}
export default chain([withPredefinedPagesMiddleware, withAdminRedirectMiddleware, withDamRewriteMiddleware]);
Copy link
Collaborator

@johnnyomair johnnyomair Nov 13, 2024

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();
Copy link
Contributor

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}`));
Copy link
Contributor

@fraxachun fraxachun Nov 14, 2024

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 };
Copy link
Contributor

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

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.

3 participants