Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/plugin-rsc/src/core/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ let init = false

export function setRequireModule(options: {
load: (id: string) => unknown
/**
* Called EVERY time a module is requested
*/
onLoad?: (id: string) => void
}): void {
if (init) return
init = true
Expand All @@ -15,6 +19,8 @@ export function setRequireModule(options: {
})

const clientRequire = (id: string) => {
const cleanId = removeReferenceCacheTag(id)
options.onLoad?.(cleanId)
return requireModule(id)
}
;(globalThis as any).__vite_rsc_client_require__ = clientRequire
Expand Down
34 changes: 34 additions & 0 deletions packages/plugin-rsc/src/ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ export { createServerConsumerManifest } from './core/ssr'

export * from './react/ssr'

/**
* Callback type for client reference dependency notifications.
* Called during SSR when a client component's dependencies are loaded.
*/
export type OnClientReferenceDeps = (deps: {
js: readonly string[]
css: readonly string[]
}) => void

// Registered callback for client reference deps
let onClientReferenceDepsCallback: OnClientReferenceDeps | undefined

/**
* Register a callback to be notified when client reference dependencies are loaded.
* Called during SSR when a client component is accessed.
*
*/
export function setOnClientReferenceDeps(
callback: OnClientReferenceDeps | undefined,
): void {
onClientReferenceDepsCallback = callback
}

initialize()

function initialize(): void {
Expand Down Expand Up @@ -38,6 +61,17 @@ function initialize(): void {
return wrapResourceProxy(mod, deps)
}
},
// Called EVERY time a module is requested (not memoized).
// Notify framework callback for per-request asset collection.
onLoad: (id) => {
if (!import.meta.env.__vite_rsc_build__) return
if (onClientReferenceDepsCallback) {
const deps = assetsManifest.clientReferenceDeps[id]
if (deps) {
onClientReferenceDepsCallback({ js: deps.js, css: deps.css })
}
}
},
})
}

Expand Down
Loading