Skip to content

Commit 165b5d5

Browse files
authored
chore(rsc): use websocket for browser module runner transport (#946)
1 parent 081ee7c commit 165b5d5

File tree

7 files changed

+33
-50
lines changed

7 files changed

+33
-50
lines changed

packages/plugin-rsc/examples/browser-mode/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/react-dom": "^19.2.2",
1919
"@vitejs/plugin-react": "latest",
2020
"@vitejs/plugin-rsc": "latest",
21-
"vite": "^7.1.10"
21+
"vite": "^7.1.10",
22+
"vite-dev-rpc": "^1.1.0"
2223
}
2324
}

packages/plugin-rsc/examples/browser-mode/src/framework/load-client-dev.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { ESModulesEvaluator, ModuleRunner } from 'vite/module-runner'
2+
import { createRPCClient } from 'vite-dev-rpc'
3+
4+
const rpcClient = createRPCClient<{ invoke: Function }, {}>(
5+
'transport-proxy',
6+
import.meta.hot!,
7+
)
28

39
export default async function loadClient() {
410
const runner = new ModuleRunner(
511
{
612
sourcemapInterceptor: false,
713
transport: {
8-
invoke: async (payload) => {
9-
const response = await fetch(
10-
'/@vite/invoke-react-client?' +
11-
new URLSearchParams({
12-
data: JSON.stringify(payload),
13-
}),
14-
)
15-
return response.json()
16-
},
14+
invoke: (payload) => rpcClient.invoke(payload),
1715
},
1816
hmr: false,
1917
},

packages/plugin-rsc/examples/browser-mode/vite.config.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getPluginApi,
55
type PluginApi,
66
} from '@vitejs/plugin-rsc/plugin'
7+
import { createRPCServer } from 'vite-dev-rpc'
78

89
export default defineConfig({
910
plugins: [
@@ -129,19 +130,9 @@ function rscBrowserModePlugin(): Plugin[] {
129130
manager = getPluginApi(config)!.manager
130131
},
131132
configureServer(server) {
132-
server.middlewares.use(async (req, res, next) => {
133-
const url = new URL(req.url ?? '/', 'https://any.local')
134-
if (url.pathname === '/@vite/invoke-react-client') {
135-
const payload = JSON.parse(url.searchParams.get('data')!)
136-
const result =
137-
await server.environments['react_client']!.hot.handleInvoke(
138-
payload,
139-
)
140-
res.setHeader('Content-Type', 'application/json')
141-
res.end(JSON.stringify(result))
142-
return
143-
}
144-
next()
133+
createRPCServer('transport-proxy', server.ws, {
134+
invoke: (payload: any) =>
135+
server.environments.react_client.hot.handleInvoke(payload),
145136
})
146137
},
147138
hotUpdate(ctx) {

packages/plugin-rsc/examples/browser/lib/dev-proxy.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { ESModulesEvaluator, ModuleRunner } from 'vite/module-runner'
2+
import { createRPCClient } from 'vite-dev-rpc'
3+
4+
const rpcClient = createRPCClient<{ invoke: Function }, {}>(
5+
'rsc:transport-proxy',
6+
import.meta.hot!,
7+
)
28

39
const runner = new ModuleRunner(
410
{
511
sourcemapInterceptor: false,
612
transport: {
7-
invoke: async (payload) => {
8-
const response = await fetch(
9-
'/@vite/invoke-rsc?' +
10-
new URLSearchParams({
11-
data: JSON.stringify(payload),
12-
}),
13-
)
14-
return response.json()
15-
},
13+
invoke: (payload) => rpcClient.invoke(payload),
1614
},
1715
hmr: false,
1816
},

packages/plugin-rsc/examples/browser/lib/plugin.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { rmSync } from 'node:fs'
22
import path from 'node:path'
33
import { normalizePath, type Plugin } from 'vite'
4+
import { createRPCServer } from 'vite-dev-rpc'
45

56
export default function vitePluginRscBrowser(): Plugin[] {
67
return [
@@ -51,17 +52,9 @@ export default function vitePluginRscBrowser(): Plugin[] {
5152
},
5253
},
5354
configureServer(server) {
54-
server.middlewares.use(async (req, res, next) => {
55-
const url = new URL(req.url ?? '/', 'https://any.local')
56-
if (url.pathname === '/@vite/invoke-rsc') {
57-
const payload = JSON.parse(url.searchParams.get('data')!)
58-
const result =
59-
await server.environments['rsc']!.hot.handleInvoke(payload)
60-
res.setHeader('Content-Type', 'application/json')
61-
res.end(JSON.stringify(result))
62-
return
63-
}
64-
next()
55+
createRPCServer('rsc:transport-proxy', server.ws, {
56+
invoke: (payload: any) =>
57+
server.environments.rsc.hot.handleInvoke(payload),
6558
})
6659
},
6760
},

packages/plugin-rsc/examples/browser/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/react-dom": "^19.2.2",
1919
"@vitejs/plugin-react": "latest",
2020
"@vitejs/plugin-rsc": "latest",
21-
"vite": "^7.1.10"
21+
"vite": "^7.1.10",
22+
"vite-dev-rpc": "^1.1.0"
2223
}
2324
}

pnpm-lock.yaml

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)