diff --git a/packages/vite/src/rsc/rscWorker.ts b/packages/vite/src/rsc/rscWorker.ts index 11d878199f43..079ca2270410 100644 --- a/packages/vite/src/rsc/rscWorker.ts +++ b/packages/vite/src/rsc/rscWorker.ts @@ -196,7 +196,25 @@ parentPort.on('message', (message: MessageReq) => { // Let me re-assign root type ConfigType = Omit & { root: string } -const configPromise: Promise = resolveConfig({}, 'serve') + +/** + * Gets the Vite config. + * Makes sure root is configured properly and then caches the result + */ +async function getViteConfig() { + let cachedConfig: ConfigType | null = null + + return (async () => { + if (cachedConfig) { + return cachedConfig + } + + cachedConfig = await resolveConfig({}, 'serve') + setRootInConfig(cachedConfig) + + return cachedConfig + })() +} const getFunctionComponent = async (rscId: string) => { // TODO (RSC): Get rid of this when we only use the worker in dev mode @@ -276,8 +294,7 @@ function resolveClientEntryForDev(id: string, config: { base: string }) { } async function setClientEntries(): Promise { - // This is the Vite config - const config = await configPromise + const config = await getViteConfig() const entriesFile = getPaths().web.distRscEntries console.log('setClientEntries :: entriesFile', entriesFile) @@ -372,8 +389,7 @@ async function renderRsc(input: RenderInput): Promise { console.log('renderRsc input', input) - const config = await configPromise - setRootInConfig(config) + const config = await getViteConfig() const component = await getFunctionComponent(input.rscId) @@ -393,15 +409,14 @@ function isSerializedFormData(data?: unknown): data is SerializedFormData { } async function handleRsa(input: RenderInput): Promise { - const config = await configPromise - setRootInConfig(config) - console.log('handleRsa input', input) if (!input.rsfId || !input.args) { throw new Error('Unexpected input') } + const config = await getViteConfig() + const [fileId, name] = input.rsfId.split('#') const fname = path.join(config.root, fileId) console.log('Server Action, fileId', fileId, 'name', name, 'fname', fname)