@@ -53,9 +53,11 @@ function rscFetchRoutes(serializedProps: string) {
53
53
const searchParams = new URLSearchParams ( )
54
54
searchParams . set ( 'props' , serializedProps )
55
55
56
+ const rscId = '__rwjs__Routes'
57
+
56
58
// TODO (RSC): During SSR we should not fetch (Is this function really
57
59
// called during SSR?)
58
- const responsePromise = fetch ( BASE_PATH + '__rwjs__Routes ?' + searchParams , {
60
+ const responsePromise = fetch ( BASE_PATH + rscId + ' ?' + searchParams , {
59
61
headers : {
60
62
'rw-rsc' : '1' ,
61
63
} ,
@@ -70,21 +72,18 @@ function rscFetchRoutes(serializedProps: string) {
70
72
callServer : async function ( rsaId : string , args : unknown [ ] ) {
71
73
// `args` is often going to be an array with just a single element,
72
74
// and that element will be FormData
73
- console . log ( 'RscFetcher :: callServer rsaId' , rsaId , 'args' , args )
74
-
75
- // Including rsaId here to make sure the page rerenders when calling RSAs
76
- // Calling a RSA doesn't change the url (i.e. `serializedProps`), and it
77
- // also doesn't change the rscId, so React would not detect a state change
78
- // that would trigger a rerender. So we include the rsaId here to make
79
- // a new cache key that will trigger a rerender.
80
- // TODO (RSC): What happens if you call the same RSA twice in a row?
81
- // Like `increment()`
75
+ console . log ( 'RscRoutes :: callServer rsaId' , rsaId , 'args' , args )
76
+
77
+ // Including rsaId here for debugging reasons only, what's important is
78
+ // `new Date()`, to make sure the cache key is unique so we trigger a
79
+ // rerender. It's needed to handle calling RSAs multiple times with the
80
+ // same arguments
82
81
const rscCacheKey = `${ serializedProps } ::${ rsaId } ::${ new Date ( ) } `
83
82
84
83
const searchParams = new URLSearchParams ( )
85
84
searchParams . set ( 'action_id' , rsaId )
86
85
searchParams . set ( 'props' , serializedProps )
87
- const id = '_'
86
+ const rscId = '_'
88
87
89
88
let body : Awaited < ReturnType < typeof encodeReply > > = ''
90
89
@@ -94,7 +93,7 @@ function rscFetchRoutes(serializedProps: string) {
94
93
console . error ( 'Error encoding Server Action arguments' , e )
95
94
}
96
95
97
- const responsePromise = fetch ( BASE_PATH + id + '?' + searchParams , {
96
+ const responsePromise = fetch ( BASE_PATH + rscId + '?' + searchParams , {
98
97
method : 'POST' ,
99
98
body,
100
99
headers : {
@@ -118,7 +117,7 @@ function rscFetchRoutes(serializedProps: string) {
118
117
// rscCache.set(rscCacheKey, dataPromise)
119
118
120
119
const dataValue = await dataPromise
121
- console . log ( 'RscFetcher :: callServer dataValue' , dataValue )
120
+ console . log ( 'RscRoutes :: callServer dataValue' , dataValue )
122
121
// TODO (RSC): Fix the types for `createFromFetch`
123
122
// @ts -expect-error The type is wrong for createFromFetch
124
123
const Routes = dataValue . Routes ?. [ 0 ]
@@ -147,36 +146,36 @@ function rscFetchRoutes(serializedProps: string) {
147
146
}
148
147
149
148
interface Props {
150
- rscProps : RscProps
149
+ routesProps : RscProps
151
150
}
152
151
153
- export const RscFetcher = ( { rscProps } : Props ) => {
154
- const serializedProps = JSON . stringify ( rscProps )
152
+ export const RscRoutes = ( { routesProps } : Props ) => {
153
+ const serializedProps = JSON . stringify ( routesProps )
155
154
const [ currentRscCacheKey , setCurrentRscCacheKey ] = useState ( ( ) => {
156
- console . log ( 'RscFetcher :: useState initial value' )
155
+ console . log ( 'RscRoutes :: useState initial value' )
157
156
// Calling rscFetchRoutes here to prime the cache
158
157
rscFetchRoutes ( serializedProps )
159
158
return serializedProps
160
159
} )
161
160
162
161
useEffect ( ( ) => {
163
- console . log ( 'RscFetcher :: useEffect set updateCurrentRscCacheKey' )
162
+ console . log ( 'RscRoutes :: useEffect set updateCurrentRscCacheKey' )
164
163
updateCurrentRscCacheKey = ( key : string ) => {
165
- console . log ( 'RscFetcher inside updateCurrentRscCacheKey' , key )
164
+ console . log ( 'RscRoutes inside updateCurrentRscCacheKey' , key )
166
165
167
166
setCurrentRscCacheKey ( key )
168
167
}
169
168
} , [ ] )
170
169
171
170
useEffect ( ( ) => {
172
- console . log ( 'RscFetcher :: useEffect about to call rscFetchRoutes' )
171
+ console . log ( 'RscRoutes :: useEffect about to call rscFetchRoutes' )
173
172
// rscFetchRoutes will update rscCache with the fetched component
174
173
rscFetchRoutes ( serializedProps )
175
174
setCurrentRscCacheKey ( serializedProps )
176
175
} , [ serializedProps ] )
177
176
178
- console . log ( 'RscFetcher :: current props\n rscProps : ' + serializedProps )
179
- console . log ( 'RscFetcher :: rendering cache entry for\n' + currentRscCacheKey )
177
+ console . log ( 'RscRoutes :: current props\n routesProps : ' + serializedProps )
178
+ console . log ( 'RscRoutes :: rendering cache entry for\n' + currentRscCacheKey )
180
179
181
180
const component = rscCache . get ( currentRscCacheKey )
182
181
0 commit comments