Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/plugin-rsc/examples/browser-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"license": "MIT",
"type": "module",
"scripts": {
"dev": "vite",
"dev": "rm -rf ./node_modules/.vite && vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@storybook/nextjs-vite-rsc": "0.0.0-pr-32412-sha-b8362176",
"storybook": "0.0.0-pr-32412-sha-b8362176",
"react": "^19.1.1",
"react-dom": "^19.1.1"
"react-dom": "^19.1.1",
"next": "^15.5.4"
},
"devDependencies": {
"@types/react": "^19.1.16",
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-rsc/examples/browser-mode/src/client.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use client'

import React from 'react'
import { usePathname } from 'next/navigation'

export function ClientCounter() {
const pathname = usePathname()
const [count, setCount] = React.useState(0)

return (
<button onClick={() => setCount((count) => count + 1)}>
Client Counter: {count}
Client Counter: {count} <br />
Injected pathname: {pathname}
</button>
)
}
22 changes: 19 additions & 3 deletions packages/plugin-rsc/examples/browser-mode/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@ import { ClientCounter } from './client.tsx'
import { TestUseActionState } from './action-from-client/client.tsx'
import { TestActionBind } from './action-bind/server.tsx'

// using the client component directly works
import { NextRouter } from '@storybook/nextjs-vite-rsc/rsc/client'
import Link from 'next/link'

export function Root() {
return <App />
return (
<NextRouter url="/some-path-name">
<App />
</NextRouter>
)
}

export function WorkingRoot() {
return (
<NextRouter url="/some-path-name">
<App />
</NextRouter>
)
}

function App() {
return (
<div id="root">
<div>
<a href="https://vite.dev" target="_blank">
<Link href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
</Link>
<a
href="https://react.dev/reference/rsc/server-components"
target="_blank"
Expand Down
25 changes: 25 additions & 0 deletions packages/plugin-rsc/examples/browser-mode/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getPluginApi,
type PluginApi,
} from '@vitejs/plugin-rsc/plugin'
import * as path from 'node:path'
// import inspect from 'vite-plugin-inspect'

export default defineConfig({
Expand Down Expand Up @@ -35,10 +36,26 @@ function rscBrowserModePlugin(): Plugin[] {
config(userConfig, env) {
return {
define: {
'process.env': JSON.stringify({}),
__dirname: JSON.stringify(null),
'import.meta.env.__vite_rsc_build__': JSON.stringify(
env.command === 'build',
),
},
resolve: {
alias: {
// when using the app router, this is the location of next/link
'next/link': 'next/dist/client/app-dir/link',
'@vercel/turbopack-ecmascript-runtime/browser/dev/hmr-client/hmr-client.ts':
'next/dist/client/dev/noop-turbopack-hmr',
'react-server-dom-webpack/client': path.resolve(
'../../dist/vendor/react-server-dom/client.edge.js',
),
'react-server-dom-webpack/client.edge': path.resolve(
'../../dist/vendor/react-server-dom/client.edge.js',
),
},
},
environments: {
client: {
keepProcessEnv: false,
Expand All @@ -52,8 +69,10 @@ function rscBrowserModePlugin(): Plugin[] {
'react-dom/client',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'next/link',
'@vitejs/plugin-rsc/vendor/react-server-dom/server.edge',
'@vitejs/plugin-rsc/vendor/react-server-dom/client.edge',
'@storybook/nextjs-vite-rsc/rsc/client',
],
exclude: ['vite', '@vitejs/plugin-rsc'],
},
Expand All @@ -74,7 +93,10 @@ function rscBrowserModePlugin(): Plugin[] {
'react-dom/client',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'next/link',
'@vitejs/plugin-rsc/vendor/react-server-dom/client.browser',
'@storybook/nextjs-vite-rsc/rsc/client',
'next/navigation',
],
exclude: ['@vitejs/plugin-rsc'],
esbuildOptions: {
Expand Down Expand Up @@ -161,6 +183,9 @@ function rscBrowserModePlugin(): Plugin[] {
manager.isScanBuild = true
reactServer.config.build.write = false
await builder.build(reactServer)
// For some reason I sometimes need to build the reactServer twice before I can build the reactClient.
// Will also make a more dedicated reproduction for that.
await builder.build(reactServer)
manager.isScanBuild = false
reactServer.config.build.write = true
await builder.build(reactClient)
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export function vitePluginRscMinimal(
...vitePluginUseServer(rscPluginOptions, manager),
...vitePluginDefineEncryptionKey(rscPluginOptions),
scanBuildStripPlugin({ manager }),
...cjsModuleRunnerPlugin(),
]
}

Expand Down
Loading
Loading