cloudflare/vinext

Build failure: `server-only` incorrectly blocked in middleware (SSR environment)

Closed

#1,344 opened on May 20, 2026

 (1 comment) (0 reactions) (0 assignees)TypeScript (371 forks)github user discovery
adapter-api-e2ehelp wanted

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 main vs Next.js v16.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

  1. Reproduce first in vinext's own test suite. Add a test with a middleware file that imports server-only and verify the build succeeds. Confirm it currently fails.

  2. Exclude middleware from the server-only validation. Either configure the RSC plugin to allow server-only in the SSR environment when imported by middleware, or move middleware to a server-appropriate build environment.

  3. Study how Next.js handles middleware module layers. In Next.js, middleware is treated as a server context where server-only is permitted. Search .nextjs-ref/packages/next/src/build/ for how middleware is layered.

Contributor guide