Skip to content

Commit

Permalink
RSC: Check for rw-rsc header (#9410)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Nov 12, 2023
1 parent 114a2b0 commit cb0b035
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 0 additions & 1 deletion .github/actions/actionsLib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,3 @@ export async function setUpRscTestProject(
await execInProject(`node ${rwBinPath} build -v`)
console.log()
}

12 changes: 10 additions & 2 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const checkStatus = async (
return response
}

export function serve<Props>(rscId: string, basePath = '/RSC/') {
export function serve<Props>(rscId: string, basePath = '/rw-rsc/') {
type SetRerender = (
rerender: (next: [ReactElement, string]) => void
) => () => void
Expand Down Expand Up @@ -54,6 +54,9 @@ export function serve<Props>(rscId: string, basePath = '/RSC/') {
const response = fetch(basePath + id + '/' + searchParams, {
method: 'POST',
body: await encodeReply(args),
headers: {
'rw-rsc': '1',
},
})

const data = createFromFetch(response, options)
Expand All @@ -76,7 +79,12 @@ export function serve<Props>(rscId: string, basePath = '/RSC/') {
)

const response =
prefetched || fetch(basePath + rscId + '/' + searchParams)
prefetched ||
fetch(basePath + rscId + '/' + searchParams, {
headers: {
'rw-rsc': '1',
},
})
const data = createFromFetch(checkStatus(response), options)
console.log('fetchRSC after createFromFetch. data:', data)

Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/rsc/rscRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import { renderRsc } from './rscWorkerCommunication'
const { decodeReply, decodeReplyFromBusboy } = RSDWServer

export function createRscRequestHandler() {
// This is mounted at /RSC, so will have /RSC stripped from req.url
return async (req: Request, res: Response) => {
const basePath = '/RSC/'
// This is mounted at /rw-rsc, so will have /rw-rsc stripped from req.url
return async (req: Request, res: Response, next: () => void) => {
const basePath = '/rw-rsc/'
console.log('basePath', basePath)
console.log('req.originalUrl', req.originalUrl, 'req.url', req.url)
console.log('req.headers.host', req.headers.host)
console.log("req.headers['rw-rsc']", req.headers['rw-rsc'])

// https://www.rfc-editor.org/rfc/rfc6648
// SHOULD NOT prefix their parameter names with "X-" or similar constructs.
if (req.headers['rw-rsc'] !== '1') {
return next()
}

const url = new URL(req.originalUrl || '', 'http://' + req.headers.host)
let rscId: string | undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/runRscFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export async function runFeServer() {
})
)

// Mounting middleware at /RSC will strip /RSC from req.url
app.use('/RSC', createRscRequestHandler())
// Mounting middleware at /rw-rsc will strip /rw-rsc from req.url
app.use('/rw-rsc', createRscRequestHandler())

app.use(express.static(rwPaths.web.dist))

Expand Down

0 comments on commit cb0b035

Please sign in to comment.