Unverified Commit 99b5c803 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix HMR subscription initialization race

parent 08c7ab6c
......@@ -2558,13 +2558,12 @@ impl Project {
}
let version_op = hmr_version_operation(self, chunk_name.clone(), target);
// 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 initial_version = version_op
.read_trait_strongly_consistent()
.untracked()
.await?;
// INVALIDATION: This is intentionally eventual and untracked. The baseline
// must remain the first version observed when the subscription starts. A
// strongly consistent read can restart after an invalidation and instead
// adopt the post-edit version. The following update then appears empty even
// though an already-loaded browser still has the pre-edit output.
let initial_version = version_op.connect().into_trait_ref().untracked().await?;
if tracing::enabled!(target: "turbopack_hmr_diagnostics", tracing::Level::INFO) {
let version_id = TraitRef::cell(initial_version.clone())
.id()
......
......@@ -1050,8 +1050,10 @@ export async function createHotReloaderTurbopack(
state.subscriptions.set(id, subscription)
hmrDiagnostic('client-subscription-created', { id })
// The subscription will always emit once, which is the initial
// computation. This is not a change, so swallow it.
// The subscription always emits once for its initial computation. Usually
// that is issues-only, but an edit can land while the baseline is being
// established. In that case the first emission is already a real update
// and must be forwarded to the browser.
try {
const initial = await subscription.next()
hmrDiagnostic('client-subscription-initial', {
......@@ -1059,6 +1061,23 @@ export async function createHotReloaderTurbopack(
done: initial.done,
updateType: initial.value?.type,
})
if (initial.done) {
return
}
const initialData = initial.value
processIssues(state.clientIssues, key, initialData, false, true)
if (initialData.type !== 'issues') {
hmrDiagnostic('client-subscription-initial-update', {
id,
updateType: initialData.type,
instructionType:
'instruction' in initialData
? (initialData.instruction as { type?: string } | undefined)?.type
: undefined,
})
sendTurbopackMessage(initialData as TurbopackUpdate)
}
for await (const data of subscription) {
hmrDiagnostic('client-subscription-update', {
......
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