Skip to content

Commit b9cc9bc

Browse files
antfuclaude
andcommitted
refactor: replace window.location.reload with requestTrustWithToken
Add `requestTrustWithToken(token)` to DevToolsRpcClient interface that re-requests trust over the existing WS connection with a new token, avoiding page reload that would lose user app state. - BroadcastChannel auth-update handler now calls requestTrustWithToken - ViewBuiltinClientAuthNotice submit calls context.rpc.requestTrustWithToken - Token is persisted to localStorage/globalThis for future reconnections - Static RPC mode provides no-op implementation (always trusted) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent eeb9483 commit b9cc9bc

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

packages/core/src/client/webcomponents/components/views-builtin/ViewBuiltinClientAuthNotice.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { DocksContext } from '@vitejs/devtools-kit/client'
33
import { ref } from 'vue'
44
import VitePlus from '../icons/VitePlus.vue'
55
6-
defineProps<{
6+
const props = defineProps<{
77
context: DocksContext
88
}>()
99
@@ -13,8 +13,7 @@ function submitToken() {
1313
const value = tokenInput.value.trim()
1414
if (!value)
1515
return
16-
localStorage.setItem('__VITE_DEVTOOLS_CONNECTION_AUTH_TOKEN__', value)
17-
window.location.reload()
16+
props.context.rpc.requestTrustWithToken(value)
1817
}
1918
</script>
2019

packages/kit/src/client/rpc-static.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export async function createStaticRpcClientMode(
1515
return {
1616
isTrusted: true,
1717
requestTrust: async () => true,
18+
requestTrustWithToken: async () => true,
1819
ensureTrusted: async () => true,
1920
call: (...args: any): any => staticCaller.call(
2021
args[0] as string,

packages/kit/src/client/rpc-ws.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ export function createWsRpcClientMode(
6161
},
6262
})
6363

64-
async function requestTrust() {
65-
if (isTrusted)
66-
return true
64+
let currentAuthToken = authToken
65+
66+
async function requestTrustWithToken(token: string) {
67+
currentAuthToken = token
6768

6869
const info = parseUA(navigator.userAgent)
6970
const ua = [
@@ -76,7 +77,7 @@ export function createWsRpcClientMode(
7677
].filter(i => i).join(' ')
7778

7879
const result = await serverRpc.$call('vite:anonymous:auth', {
79-
authToken,
80+
authToken: token,
8081
ua,
8182
origin: location.origin,
8283
})
@@ -87,6 +88,12 @@ export function createWsRpcClientMode(
8788
return result.isTrusted
8889
}
8990

91+
async function requestTrust() {
92+
if (isTrusted)
93+
return true
94+
return requestTrustWithToken(currentAuthToken)
95+
}
96+
9097
async function ensureTrusted(timeout = 60_000): Promise<boolean> {
9198
if (isTrusted)
9299
trustedPromise.resolve(true)
@@ -113,6 +120,7 @@ export function createWsRpcClientMode(
113120
return isTrusted
114121
},
115122
requestTrust,
123+
requestTrustWithToken,
116124
ensureTrusted,
117125
call: (...args: any): any => {
118126
return serverRpc.$call(

packages/kit/src/client/rpc.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export interface DevToolsRpcClient {
5959
*/
6060
requestTrust: () => Promise<boolean>
6161

62+
/**
63+
* Request trust from the server using a specific auth token.
64+
* Updates the stored token and re-requests trust without reloading the page.
65+
*/
66+
requestTrustWithToken: (token: string) => Promise<boolean>
67+
6268
/**
6369
* Call a RPC function on the server
6470
*/
@@ -86,6 +92,7 @@ export interface DevToolsRpcClientMode {
8692
readonly isTrusted: boolean
8793
ensureTrusted: DevToolsRpcClient['ensureTrusted']
8894
requestTrust: DevToolsRpcClient['requestTrust']
95+
requestTrustWithToken: DevToolsRpcClient['requestTrustWithToken']
8996
call: DevToolsRpcClient['call']
9097
callEvent: DevToolsRpcClient['callEvent']
9198
callOptional: DevToolsRpcClient['callOptional']
@@ -234,6 +241,12 @@ export async function getDevToolsRpcClient(
234241
connectionMeta,
235242
ensureTrusted: mode.ensureTrusted,
236243
requestTrust: mode.requestTrust,
244+
requestTrustWithToken: async (token: string) => {
245+
// Update stored token for future reconnections
246+
localStorage.setItem(CONNECTION_AUTH_TOKEN_KEY, token)
247+
;(globalThis as any)[CONNECTION_AUTH_TOKEN_KEY] = token
248+
return mode.requestTrustWithToken(token)
249+
},
237250
call: mode.call,
238251
callEvent: mode.callEvent,
239252
callOptional: mode.callOptional,
@@ -252,8 +265,7 @@ export async function getDevToolsRpcClient(
252265
const bc = new BroadcastChannel('vite-devtools-auth')
253266
bc.onmessage = (event) => {
254267
if (event.data?.type === 'auth-update' && event.data.authToken) {
255-
localStorage.setItem(CONNECTION_AUTH_TOKEN_KEY, event.data.authToken)
256-
window.location.reload()
268+
rpc.requestTrustWithToken(event.data.authToken)
257269
}
258270
}
259271
}

0 commit comments

Comments
 (0)