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 won't deploy in subpath with error no-server-import-in-page with React Server Components and concurrentFeatures #32467

Closed
KlotzJesse opened this issue Dec 13, 2021 · 7 comments
Labels
good first issue Easy to fix issues, good for newcomers Linting Related to `next lint` or ESLint with Next.js.

Comments

@KlotzJesse
Copy link

What version of Next.js are you using?

12.0.8-canary.4

What version of Node.js are you using?

local: 14.8.1 // vercel: 12

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When deploying an NextJs App to Vercel, with having a _middleware.js / _middleware.ts in a sub-path of pages, e.g. /pages/dashboard/_middleware.ts
the build fails with:
Error: next/server should not be imported outside of pages/_middleware.js. See https://nextjs.org/docs/messages/no-server-import-in-page. @next/next/no-server-import-in-page

Expected Behavior

Deploy sucessfully

To Reproduce

With:
concurrentFeatures: true
serverComponents: true

Add:
/pages/test/_middleware.js

import { NextResponse } from "next/server";

export default async function middleware(req, event) {
  return NextResponse.next();
}
@KlotzJesse KlotzJesse added the bug Issue was opened via the bug report template. label Dec 13, 2021
@KlotzJesse
Copy link
Author

KlotzJesse commented Dec 13, 2021

When added to /pages/_middleware.js:

It will deploy on vercel, but won't be executed. Related to #31803

When in sub path:
Normally, it won't even build, related to: #31659

All related to React Server Components, I think.

@KlotzJesse KlotzJesse changed the title Middleware doesn't run in subpath with error no-server-import-in-page Middleware doesn't run in subpath with error no-server-import-in-page with React Server Components and concurrentFeatures Dec 13, 2021
@KlotzJesse KlotzJesse changed the title Middleware doesn't run in subpath with error no-server-import-in-page with React Server Components and concurrentFeatures Middleware won't deploy in subpath with error no-server-import-in-page with React Server Components and concurrentFeatures Dec 13, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented Dec 14, 2021

This should have been fixed by #32139, but it looks like it wasn't. It reproduces with 12.0.8-canary.5 on my machine as you described.

This is an ESLint issue, something being wrong in https://github.com/thibautsabot/next.js/blob/canary/packages/eslint-plugin-next/lib/rules/no-server-import-in-page.js

@thibautsabot any ideas?

It will deploy on vercel, but won't be executed

That's already reported and in the backlog, so that's a duplicate of #31803.

@balazsorban44 balazsorban44 added Linting Related to `next lint` or ESLint with Next.js. good first issue Easy to fix issues, good for newcomers kind: bug and removed bug Issue was opened via the bug report template. labels Dec 14, 2021
@awareness481
Copy link
Contributor

awareness481 commented Dec 14, 2021

Given that has been a common error with eslint rules, I'm proposing a common utility that uses path.basename(page, path.extname) to avoid errors with the path or with extensions being either .tsx or .js (or even .jsx).

This could also require doing an additional check with path.win32 (https://nodejs.org/api/path.html#windows-vs-posix)

I think it would be a good idea to switch any code checking for a filename to the above. There are multiple rules that check for _document for example.

Edit: With a common utility it would also be easier for future contributors.

@thibautsabot
Copy link
Contributor

thibautsabot commented Dec 14, 2021

Hey!

Really sorry about the hiccup 🙏 .

I'm unable to reproduce it with next and eslint-config-next on 12.0.8-canary.5 though 😕 .
Did you update both packages?

@balazsorban44
Copy link
Member

@thibautsabot I forgot to check if I upgraded the ESLint plugin as well. 🤦‍♂️. Once I get home I'll try again.. 👀

@balazsorban44
Copy link
Member

@thibautsabot is totally right.

This has been fixed in eslint-config-next@12.0.8-canary.0. Make sure you have that or a newer version!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers Linting Related to `next lint` or ESLint with Next.js.
Projects
None yet
Development

No branches or pull requests

4 participants