Skip to content

Commit c01809e

Browse files
authored
chore(rsc): Refactor: Rename RscFetcher -> RscRoutes (#11409)
1 parent 188efb6 commit c01809e

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

packages/router/src/rsc/ClientRouter.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { namedRoutes } from '../namedRoutes.js'
77
import { RouterContextProvider } from '../router-context.js'
88
import type { RouterProps } from '../router.js'
99

10-
import { RscFetcher } from './RscFetcher.js'
10+
import { RscRoutes } from './RscRoutes.js'
1111

1212
export const Router = ({ useAuth, paramTypes, children }: RouterProps) => {
1313
return (
@@ -48,8 +48,8 @@ const LocationAwareRouter = ({
4848
// 'No route found for the current URL. Make sure you have a route ' +
4949
// 'defined for the root of your React app.',
5050
// )
51-
const rscProps = { location: { pathname, search } }
52-
return <RscFetcher rscProps={rscProps} />
51+
const routesProps = { location: { pathname, search } }
52+
return <RscRoutes routesProps={routesProps} />
5353
}
5454

5555
const requestedRoute = pathRouteMap[activeRoutePath]
@@ -70,7 +70,7 @@ const LocationAwareRouter = ({
7070
)
7171
}
7272

73-
const rscProps = { location: { pathname, search } }
73+
const routesProps = { location: { pathname, search } }
7474

7575
return (
7676
<RouterContextProvider
@@ -80,18 +80,18 @@ const LocationAwareRouter = ({
8080
activeRouteName={requestedRoute.name}
8181
>
8282
<AuthenticatedRoute unauthenticated={unauthenticated}>
83-
<RscFetcher rscProps={rscProps} />
83+
<RscRoutes routesProps={routesProps} />
8484
</AuthenticatedRoute>
8585
</RouterContextProvider>
8686
)
8787
}
8888

89-
const rscProps = { location: { pathname, search } }
89+
const routesProps = { location: { pathname, search } }
9090
// TODO (RSC): I think that moving between private and public routes
9191
// re-initializes RscFetcher. I wonder if there's an optimization to be made
9292
// here. Maybe we can lift RscFetcher up so we can keep the same instance
9393
// around and reuse it everywhere
94-
return <RscFetcher rscProps={rscProps} />
94+
return <RscRoutes routesProps={routesProps} />
9595
}
9696

9797
export interface RscFetchProps extends Record<string, unknown> {

packages/router/src/rsc/RscFetcher.tsx renamed to packages/router/src/rsc/RscRoutes.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ function rscFetchRoutes(serializedProps: string) {
5353
const searchParams = new URLSearchParams()
5454
searchParams.set('props', serializedProps)
5555

56+
const rscId = '__rwjs__Routes'
57+
5658
// TODO (RSC): During SSR we should not fetch (Is this function really
5759
// called during SSR?)
58-
const responsePromise = fetch(BASE_PATH + '__rwjs__Routes?' + searchParams, {
60+
const responsePromise = fetch(BASE_PATH + rscId + '?' + searchParams, {
5961
headers: {
6062
'rw-rsc': '1',
6163
},
@@ -70,21 +72,18 @@ function rscFetchRoutes(serializedProps: string) {
7072
callServer: async function (rsaId: string, args: unknown[]) {
7173
// `args` is often going to be an array with just a single element,
7274
// 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
8281
const rscCacheKey = `${serializedProps}::${rsaId}::${new Date()}`
8382

8483
const searchParams = new URLSearchParams()
8584
searchParams.set('action_id', rsaId)
8685
searchParams.set('props', serializedProps)
87-
const id = '_'
86+
const rscId = '_'
8887

8988
let body: Awaited<ReturnType<typeof encodeReply>> = ''
9089

@@ -94,7 +93,7 @@ function rscFetchRoutes(serializedProps: string) {
9493
console.error('Error encoding Server Action arguments', e)
9594
}
9695

97-
const responsePromise = fetch(BASE_PATH + id + '?' + searchParams, {
96+
const responsePromise = fetch(BASE_PATH + rscId + '?' + searchParams, {
9897
method: 'POST',
9998
body,
10099
headers: {
@@ -118,7 +117,7 @@ function rscFetchRoutes(serializedProps: string) {
118117
// rscCache.set(rscCacheKey, dataPromise)
119118

120119
const dataValue = await dataPromise
121-
console.log('RscFetcher :: callServer dataValue', dataValue)
120+
console.log('RscRoutes :: callServer dataValue', dataValue)
122121
// TODO (RSC): Fix the types for `createFromFetch`
123122
// @ts-expect-error The type is wrong for createFromFetch
124123
const Routes = dataValue.Routes?.[0]
@@ -147,36 +146,36 @@ function rscFetchRoutes(serializedProps: string) {
147146
}
148147

149148
interface Props {
150-
rscProps: RscProps
149+
routesProps: RscProps
151150
}
152151

153-
export const RscFetcher = ({ rscProps }: Props) => {
154-
const serializedProps = JSON.stringify(rscProps)
152+
export const RscRoutes = ({ routesProps }: Props) => {
153+
const serializedProps = JSON.stringify(routesProps)
155154
const [currentRscCacheKey, setCurrentRscCacheKey] = useState(() => {
156-
console.log('RscFetcher :: useState initial value')
155+
console.log('RscRoutes :: useState initial value')
157156
// Calling rscFetchRoutes here to prime the cache
158157
rscFetchRoutes(serializedProps)
159158
return serializedProps
160159
})
161160

162161
useEffect(() => {
163-
console.log('RscFetcher :: useEffect set updateCurrentRscCacheKey')
162+
console.log('RscRoutes :: useEffect set updateCurrentRscCacheKey')
164163
updateCurrentRscCacheKey = (key: string) => {
165-
console.log('RscFetcher inside updateCurrentRscCacheKey', key)
164+
console.log('RscRoutes inside updateCurrentRscCacheKey', key)
166165

167166
setCurrentRscCacheKey(key)
168167
}
169168
}, [])
170169

171170
useEffect(() => {
172-
console.log('RscFetcher :: useEffect about to call rscFetchRoutes')
171+
console.log('RscRoutes :: useEffect about to call rscFetchRoutes')
173172
// rscFetchRoutes will update rscCache with the fetched component
174173
rscFetchRoutes(serializedProps)
175174
setCurrentRscCacheKey(serializedProps)
176175
}, [serializedProps])
177176

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)
180179

181180
const component = rscCache.get(currentRscCacheKey)
182181

0 commit comments

Comments
 (0)