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 is incompatible with `server-only #55206

Closed
1 task done
barryengineerapart opened this issue Sep 10, 2023 · 2 comments
Closed
1 task done

Middleware is incompatible with `server-only #55206

barryengineerapart opened this issue Sep 10, 2023 · 2 comments
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@barryengineerapart
Copy link

barryengineerapart commented Sep 10, 2023

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/vigorous-ace-sx277k?file=/middleware.ts:1,1

To Reproduce

If I create the following middlerware.ts with contents:

import "server-only";
export function middleware() {}

I get the following error:

Server Error
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
node_modules/server-only/index.js (1:0)
eval
node_modules/server-only/index.js (1:6)
(middleware)/./node_modules/server-only/index.js
file:///Users/adriang/code/mid-test/.next/server/middleware.js (435:1)
__webpack_require__
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (268:21)
eval
webpack-internal:///(middleware)/./middleware.ts (5:69)
(middleware)/./middleware.ts
file:///Users/adriang/code/mid-test/.next/server/middleware.js (29:1)
__webpack_require__
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (268:21)
eval
webpack-internal:///(middleware)/./node_modules/next/dist/build/webpack/loaders/next-middleware-loader.js (7:72)
(middleware)/./node_modules/next/dist/build/webpack/loaders/next-middleware-loader.js?absolutePagePath=%2FUsers%2Fadriang%2Fcode%2Fmid-test%2Fmiddleware.ts&page=%2Fmiddleware&rootDir=%2FUsers%2Fadriang%2Fcode%2Fmid-test&matchers=&preferredRegion=&middlewareConfig=e30%3D!
file:///Users/adriang/code/mid-test/.next/server/middleware.js (18:1)

Current vs. Expected behavior

The expectation is that this would not throw the above exception since I am under the impression that middleware would only run on the server.

It doesn't make sense to run them client-side, from docs:

Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:21:53 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6020
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.12
      eslint-config-next: 13.4.12
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

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

Middleware / Edge (API routes, runtime)

Additional context

No response

@barryengineerapart barryengineerapart added the bug Issue was opened via the bug report template. label Sep 10, 2023
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Sep 10, 2023
@balazsorban44
Copy link
Member

Duplicate of #54549

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants