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: Cannot export config after declaration in export list format #70008

Open
mahmad97 opened this issue Sep 12, 2024 · 0 comments
Open
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.

Comments

@mahmad97
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/nameless-hill-dt239f

To Reproduce

Link to codesandbox is provided and it shows the issue as well.

Current vs. Expected behavior

The config object from middleware.ts is loaded correctly in case 1 (exporting during declaration) while it is not loaded correctly in case 2 (exporting after decleration):

import { NextRequest, NextResponse } from 'next/server';

// case 1 -> works as expected
export const middleware = (request: NextRequest) => {
	console.log(request.nextUrl.pathname);

	return NextResponse.next();
};

export const config = {
	matcher: '/about',
};

// case 2 -> does not work as expected
const middleware = (request: NextRequest) => {
	console.log(request.nextUrl.pathname);

	return NextResponse.next();
};

const config = {
	matcher: '/about',
};

export { middleware, config };

When the config is loaded only paths with /about should be printed in the console. If not loaded, all paths are printed in the console. Also no error is reported (I am not sure if an error should be reported).

Provide environment information

Sandbox is running on next.js version 15.0.0-canary-148. I encountered the same issue locally on 15.0.0-rc.0. Used app router in both instances.

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Exporting the config object while declaring it solves the issue for me, but the difference should only be one of syntax and not functionality. The middleware function is unaffected by this issue, only the config object is affected.

This issue might be related but I'm not completely sure: #67169
In the case that it is feel free to remove this issue/combine both issues.

@mahmad97 mahmad97 added the bug Issue was opened via the bug report template. label Sep 12, 2024
@github-actions github-actions bot added the Middleware Related to Next.js Middleware. label Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.
Projects
None yet
Development

No branches or pull requests

1 participant