Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Allow to export "empty" page middleware #37343

Closed
vklimontovich opened this issue May 31, 2022 · 1 comment
Closed

Allow to export "empty" page middleware #37343

vklimontovich opened this issue May 31, 2022 · 1 comment

Comments

@vklimontovich
Copy link

Describe the feature you'd like to request

In some cases, next app will require middleware only if certain config param is enabled. For example, the app can record usage only if certain env variable is set.

To avoid execution of middleware that does nothing, it would be nice to allow export "null" or "undefined" middleware that signals Edge Framework to not run middleware at all.

Describe the solution you'd like

If _middleware.ts export undefined or null, next should not run it at all

import {NextMiddleware, NextResponse} from "next/server";

const middleware: NextMiddleware = (req, res) => {
    //do something
    return NextResponse.next()
}

export default process.env.FEATURE_THAT_REQUIRE_MIDDLEWARE_ENABLED ? middleware : null;

Describe alternatives you've considered

Alternative solution would be like this:

import {NextMiddleware, NextResponse} from "next/server";

const middleware: NextMiddleware = (req, res) => {
    if (process.env.FEATURE_THAT_REQUIRE_MIDDLEWARE_ENABLED) {
        //do something
    } else {
        return NextResponse.next()
    }
}

export default middleware;

However, in this case middleware is executed all the time, which could increase usage bills, response time etc

@matamatanot
Copy link
Contributor

#36772

declare your middleware in the root folder and use NextRequest parsed URL to define which path the middleware code should be executed for.

@vercel vercel locked and limited conversation to collaborators May 31, 2022
@balazsorban44 balazsorban44 converted this issue into discussion #37346 May 31, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants