Skip to content

Commit 199e080

Browse files
committed
fix(rsc-mf): route known remote action ids to remote server
1 parent 8fcf3a2 commit 199e080

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

tests/integration/rsc-mf/host/src/server-component-root/HostRemoteActionRunner.tsx

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,65 @@
11
'use client';
22

3+
import { callServer as hostCallServer } from '@modern-js/runtime/rsc/client';
34
import { useState } from 'react';
5+
import {
6+
createFromFetch,
7+
createTemporaryReferenceSet,
8+
encodeReply,
9+
setServerCallback,
10+
} from 'react-server-dom-rspack/client.browser';
411
import RemoteClientBadge from 'rscRemote/RemoteClientBadge';
512
import { RemoteClientCounter as RemoteClientCounterBridge } from 'rscRemote/RemoteClientCounter';
6-
import { remoteActionEcho } from 'rscRemote/actions';
13+
import { incrementRemoteCount, remoteActionEcho } from 'rscRemote/actions';
714
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+
}
957

1058
export default function HostRemoteActionRunner() {
1159
// Keep this import in the client graph so federated RSC bridge IDs
1260
// can map back to a concrete remote module factory at runtime.
1361
void RemoteClientCounterBridge;
14-
registerRemoteServerCallback(__RSC_MF_REMOTE_ORIGIN__);
62+
configureRemoteActionRouting();
1563
const [defaultResult, setDefaultResult] = useState('');
1664
const [echoResult, setEchoResult] = useState('');
1765
const [isPending, setIsPending] = useState(false);

0 commit comments

Comments
 (0)