Skip to content

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Sep 16, 2025

Description

I looked at sveltekit sveltejs/kit#14155, but this.resolve tracking feels more complicated, so I went with a different approach of probing module graph. It employs a separate technique for dev and build, which isn't something I wanted, but for now this should be fine.

This is how new error message looks like:

Before

error during build:
[rsc:validate-imports] 'server-only' cannot be imported in client build
(importer: '/home/hiroshi/code/others/vite-plugin-react/packages/plugin-rsc/examples/e2e/temp/validate-server-only/src/client.tsx', environment: ssr)

After

error during build:
[rsc:validate-imports] 'server-only' cannot be imported in client build ('ssr' environment):
 imported by src/client.tsx
  imported by virtual:vite-rsc/client-references
   imported by ../../../../dist/ssr-8BA2nj0-.js
    imported by ../../../../dist/ssr.js
     imported by src/framework/entry.ssr.tsx

file: /home/hiroshi/code/others/vite-plugin-react/packages/plugin-rsc/examples/e2e/temp/validate-server-only/src/client.tsx

TODO

  • share social

Improved the error message for server-only and client-only imports on Vite RSC plugin.

image

These "only" packages provide errors only during runtime on their own, but I didn't see any reason not to surface as build time error, so this has been included as a "plugin feature". Read the updated documentation to learn more! https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-rsc/README.md#server-only-and-client-only-import

It's good to know Next.js also clarifies this as a "framework feature" and not package's own behavior, which I obviously referenced it when adding the doc https://nextjs.org/docs/app/getting-started/server-and-client-components

@hi-ogawa hi-ogawa marked this pull request as ready for review September 16, 2025 08:55
@hi-ogawa hi-ogawa merged commit ba16c34 into main Sep 17, 2025
22 checks passed
@hi-ogawa hi-ogawa deleted the 09-16-fix_rsc_show_import_chain_for_server-only_and_client-only_import_error branch September 17, 2025 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve server-only and client-only error message
1 participant