Skip to content

Commit

Permalink
chore(rsc): Random changes, mostly to comments etc, in prep for remov…
Browse files Browse the repository at this point in the history
…ing the worker (#11451)
  • Loading branch information
Tobbe authored and Josh-Walker-GM committed Sep 6, 2024
1 parent 061ded0 commit 11f30ab
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/router/src/rsc/ServerRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface RouterProps
export const Router: React.FC<RouterProps> = ({ paramTypes, children }) => {
const location = getLocation()

console.log('ServerRouter.tsx location', location)

const analyzedRoutes = analyzeRoutes(children, {
currentPathName: location.pathname,
// @TODO We haven't handled this with SSR/Streaming yet.
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type ClientManifest = {
type ReactClientValue = any

declare module 'react-server-dom-webpack/server.edge' {
import type { ReadableStream } from 'node:stream/web'

type Options = {
environmentName?: string
identifierPrefix?: string
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/rsc/rscRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ import { getFullUrlForFlightRequest } from '../utils.js'
import { sendRscFlightToStudio } from './rscStudioHandlers.js'
import { renderRsc } from './rscWorkerCommunication.js'

const BASE_PATH = '/rw-rsc/'

interface CreateRscRequestHandlerOptions {
getMiddlewareRouter: () => Promise<Router.Instance<any>>
viteDevServer?: ViteDevServer
}

const BASE_PATH = '/rw-rsc/'

export function createRscRequestHandler(
options: CreateRscRequestHandlerOptions,
) {
// This is mounted at /rw-rsc, so will have /rw-rsc stripped from req.url

// TODO (RSC): Switch from Express to Web compatible Request and Response
return async (
req: ExpressRequest,
res: ExpressResponse,
Expand Down Expand Up @@ -165,6 +166,8 @@ export function createRscRequestHandler(

if (rscId || rsaId) {
const handleError = (err: unknown) => {
console.log('handleError() err', err)

if (hasStatusCode(err)) {
res.statusCode = err.statusCode
} else {
Expand Down Expand Up @@ -205,6 +208,7 @@ export function createRscRequestHandler(
},
})

// TODO (RSC): Can we reuse `pipeable` here somehow?
await sendRscFlightToStudio({
rscId,
props,
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/rsc/rscWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// like `useState` and `createContext`.
import { Buffer } from 'node:buffer'
import path from 'node:path'
import type { ReadableStream } from 'node:stream/web'
import { parentPort } from 'node:worker_threads'

import { createElement } from 'react'
Expand Down Expand Up @@ -112,6 +113,7 @@ const handleRender = async ({ id, input }: MessageReq & { type: 'render' }) => {
registerFwGlobalsAndShims()

async function loadServerFile(filePath: string) {
console.log('rscWorker.ts loadServerFile filePath', filePath)
return import(`file://${filePath}`)
}

Expand Down
27 changes: 26 additions & 1 deletion packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,32 @@ export function convertExpressHeaders(
}

export const getFullUrl = (req: ExpressRequest) => {
return req.protocol + '://' + req.get('host') + req.originalUrl
// For a standard request:
//
// req.originalUrl /about
// req.protocol http
// req.headers.host localhost:8910
// req.get('host') localhost:8910
// baseUrl http://localhost:8910
//
// For an RSC request:
//
// req.originalUrl /rw-rsc/__rwjs__Routes?props=%7B%22location%22%3A%7B%22pathname%22%3A%22%2Fabout%22%2C%22search%22%3A%22%22%7D%7D
// req.protocol http
// req.headers.host localhost:8910
// req.get('host') localhost:8910
// baseUrl http://localhost:8910

console.log('getFullUrl req.originalUrl', req.originalUrl)
console.log('getFullUrl req.protocol', req.protocol)
console.log('getFullUrl req.headers.host', req.headers.host)
console.log("getFullUrl req.get('host')", req.get('host'))

const baseUrl = req.protocol + '://' + req.headers.host

console.log('getFullUrl baseUrl', baseUrl)

return baseUrl + req.originalUrl
}

function isRscFetchProps(
Expand Down

0 comments on commit 11f30ab

Please sign in to comment.