Commit 34d65631 authored by Will Binns-Smith's avatar Will Binns-Smith

Add server-side HMR infrastructure

Add server HMR functions to enable hot module replacement for server-side
code in Turbopack:

Rust (crates/next-api/src/project.rs):
- server_hmr_content: Get server module content by identifier
- server_hmr_version: Get server module version
- server_hmr_version_state: Get version state for server HMR session
- server_hmr_update: Compute server HMR update delta
- server_hmr_identifiers: List all server HMR identifiers

NAPI bindings (crates/napi/src/next_api/project.rs):
- project_server_hmr_events: Subscribe to server HMR events
- project_server_hmr_identifiers_subscribe: Subscribe to server identifiers

TypeScript (packages/next/src/build/swc):
- serverHmrEvents(): Subscribe to server HMR events
- serverHmrIdentifiersSubscribe(): Subscribe to server identifiers

Debug test code added to hot-reloader-turbopack.ts to verify the
subscriptions work correctly during development.
parent 05da4427
......@@ -2115,6 +2115,93 @@ impl Project {
}
}
/// Get server module content by identifier (path relative to node_root).
/// This is the server-side equivalent of `hmr_content`.
#[turbo_tasks::function]
async fn server_hmr_content(
self: Vc<Self>,
identifier: RcStr,
) -> Result<Vc<OptionVersionedContent>> {
if let Some(map) = self.await?.versioned_content_map {
// Key difference from hmr_content: use node_root instead of client_relative_path
let content = map.get(self.node_root().await?.join(&identifier)?);
Ok(content)
} else {
bail!("must be in dev mode to hmr")
}
}
/// Get server module version.
/// This is the server-side equivalent of `hmr_version`.
#[turbo_tasks::function]
async fn server_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.server_hmr_content(identifier).await?;
if let Some(content) = &*content {
Ok(content.version())
} else {
Ok(Vc::upcast(NotFoundVersion::new()))
}
}
/// Get the version state for a server HMR session. Initialized with the first seen
/// version in that session.
/// This is the server-side equivalent of `hmr_version_state`.
#[turbo_tasks::function]
pub async fn server_hmr_version_state(
self: Vc<Self>,
identifier: RcStr,
session: TransientInstance<()>,
) -> Result<Vc<VersionState>> {
let version = self.server_hmr_version(identifier);
// The session argument is important to avoid caching this function between
// sessions.
let _ = session;
// INVALIDATION: This is intentionally untracked to avoid invalidating this
// function completely. We want to initialize the VersionState with the
// first seen version of the session.
let state = VersionState::new(
version
.into_trait_ref()
.strongly_consistent()
.untracked()
.await?,
)
.await?;
Ok(state)
}
/// Emits opaque HMR events whenever a change is detected in server-side modules
/// internally known as `identifier`.
/// This is the server-side equivalent of `hmr_update`.
#[turbo_tasks::function]
pub async fn server_hmr_update(
self: Vc<Self>,
identifier: RcStr,
from: Vc<VersionState>,
) -> Result<Vc<Update>> {
let from = from.get();
let content = self.server_hmr_content(identifier).await?;
if let Some(content) = *content {
Ok(content.update(from))
} else {
Ok(Update::Missing.cell())
}
}
/// Gets a list of all server HMR identifiers that can be subscribed to.
/// These are paths relative to node_root (e.g., "server/app/page.js").
/// This is the server-side equivalent of `hmr_identifiers`.
#[turbo_tasks::function]
pub async fn server_hmr_identifiers(self: Vc<Self>) -> Result<Vc<Vec<RcStr>>> {
if let Some(map) = self.await?.versioned_content_map {
Ok(map.keys_in_path(self.node_root().owned().await?))
} else {
bail!("must be in dev mode to hmr")
}
}
/// Completion when server side changes are detected in output assets
/// referenced from the roots
#[turbo_tasks::function]
......
......@@ -1232,6 +1232,35 @@ fn project_client_hmr_update_operation(
project.client_hmr_update(identifier, *state)
}
#[turbo_tasks::function(operation)]
fn project_server_hmr_update_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.server_hmr_update(identifier, *state)
}
#[turbo_tasks::function(operation)]
async fn server_hmr_update_with_issues_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Result<Vc<HmrUpdateWithIssues>> {
let update_op = project_server_hmr_update_operation(project, identifier, state);
let update = update_op.read_strongly_consistent().await?;
let issues = get_issues(update_op, NEXT_ISSUE_FILTER).await?;
let diagnostics = get_diagnostics(update_op).await?;
let effects = Arc::new(get_effects(update_op).await?);
Ok(HmrUpdateWithIssues {
update,
issues,
diagnostics,
effects,
}
.cell())
}
#[tracing::instrument(level = "info", name = "get HMR events", skip(project, func))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_client_hmr_events(
......@@ -1317,6 +1346,91 @@ pub fn project_client_hmr_events(
)
}
#[tracing::instrument(level = "info", name = "get server HMR events", skip(project, func))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_server_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
identifier: RcStr,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let container = project.container;
let session = TransientInstance::new(());
subscribe(
project.turbopack_ctx.clone(),
func,
{
let outer_identifier = identifier.clone();
let session = session.clone();
move || {
let identifier: RcStr = outer_identifier.clone();
let session = session.clone();
async move {
let project = container.project().to_resolved().await?;
let state = project
.server_hmr_version_state(identifier.clone(), session)
.to_resolved()
.await?;
let update_op =
server_hmr_update_with_issues_operation(project, identifier.clone(), state);
let update = update_op.read_strongly_consistent().await?;
let HmrUpdateWithIssues {
update,
issues,
diagnostics,
effects,
} = &*update;
effects.apply().await?;
match &**update {
Update::Missing | Update::None => {}
Update::Total(TotalUpdate { to }) => {
state.set(to.clone()).await?;
}
Update::Partial(PartialUpdate { to, .. }) => {
state.set(to.clone()).await?;
}
}
Ok((Some(update.clone()), issues.clone(), diagnostics.clone()))
}
}
},
move |ctx| {
let (update, issues, diags) = ctx.value;
let napi_issues = issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect();
let update_issues = issues
.iter()
.map(|issue| Issue::from(&**issue))
.collect::<Vec<_>>();
let identifier = ResourceIdentifier {
path: identifier.clone(),
headers: None,
};
let update = match update.as_deref() {
None | Some(Update::Missing) | Some(Update::Total(_)) => {
ClientUpdateInstruction::restart(&identifier, &update_issues)
}
Some(Update::Partial(update)) => ClientUpdateInstruction::partial(
&identifier,
&update.instruction,
&update_issues,
),
Some(Update::None) => ClientUpdateInstruction::issues(&identifier, &update_issues),
};
Ok(vec![TurbopackResult {
result: ctx.env.to_js_value(&update)?,
issues: napi_issues,
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
}])
},
)
}
#[napi(object)]
struct HmrIdentifiers {
pub identifiers: Vec<RcStr>,
......@@ -1355,6 +1469,31 @@ fn project_container_client_hmr_identifiers_operation(
container.client_hmr_identifiers()
}
#[turbo_tasks::function(operation)]
fn project_server_hmr_identifiers_operation(
container: ResolvedVc<ProjectContainer>,
) -> Vc<Vec<RcStr>> {
container.project().server_hmr_identifiers()
}
#[turbo_tasks::function(operation)]
async fn get_server_hmr_identifiers_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<HmrIdentifiersWithIssues>> {
let hmr_identifiers_op = project_server_hmr_identifiers_operation(container);
let hmr_identifiers = hmr_identifiers_op.read_strongly_consistent().await?;
let issues = get_issues(hmr_identifiers_op, NEXT_ISSUE_FILTER).await?;
let diagnostics = get_diagnostics(hmr_identifiers_op).await?;
let effects = Arc::new(get_effects(hmr_identifiers_op).await?);
Ok(HmrIdentifiersWithIssues {
identifiers: hmr_identifiers,
issues,
diagnostics,
effects,
}
.cell())
}
#[tracing::instrument(level = "info", name = "get HMR identifiers", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_client_hmr_identifiers_subscribe(
......@@ -1400,6 +1539,51 @@ pub fn project_client_hmr_identifiers_subscribe(
)
}
#[tracing::instrument(level = "info", name = "get server HMR identifiers", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_server_hmr_identifiers_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let container = project.container;
subscribe(
project.turbopack_ctx.clone(),
func,
move || async move {
let hmr_identifiers_with_issues_op =
get_server_hmr_identifiers_with_issues_operation(container);
let HmrIdentifiersWithIssues {
identifiers,
issues,
diagnostics,
effects,
} = &*hmr_identifiers_with_issues_op
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((identifiers.clone(), issues.clone(), diagnostics.clone()))
},
move |ctx| {
let (identifiers, issues, diagnostics) = ctx.value;
Ok(vec![TurbopackResult {
result: HmrIdentifiers {
identifiers: ReadRef::into_owned(identifiers),
},
issues: issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
}])
},
)
}
pub enum UpdateMessage {
Start,
End(UpdateInfo),
......
......@@ -318,6 +318,11 @@ export declare function projectClientHmrEvents(
identifier: RcStr,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectServerHmrEvents(
project: { __napiType: 'Project' },
identifier: RcStr,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export interface HmrIdentifiers {
identifiers: Array<RcStr>
}
......@@ -325,6 +330,10 @@ export declare function projectClientHmrIdentifiersSubscribe(
project: { __napiType: 'Project' },
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectServerHmrIdentifiersSubscribe(
project: { __napiType: 'Project' },
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export interface NapiUpdateMessage {
updateType: string
value?: NapiUpdateInfo
......
......@@ -29,6 +29,7 @@ import type {
Endpoint,
HmrIdentifiers,
Lockfile,
NodeJsHmrUpdate,
PartialProjectOptions,
Project,
ProjectOptions,
......@@ -756,6 +757,29 @@ function bindingToApi(
)
}
serverHmrEvents(identifier: string) {
return subscribe<TurbopackResult<NodeJsHmrUpdate>>(
true,
async (callback) =>
binding.projectServerHmrEvents(
this._nativeProject,
identifier,
callback
)
)
}
serverHmrIdentifiersSubscribe() {
return subscribe<TurbopackResult<HmrIdentifiers>>(
false,
async (callback) =>
binding.projectServerHmrIdentifiersSubscribe(
this._nativeProject,
callback
)
)
}
traceSource(
stackFrame: TurbopackStackFrame,
currentDirectoryFileUrl: string
......
......@@ -195,6 +195,20 @@ interface PartialUpdate extends BaseUpdate {
export type Update = IssuesUpdate | PartialUpdate
export interface NodeJsPartialHmrUpdate extends BaseUpdate {
type: 'partial'
instruction: {
type: 'EcmascriptMergedUpdate'
entries: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<string, { type: 'partial' }>
}
}
export type NodeJsHmrUpdate = IssuesUpdate | NodeJsPartialHmrUpdate
export interface HmrIdentifiers {
identifiers: string[]
}
......@@ -254,6 +268,14 @@ export interface Project {
TurbopackResult<HmrIdentifiers>
>
serverHmrEvents(
identifier: string
): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
serverHmrIdentifiersSubscribe(): AsyncIterableIterator<
TurbopackResult<HmrIdentifiers>
>
getSourceForAsset(filePath: string): Promise<string | null>
getSourceMap(filePath: string): Promise<string | null>
......
......@@ -61,7 +61,11 @@ if (process.env.NODE_ENV === 'development') {
declare global {
var __next__clear_chunk_cache__: (() => void) | null | undefined
var __turbopack_clear_chunk_cache__: () => void | null | undefined
var __turbopack_server_hmr_apply__:
| ((update: NodeJsPartialHmrUpdate) => boolean)
| undefined
}
// hot-reloader modules are not bundled so we need to inject `__next__clear_chunk_cache__`
// into globalThis from this file which is bundled.
if (process.env.TURBOPACK) {
......
......@@ -24,6 +24,8 @@ import type {
TurbopackResult,
Project,
Entrypoints,
NodeJsHmrUpdate,
NodeJsPartialHmrUpdate,
} from '../../build/swc/types'
import { createDefineEnv, getBindingsSync } from '../../build/swc'
import * as Log from '../../build/output/log'
......@@ -144,6 +146,90 @@ const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random())
declare const __next__clear_chunk_cache__: (() => void) | null | undefined
declare const __turbopack_server_hmr_apply__:
| ((update: NodeJsPartialHmrUpdate) => boolean)
| undefined
function setupServerHmr(project: Project) {
const serverHmrSubscriptions = new Map<
string,
AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
>()
function subscribeToServerHmr(identifier: string) {
if (serverHmrSubscriptions.has(identifier)) {
return
}
const subscription = project.serverHmrEvents(identifier)
serverHmrSubscriptions.set(identifier, subscription)
// Start listening for changes in background
;(async () => {
// Skip initial state
await subscription.next()
for await (const result of subscription) {
const update = result as NodeJsHmrUpdate
// Only process partial updates with actual code changes
if (update.type !== 'partial') {
continue
}
const entries = update.instruction?.entries
if (!entries || Object.keys(entries).length === 0) {
continue
}
if (typeof __turbopack_server_hmr_apply__ === 'function') {
const applied = __turbopack_server_hmr_apply__(update as any)
if (applied) {
// TODO: Notify clients to refresh and get updated server content
// This will trigger the client to re-fetch the page with new server components
}
}
}
})().catch((err) => {
console.error('[Server HMR] Subscription error:', err)
serverHmrSubscriptions.delete(identifier)
})
}
// Listen to the Rust bindings update us on changing server HMR identifiers
;(async () => {
const serverHmrIdentifiers = project.serverHmrIdentifiersSubscribe()
// Process identifiers (both initial and subsequent updates)
for await (const data of serverHmrIdentifiers) {
const currentIdentifiers = new Set(
data.identifiers.filter((id) => id.endsWith('.js'))
)
// Clean up subscriptions for removed identifiers (like when pages are deleted)
const identifiersToRemove = []
for (const identifier of serverHmrSubscriptions.keys()) {
if (!currentIdentifiers.has(identifier)) {
identifiersToRemove.push(identifier)
}
}
for (const identifier of identifiersToRemove) {
const subscription = serverHmrSubscriptions.get(identifier)
subscription?.return?.()
serverHmrSubscriptions.delete(identifier)
}
// Subscribe to HMR events for new server modules
for (const identifier of currentIdentifiers) {
if (!serverHmrSubscriptions.has(identifier)) {
subscribeToServerHmr(identifier)
}
}
}
})()
}
/**
* Replaces turbopack:///[project] with the specified project in the `source` field.
*/
......@@ -511,6 +597,8 @@ export async function createHotReloaderTurbopack(
}
}
setupServerHmr(project)
let hmrEventHappened = false
let hmrHash = 0
......@@ -604,7 +692,7 @@ export async function createHotReloaderTurbopack(
sendEnqueuedMessagesDebounce()
}
async function subscribeToChanges(
async function subscribeToClientChanges(
key: EntryKey,
includeIssues: boolean,
endpoint: Endpoint,
......@@ -646,7 +734,7 @@ export async function createHotReloaderTurbopack(
changeSubscriptions.delete(key)
}
async function unsubscribeFromChanges(key: EntryKey) {
async function unsubscribeFromClientChanges(key: EntryKey) {
const subscription = await changeSubscriptions.get(key)
if (subscription) {
await subscription.return?.()
......@@ -655,7 +743,7 @@ export async function createHotReloaderTurbopack(
currentEntryIssues.delete(key)
}
async function subscribeToHmrEvents(client: ws, id: string) {
async function subscribeToClientHmrEvents(client: ws, id: string) {
const key = getEntryKey('assets', 'client', id)
if (!hasEntrypointForKey(currentEntrypoints, key, assetMapper)) {
// maybe throw an error / force the client to reload?
......@@ -696,7 +784,7 @@ export async function createHotReloaderTurbopack(
}
}
function unsubscribeFromHmrEvents(client: ws, id: string) {
function unsubscribeFromClientHmrEvents(client: ws, id: string) {
const state = clientStates.get(client)
if (!state) {
return
......@@ -773,9 +861,9 @@ export async function createHotReloaderTurbopack(
propagateServerField: propagateServerField.bind(null, opts),
sendHmr,
startBuilding,
subscribeToChanges,
unsubscribeFromChanges,
unsubscribeFromHmrEvents,
subscribeToChanges: subscribeToClientChanges,
unsubscribeFromChanges: unsubscribeFromClientChanges,
unsubscribeFromHmrEvents: unsubscribeFromClientHmrEvents,
},
},
})
......@@ -1130,11 +1218,11 @@ export async function createHotReloaderTurbopack(
// Turbopack messages
switch (parsedData.type) {
case 'turbopack-subscribe':
subscribeToHmrEvents(client, parsedData.path)
subscribeToClientHmrEvents(client, parsedData.path)
break
case 'turbopack-unsubscribe':
unsubscribeFromHmrEvents(client, parsedData.path)
unsubscribeFromClientHmrEvents(client, parsedData.path)
break
default:
......@@ -1444,7 +1532,7 @@ export async function createHotReloaderTurbopack(
productionRewrites: undefined,
logErrors: true,
hooks: {
subscribeToChanges,
subscribeToChanges: subscribeToClientChanges,
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
......@@ -1512,7 +1600,7 @@ export async function createHotReloaderTurbopack(
logErrors: true,
hooks: {
subscribeToChanges,
subscribeToChanges: subscribeToClientChanges,
handleWrittenEndpoint: (id, result, forceDeleteCache) => {
currentWrittenEntrypoints.set(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
......
......@@ -665,12 +665,12 @@ describe('next.rs api', () => {
}
}
const result = await project.hmrIdentifiersSubscribe().next()
const result = await project.clientHmrIdentifiersSubscribe().next()
expect(result.done).toBe(false)
const identifiers = result.value.identifiers
expect(identifiers).toHaveProperty('length', expect.toBePositive())
const subscriptions = identifiers.map((identifier) =>
project.hmrEvents(identifier)
project.clientHmrEvents(identifier)
)
await Promise.all(
subscriptions.map(async (subscription) => {
......@@ -792,12 +792,12 @@ describe('next.rs api', () => {
if (route.type !== 'page') throw new Error('unknown route type')
await route.htmlEndpoint.writeToDisk()
const result = await project.hmrIdentifiersSubscribe().next()
const result = await project.clientHmrIdentifiersSubscribe().next()
expect(result.done).toBe(false)
const identifiers = result.value.identifiers
const subscriptions = identifiers.map((identifier) =>
project.hmrEvents(identifier)
project.clientHmrEvents(identifier)
)
await Promise.all(
subscriptions.map(async (subscription) => {
......
......@@ -21,3 +21,9 @@ let DEV_BACKEND: DevRuntimeBackend
function _eval(_: EcmascriptModuleEntry) {
throw new Error('HMR evaluation is not implemented on this backend')
}
function __turbopack_server_hmr_apply__(_update: any): boolean {
throw new Error('Server-side HMR is not implemented for edge runtime')
}
globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__
......@@ -313,6 +313,14 @@ function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath)
}
function __turbopack_server_hmr_apply__(_update: any): boolean {
// TODO: Implement actual HMR logic to update module factories
// For now, just return true to indicate we "accepted" the update
return true
}
globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__
module.exports = (sourcePath: ChunkPath) => ({
m: (id: ModuleId) => getOrInstantiateRuntimeModule(sourcePath, id),
c: (chunkData: ChunkData) => loadRuntimeChunk(sourcePath, chunkData),
......
......@@ -792,6 +792,12 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
*/ function isJs(chunkUrlOrPath) {
return regexJsUrl.test(chunkUrlOrPath);
}
function __turbopack_server_hmr_apply__(_update) {
// TODO: Implement actual HMR logic to update module factories
// For now, just return true to indicate we "accepted" the update
return true;
}
globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__;
module.exports = (sourcePath)=>({
m: (id)=>getOrInstantiateRuntimeModule(sourcePath, id),
c: (chunkData)=>loadRuntimeChunk(sourcePath, chunkData)
......
......@@ -792,6 +792,12 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
*/ function isJs(chunkUrlOrPath) {
return regexJsUrl.test(chunkUrlOrPath);
}
function __turbopack_server_hmr_apply__(_update) {
// TODO: Implement actual HMR logic to update module factories
// For now, just return true to indicate we "accepted" the update
return true;
}
globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__;
module.exports = (sourcePath)=>({
m: (id)=>getOrInstantiateRuntimeModule(sourcePath, id),
c: (chunkData)=>loadRuntimeChunk(sourcePath, chunkData)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment