|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import { callServer as hostCallServer } from '@modern-js/runtime/rsc/client'; |
3 | 4 | import { useState } from 'react'; |
| 5 | +import { |
| 6 | + createFromFetch, |
| 7 | + createTemporaryReferenceSet, |
| 8 | + encodeReply, |
| 9 | + setServerCallback, |
| 10 | +} from 'react-server-dom-rspack/client.browser'; |
4 | 11 | import RemoteClientBadge from 'rscRemote/RemoteClientBadge'; |
5 | 12 | import { RemoteClientCounter as RemoteClientCounterBridge } from 'rscRemote/RemoteClientCounter'; |
6 | | -import { remoteActionEcho } from 'rscRemote/actions'; |
| 13 | +import { incrementRemoteCount, remoteActionEcho } from 'rscRemote/actions'; |
7 | 14 | import defaultRemoteAction from 'rscRemote/defaultAction'; |
8 | | -import { registerRemoteServerCallback } from 'rscRemote/registerServerCallback'; |
| 15 | +import { nestedRemoteAction } from 'rscRemote/nestedActions'; |
| 16 | + |
| 17 | +let hasConfiguredRemoteActionRouting = false; |
| 18 | + |
| 19 | +function configureRemoteActionRouting() { |
| 20 | + if (hasConfiguredRemoteActionRouting) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const remoteActionIds = new Set( |
| 25 | + [ |
| 26 | + defaultRemoteAction, |
| 27 | + incrementRemoteCount, |
| 28 | + nestedRemoteAction, |
| 29 | + remoteActionEcho, |
| 30 | + ] |
| 31 | + .map(action => (action as any)?.$$id) |
| 32 | + .filter((id): id is string => typeof id === 'string'), |
| 33 | + ); |
| 34 | + if (remoteActionIds.size === 0) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const remoteActionUrl = `${__RSC_MF_REMOTE_ORIGIN__}/`; |
| 39 | + setServerCallback(async (id, args) => { |
| 40 | + if (!remoteActionIds.has(id)) { |
| 41 | + return hostCallServer(id, args); |
| 42 | + } |
| 43 | + const temporaryReferences = createTemporaryReferenceSet(); |
| 44 | + const response = fetch(remoteActionUrl, { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + Accept: 'text/x-component', |
| 48 | + 'x-rsc-action': id, |
| 49 | + }, |
| 50 | + body: await encodeReply(args, { temporaryReferences }), |
| 51 | + }); |
| 52 | + return createFromFetch(response, { temporaryReferences }); |
| 53 | + }); |
| 54 | + |
| 55 | + hasConfiguredRemoteActionRouting = true; |
| 56 | +} |
9 | 57 |
|
10 | 58 | export default function HostRemoteActionRunner() { |
11 | 59 | // Keep this import in the client graph so federated RSC bridge IDs |
12 | 60 | // can map back to a concrete remote module factory at runtime. |
13 | 61 | void RemoteClientCounterBridge; |
14 | | - registerRemoteServerCallback(__RSC_MF_REMOTE_ORIGIN__); |
| 62 | + configureRemoteActionRouting(); |
15 | 63 | const [defaultResult, setDefaultResult] = useState(''); |
16 | 64 | const [echoResult, setEchoResult] = useState(''); |
17 | 65 | const [isPending, setIsPending] = useState(false); |
|
0 commit comments