Skip to content

Commit

Permalink
chore(rsc): Refactor: Extract rscFetch and rsaFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Sep 18, 2024
1 parent 4972f4a commit cf28fc7
Showing 1 changed file with 41 additions and 41 deletions.
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

0 comments on commit cf28fc7

Please sign in to comment.