From 11f30ab225f5f2d701fc94f36e14983bd4c70d46 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Fri, 6 Sep 2024 15:03:04 +0200 Subject: [PATCH] chore(rsc): Random changes, mostly to comments etc, in prep for removing the worker (#11451) --- packages/router/src/rsc/ServerRouter.tsx | 2 ++ packages/vite/modules.d.ts | 2 ++ packages/vite/src/rsc/rscRequestHandler.ts | 8 +++++-- packages/vite/src/rsc/rscWorker.ts | 2 ++ packages/vite/src/utils.ts | 27 +++++++++++++++++++++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/router/src/rsc/ServerRouter.tsx b/packages/router/src/rsc/ServerRouter.tsx index a483ac0efac5..f6bcf39fb641 100644 --- a/packages/router/src/rsc/ServerRouter.tsx +++ b/packages/router/src/rsc/ServerRouter.tsx @@ -24,6 +24,8 @@ export interface RouterProps export const Router: React.FC = ({ 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. diff --git a/packages/vite/modules.d.ts b/packages/vite/modules.d.ts index a231fb393178..51a1472dca1a 100644 --- a/packages/vite/modules.d.ts +++ b/packages/vite/modules.d.ts @@ -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 diff --git a/packages/vite/src/rsc/rscRequestHandler.ts b/packages/vite/src/rsc/rscRequestHandler.ts index 78e2afa6000d..cd345eab064c 100644 --- a/packages/vite/src/rsc/rscRequestHandler.ts +++ b/packages/vite/src/rsc/rscRequestHandler.ts @@ -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> 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, @@ -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 { @@ -205,6 +208,7 @@ export function createRscRequestHandler( }, }) + // TODO (RSC): Can we reuse `pipeable` here somehow? await sendRscFlightToStudio({ rscId, props, diff --git a/packages/vite/src/rsc/rscWorker.ts b/packages/vite/src/rsc/rscWorker.ts index ae60202f4dd6..5308cd08cb2a 100644 --- a/packages/vite/src/rsc/rscWorker.ts +++ b/packages/vite/src/rsc/rscWorker.ts @@ -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' @@ -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}`) } diff --git a/packages/vite/src/utils.ts b/packages/vite/src/utils.ts index 85cfd17f8695..678fe6808857 100644 --- a/packages/vite/src/utils.ts +++ b/packages/vite/src/utils.ts @@ -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(