Skip to content

Commit

Permalink
RSC: getViteConfig in rscWorker (redwoodjs#10567)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed May 13, 2024
1 parent 9bf9ada commit 34d1da9
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/vite/src/rsc/rscWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,25 @@ parentPort.on('message', (message: MessageReq) => {

// Let me re-assign root
type ConfigType = Omit<ResolvedConfig, 'root'> & { root: string }
const configPromise: Promise<ConfigType> = 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
Expand Down Expand Up @@ -276,8 +294,7 @@ function resolveClientEntryForDev(id: string, config: { base: string }) {
}

async function setClientEntries(): Promise<void> {
// This is the Vite config
const config = await configPromise
const config = await getViteConfig()

const entriesFile = getPaths().web.distRscEntries
console.log('setClientEntries :: entriesFile', entriesFile)
Expand Down Expand Up @@ -372,8 +389,7 @@ async function renderRsc(input: RenderInput): Promise<PipeableStream> {

console.log('renderRsc input', input)

const config = await configPromise
setRootInConfig(config)
const config = await getViteConfig()

const component = await getFunctionComponent(input.rscId)

Expand All @@ -393,15 +409,14 @@ function isSerializedFormData(data?: unknown): data is SerializedFormData {
}

async function handleRsa(input: RenderInput): Promise<PipeableStream> {
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)
Expand Down

0 comments on commit 34d1da9

Please sign in to comment.