Skip to content

Commit c99ea1f

Browse files
committed
Implemented rsc fetch
Had to change method from post to put because vite interprets post as an action
1 parent 33a8401 commit c99ea1f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

packages/plugin-rsc/examples/navigation/src/client.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ async function hydrate() {
2222
}
2323
ReactDomClient.hydrateRoot(document, <Shell />);
2424
}
25-
async function fetchRSC(url: string, options: any) {
26-
const payload = await createFromFetch(fetch(url));
25+
async function fetchRSC(url: string, {body, ...options}: any) {
26+
const payload = await createFromFetch(fetch(url, {...options, body: JSON.stringify(body), method: 'PUT'}));
2727
return payload.root;
2828
}
2929
hydrate();

packages/plugin-rsc/examples/navigation/src/server.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@ import { StateNavigator } from 'navigation'
33
import stateNavigator from './stateNavigator.ts'
44

55
export default async function handler(request: Request): Promise<Response> {
6-
const url = new URL(request.url)
7-
const { NavigationHandler } = await import('navigation-react')
8-
const serverNavigator = new StateNavigator(stateNavigator)
9-
serverNavigator.navigateLink(`${url.pathname}${url.search}`)
10-
const { App } = await import('./App.tsx')
6+
let url: string;
7+
let view: any;
8+
if (request.method === 'PUT') {
9+
const sceneViews: any = {
10+
people: await import('./People.tsx'),
11+
person: await import('./Person.tsx'),
12+
friends: await import('./Friends.tsx')
13+
};
14+
const {url: reqUrl, sceneViewKey} = await request.json();
15+
url = reqUrl;
16+
const SceneView = sceneViews[sceneViewKey].default;
17+
view = <SceneView />;
18+
} else {
19+
let reqUrl = new URL(request.url);
20+
url = `${reqUrl.pathname}${reqUrl.search}`;
21+
const { App } = await import('./App.tsx');
22+
view = <App url={url} />;
23+
}
24+
const { NavigationHandler } = await import('navigation-react');
25+
const serverNavigator = new StateNavigator(stateNavigator);
26+
serverNavigator.navigateLink(url)
1127
const root = (
1228
<>
1329
<NavigationHandler stateNavigator={serverNavigator}>
14-
<App url={`${url.pathname}${url.search}`} />
30+
{view}
1531
</NavigationHandler>
1632
</>
17-
)
33+
);
1834
// @ts-ignore
19-
const nonce = !process.env.NO_CSP ? crypto.randomUUID() : undefined
20-
const response = await renderRequest(request, root, { nonce })
35+
const nonce = !process.env.NO_CSP ? crypto.randomUUID() : undefined;
36+
const response = await renderRequest(request, root, { nonce });
2137
if (nonce) {
2238
response.headers.set(
2339
'content-security-policy',
@@ -27,11 +43,11 @@ export default async function handler(request: Request): Promise<Response> {
2743
import.meta.env.DEV ? `'unsafe-eval'` : ``
2844
} ; ` +
2945
`style-src 'self' 'nonce-${nonce}'; `,
30-
)
46+
);
3147
}
32-
return response
48+
return response;
3349
}
3450

3551
if (import.meta.hot) {
36-
import.meta.hot.accept()
52+
import.meta.hot.accept();
3753
}

0 commit comments

Comments
 (0)