@@ -29,6 +29,40 @@ function onStreamFinished(
2929 )
3030}
3131
32+ async function rsaFetch (
33+ serializedLocation : string ,
34+ rsaId : string ,
35+ rsaArgs : unknown [ ] ,
36+ ) {
37+ const rscId = '_'
38+ const searchParams = new URLSearchParams ( )
39+ searchParams . set ( 'action_id' , rsaId )
40+
41+ const url = BASE_PATH + rscId + '?' + searchParams + '&' + serializedLocation
42+
43+ let body : Awaited < ReturnType < typeof encodeReply > > = ''
44+
45+ try {
46+ body = await encodeReply ( rsaArgs )
47+ } catch ( e ) {
48+ console . error ( 'Error encoding Server Action arguments' , e )
49+ }
50+
51+ return fetch ( url , {
52+ method : 'POST' ,
53+ body,
54+ headers : { 'rw-rsc' : '1' } ,
55+ } )
56+ }
57+
58+ function rscFetch ( serializedLocation : string ) {
59+ const rscId = '__rwjs__Routes'
60+
61+ return fetch ( BASE_PATH + rscId + '?' + serializedLocation , {
62+ headers : { 'rw-rsc' : '1' } ,
63+ } )
64+ }
65+
3266type SerializedLocation =
3367 | `__rwjs__pathname=${string } &__rwjs__search=${string } `
3468 | `__rwjs__pathname=${string } &__rwjs__search=${string } ::${string } `
@@ -47,22 +81,11 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
4781 console . log ( 'rscFetchRoutes :: cache miss for' , rscCacheKey )
4882 }
4983
50- const rscId = '__rwjs__Routes'
51-
52- // TODO (RSC): During SSR we should not fetch (Is this function really
53- // called during SSR?)
54- const responsePromise = fetch ( BASE_PATH + rscId + '?' + serializedLocation , {
55- headers : {
56- 'rw-rsc' : '1' ,
57- } ,
58- } )
59-
6084 const options : Options < unknown [ ] , RscModel > = {
61- // React will hold on to `callServer` and use that when it detects a
62- // server action is invoked (like `action={onSubmit}` in a <form>
63- // element). So for now at least we need to send it with every RSC
64- // request, so React knows what `callServer` method to use for server
65- // actions inside the RSC.
85+ // React will hold on to `callServer` and use that when it detects a server
86+ // action is invoked (like `action={onSubmit}` in a <form> element). So for
87+ // now at least we need to send it with every RSC request, so React knows
88+ // what `callServer` method to use for server actions inside the RSC.
6689 // TODO (RSC): Need to figure out the types for callServer
6790 // @ts -expect-error types
6891 callServer : async function ( rsaId : string , args : unknown [ ] ) {
@@ -71,33 +94,12 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
7194 console . log ( 'RscRoutes :: callServer rsaId' , rsaId , 'args' , args )
7295
7396 // Including rsaId here for debugging reasons only, what's important is
74- // `new Date()`, to make sure the cache key is unique so we trigger a
97+ // `Date.now ()`, to make sure the cache key is unique so we trigger a
7598 // rerender. It's needed to handle calling RSAs multiple times with the
7699 // same arguments
77- const rscCacheKey : SerializedLocation = `${ serializedLocation } ::${ rsaId } ::${ new Date ( ) } `
78-
79- const searchParams = new URLSearchParams ( )
80- searchParams . set ( 'action_id' , rsaId )
81- const rscId = '_'
82-
83- let body : Awaited < ReturnType < typeof encodeReply > > = ''
84-
85- try {
86- body = await encodeReply ( args )
87- } catch ( e ) {
88- console . error ( 'Error encoding Server Action arguments' , e )
89- }
90-
91- const responsePromise = fetch (
92- BASE_PATH + rscId + '?' + searchParams + '&' + serializedLocation ,
93- {
94- method : 'POST' ,
95- body,
96- headers : {
97- 'rw-rsc' : '1' ,
98- } ,
99- } ,
100- )
100+ const rscCacheKey : SerializedLocation = `${ serializedLocation } ::${ rsaId } ::${ Date . now ( ) } `
101+
102+ const responsePromise = rsaFetch ( serializedLocation , rsaId , args )
101103
102104 onStreamFinished ( responsePromise , ( ) => {
103105 updateCurrentRscCacheKey ( rscCacheKey )
@@ -116,10 +118,7 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
116118 } ,
117119 }
118120
119- const modelPromise = createFromFetch < never , RscModel > (
120- responsePromise ,
121- options ,
122- )
121+ const modelPromise = createFromFetch ( rscFetch ( serializedLocation ) , options )
123122
124123 rscCache . set ( rscCacheKey , modelPromise )
125124
0 commit comments