Build failure: `server-only` incorrectly blocked in middleware (SSR environment)
#1,344 opened on May 20, 2026
Repository metrics
- Stars
- (8,563 stars)
- PR merge metrics
- (Avg merge 1d 1h) (462 merged PRs in 30d)
Description
This issue was created by an agent analysing CI failures from the Next.js Deploy Suite (vinext
mainvs Next.jsv16.2.6, 2026-05-20).
Problem
Middleware runs server-side and should be allowed to import server-only. However, vinext bundles middleware into the SSR environment, and the @vitejs/plugin-rsc rsc:validate-imports plugin flags server-only as invalid in non-RSC environments.
[plugin rsc:validate-imports] middleware.js
Error: 'server-only' cannot be imported in client build ('ssr' environment):
imported by middleware.js
imported by virtual:vinext-server-entry
1 build failure cascades to 13 test failures because the entire test app fails to build.
Estimated Impact
~13 test failures (from 1 build failure).
Affected Test Suites
test/e2e/module-layer/module-layer.test.ts(13 failures)
Recommendation
-
Reproduce first in vinext's own test suite. Add a test with a middleware file that imports
server-onlyand verify the build succeeds. Confirm it currently fails. -
Exclude middleware from the
server-onlyvalidation. Either configure the RSC plugin to allowserver-onlyin the SSR environment when imported by middleware, or move middleware to a server-appropriate build environment. -
Study how Next.js handles middleware module layers. In Next.js, middleware is treated as a server context where
server-onlyis permitted. Search.nextjs-ref/packages/next/src/build/for how middleware is layered.