Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rsc): Refactor: Extract rscFetch and rsaFetch #11582

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions packages/router/src/rsc/RscRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ function onStreamFinished(
)
}

async function rsaFetch(
serializedLocation: string,
rsaId: string,
rsaArgs: unknown[],
) {
const rscId = '_'

const url =
BASE_PATH + rscId + '?action_id=' + rsaId + '&' + serializedLocation

console.log('rsaFetch url', url)

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

try {
body = await encodeReply(rsaArgs)
} catch (e) {
console.error('Error encoding Server Action arguments', e)
}

return fetch(url, {
method: 'POST',
body,
headers: { 'rw-rsc': '1' },
})
}

function rscFetch(serializedLocation: string) {
const rscId = '__rwjs__Routes'

return fetch(BASE_PATH + rscId + '?' + serializedLocation, {
headers: { 'rw-rsc': '1' },
})
}

type SerializedLocation =
| `__rwjs__pathname=${string}&__rwjs__search=${string}`
| `__rwjs__pathname=${string}&__rwjs__search=${string}::${string}`
Expand All @@ -47,22 +82,11 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
console.log('rscFetchRoutes :: cache miss for', rscCacheKey)
}

const rscId = '__rwjs__Routes'

// TODO (RSC): During SSR we should not fetch (Is this function really
// called during SSR?)
const responsePromise = fetch(BASE_PATH + rscId + '?' + serializedLocation, {
headers: {
'rw-rsc': '1',
},
})

const options: Options<unknown[], RscModel> = {
// React will hold on to `callServer` and use that when it detects a
// server action is invoked (like `action={onSubmit}` in a <form>
// element). So for now at least we need to send it with every RSC
// request, so React knows what `callServer` method to use for server
// actions inside the RSC.
// React will hold on to `callServer` and use that when it detects a server
// action is invoked (like `action={onSubmit}` in a <form> element). So for
// now at least we need to send it with every RSC request, so React knows
// what `callServer` method to use for server actions inside the RSC.
// TODO (RSC): Need to figure out the types for callServer
// @ts-expect-error types
callServer: async function (rsaId: string, args: unknown[]) {
Expand All @@ -76,28 +100,7 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
// same arguments
const rscCacheKey: SerializedLocation = `${serializedLocation}::${rsaId}::${new Date()}`

const searchParams = new URLSearchParams()
searchParams.set('action_id', rsaId)
const rscId = '_'

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

try {
body = await encodeReply(args)
} catch (e) {
console.error('Error encoding Server Action arguments', e)
}

const responsePromise = fetch(
BASE_PATH + rscId + '?' + searchParams + '&' + serializedLocation,
{
method: 'POST',
body,
headers: {
'rw-rsc': '1',
},
},
)
const responsePromise = rsaFetch(serializedLocation, rsaId, args)

onStreamFinished(responsePromise, () => {
updateCurrentRscCacheKey(rscCacheKey)
Expand All @@ -116,10 +119,7 @@ function rscFetchRoutes(serializedLocation: SerializedLocation) {
},
}

const modelPromise = createFromFetch<never, RscModel>(
responsePromise,
options,
)
const modelPromise = createFromFetch(rscFetch(serializedLocation), options)

rscCache.set(rscCacheKey, modelPromise)

Expand Down
Loading