Skip to content

Commit 0f49c1e

Browse files
authored
chore(rsc): Rename rscFetch to rscFetchRoutes and hardcode the rscId (#11407)
1 parent eaf6221 commit 0f49c1e

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

packages/router/src/rsc/ClientRouter.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const LocationAwareRouter = ({
4949
// 'defined for the root of your React app.',
5050
// )
5151
const rscProps = { location: { pathname, search } }
52-
return <RscFetcher rscId="__rwjs__Routes" rscProps={rscProps} />
52+
return <RscFetcher rscProps={rscProps} />
5353
}
5454

5555
const requestedRoute = pathRouteMap[activeRoutePath]
@@ -80,7 +80,7 @@ const LocationAwareRouter = ({
8080
activeRouteName={requestedRoute.name}
8181
>
8282
<AuthenticatedRoute unauthenticated={unauthenticated}>
83-
<RscFetcher rscId="__rwjs__Routes" rscProps={rscProps} />
83+
<RscFetcher rscProps={rscProps} />
8484
</AuthenticatedRoute>
8585
</RouterContextProvider>
8686
)
@@ -91,7 +91,7 @@ const LocationAwareRouter = ({
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 rscId="__rwjs__Routes" rscProps={rscProps} />
94+
return <RscFetcher rscProps={rscProps} />
9595
}
9696

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

packages/router/src/rsc/RscFetcher.tsx

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,26 @@ function onStreamFinished(
3636
)
3737
}
3838

39-
function rscFetch(rscId: string, serializedProps: string) {
39+
function rscFetchRoutes(serializedProps: string) {
4040
console.log(
41-
'rscFetch :: args:\n rscId: ' +
42-
rscId +
43-
'\n serializedProps: ' +
44-
serializedProps,
41+
'rscFetchRoutes :: args:\n serializedProps: ' + serializedProps,
4542
)
46-
const rscCacheKey = `${rscId}::${serializedProps}`
43+
const rscCacheKey = serializedProps
4744

4845
const cached = rscCache.get(rscCacheKey)
4946
if (cached) {
50-
console.log('rscFetch :: cache hit for', rscCacheKey)
47+
console.log('rscFetchRoutes :: cache hit for', rscCacheKey)
5148
return cached
5249
} else {
53-
console.log('rscFetch :: cache miss for', rscCacheKey)
50+
console.log('rscFetchRoutes :: cache miss for', rscCacheKey)
5451
}
5552

5653
const searchParams = new URLSearchParams()
5754
searchParams.set('props', serializedProps)
5855

5956
// TODO (RSC): During SSR we should not fetch (Is this function really
6057
// called during SSR?)
61-
const responsePromise = fetch(BASE_PATH + rscId + '?' + searchParams, {
58+
const responsePromise = fetch(BASE_PATH + '__rwjs__Routes?' + searchParams, {
6259
headers: {
6360
'rw-rsc': '1',
6461
},
@@ -82,7 +79,7 @@ function rscFetch(rscId: string, serializedProps: string) {
8279
// a new cache key that will trigger a rerender.
8380
// TODO (RSC): What happens if you call the same RSA twice in a row?
8481
// Like `increment()`
85-
const rscCacheKey = `${rscId}::${serializedProps}::${rsaId}::${new Date()}`
82+
const rscCacheKey = `${serializedProps}::${rsaId}::${new Date()}`
8683

8784
const searchParams = new URLSearchParams()
8885
searchParams.set('action_id', rsaId)
@@ -150,17 +147,16 @@ function rscFetch(rscId: string, serializedProps: string) {
150147
}
151148

152149
interface Props {
153-
rscId: string
154150
rscProps: RscProps
155151
}
156152

157-
export const RscFetcher = ({ rscId, rscProps }: Props) => {
153+
export const RscFetcher = ({ rscProps }: Props) => {
158154
const serializedProps = JSON.stringify(rscProps)
159155
const [currentRscCacheKey, setCurrentRscCacheKey] = useState(() => {
160156
console.log('RscFetcher :: useState initial value')
161-
// Calling rscFetch here to prime the cache
162-
rscFetch(rscId, serializedProps)
163-
return `${rscId}::${serializedProps}`
157+
// Calling rscFetchRoutes here to prime the cache
158+
rscFetchRoutes(serializedProps)
159+
return serializedProps
164160
})
165161

166162
useEffect(() => {
@@ -173,19 +169,13 @@ export const RscFetcher = ({ rscId, rscProps }: Props) => {
173169
}, [])
174170

175171
useEffect(() => {
176-
console.log('RscFetcher :: useEffect about to call rscFetch')
177-
// rscFetch will update rscCache with the fetched component
178-
rscFetch(rscId, serializedProps)
179-
setCurrentRscCacheKey(`${rscId}::${serializedProps}`)
180-
}, [rscId, serializedProps])
172+
console.log('RscFetcher :: useEffect about to call rscFetchRoutes')
173+
// rscFetchRoutes will update rscCache with the fetched component
174+
rscFetchRoutes(serializedProps)
175+
setCurrentRscCacheKey(serializedProps)
176+
}, [serializedProps])
181177

182-
console.log(
183-
'RscFetcher :: current props\n' +
184-
' rscId: ' +
185-
rscId +
186-
'\n rscProps: ' +
187-
serializedProps,
188-
)
178+
console.log('RscFetcher :: current props\n rscProps: ' + serializedProps)
189179
console.log('RscFetcher :: rendering cache entry for\n' + currentRscCacheKey)
190180

191181
const component = rscCache.get(currentRscCacheKey)

0 commit comments

Comments
 (0)