ISSUE DESCRIPTION defineModel on Firefox does not work as expected Vue version 3.5.17…
- User
ISSUE DESCRIPTION
defineModel on Firefox does not work as expected
Vue version
3.5.17
Link to minimal reproduction
Steps to reproduce
- Use firefox
- Enter anything in the field. It should be blocked and only show "a", but it wont on firefox.
What is expected?
It should only keep "a" and nothing else should "make it through"
What is actually happening?
Seems like either a race condition or firefox not updating at the same time as Chrome? no clue.
System Info
System: OS: Windows 11 10.0.26100 CPU: (20) x64 Intel(R) Core(TM) i5-14600K Memory: 12.60 GB / 31.77 GB Binaries: Node: 22.16.0 - C:\nvm4w\nodejs\node.EXE npm: 10.9.2 - C:\nvm4w\nodejs\npm.CMD Browsers: Edge: Chromium (134.0.3124.85) Internet Explorer: 11.0.26100.1882 -- Its not showing my actual browser: Firefox 139.0.4Any additional comments?
No response
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /core, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure to understand what we're dealing with. Let's first look at the root directory.
- Assistant
Let me get a better overview of the repository structure.
- Assistant
This is the Vue.js core repository. Let me look at the issue more closely. The problem is about
defineModelon Firefox not working as expected - specifically with input filtering where only "a" should be allowed but other characters get through. Let me find the relevant source code. - Assistant
The issue is about
defineModelnot working as expected in Firefox. Let me understand the reproduction code first. From the URL, it seems like a Vue Playground link. Let me decode it to understand the component.The issue is that
defineModelwith value transformation/filtering doesn't work correctly in Firefox. This is likely related to how input events are handled differently in Firefox vs Chrome.Let me look at the
defineModelimplementation in the Vue compiler and runtime. - Assistant
Let me explore the issue more. The issue is about
defineModelnot working properly on Firefox. The reproduction shows an input field that should only keep "a" but it's not working on Firefox. Let me look at the playground link to understand what the code does.Let me first decode the playground URL to understand the reproduction case.
Actually, let me first look at the
useModelfunction in runtime-core sincedefineModelcompiles down to that. - Assistant
Let me look at the useModel implementation and the useAttrsVModel implementation.
- Assistant
Let me check the playground code to understand what's happening:
- Assistant
Let me try with node instead.
- Assistant
Let me try to decode the playground URL differently. The hash is URL-safe base64 encoded and compressed.
- Assistant
Now I understand the reproduction! The issue is with
<input v-model="model" @input="onInput" />wheremodelcomes fromdefineModel(). TheonInputfunction always setsmodel.value = 'a', but in Firefox it's not working as expected. This is likely an event handler ordering or input synchronization issue.Let me look at how
v-modelon native input elements works and how it interacts withdefineModel. - Assistant
Let me look at the vModel directive implementation to understand how it handles input events.
- Assistant
Now let me understand the issue. The reproduction is:
<input v-model="model" @input="onInput" />wheremodelis fromdefineModel()onInputalways setsmodel.value = 'a'- Expected: input should always show "a"
- Actual on Firefox: other characters get through
Let me look at how the compiler handles this case and how event handlers are merged:
- Assistant
Let me look at the
vModel.tstransform in compiler-dom. - Assistant
Now let me look at the event handler registration:
- Assistant
Now I understand the reproduction. Let me trace the issue:
The reproduction has:
<template> <input v-model="model" @input="onInput" /> </template>When a user types, the
vModelTexthandler readsel.valueand calls the assigner, while the@inputhandler setsmodel.value = 'a'. On Firefox, the DOM may not be updated fast enough when the v-model handler fires.Let me look at how the
beforeUpdateofvModelTexthandles the case where the element is focused and has a different value from the model. - Assistant
Let me understand the compiled output better. Let me check how the compiler transforms v-model on native elements:
- Assistant
Let me understand the issue better. The reproduction code has:
<input v-model="model" @input="onInput" />where
modelcomes fromdefineModel(). TheonInputfunction setsmodel.value = 'a'. The issue is that this should always show "a" but on Firefox it doesn't.Let me look at the useModel test to understand the expected behavior:
- Assistant
Let me look at how the event handler ordering works when both
v-modeland@inputare on the same element: - Assistant
Let me look at how props and directives are patched in the runtime-core to understand the order.
- Assistant
Let me look at the renderer to understand the order of prop patching and directive application.
- Assistant
Let me look at the full patchElement function to see the order of operations.
- Assistant
I can see that in
patchElement, the order is:invokeDirectiveHook(n2, n1, parentComponent, 'beforeUpdate')(line 853) - this calls directivebeforeUpdatehooks BEFORE props are patched- Then props are patched3. Then
invokeDirectiveHook(n2, n1, parentComponent, 'updated')is called after
So the directive
beforeUpdatehook runs BEFORE props are patched. This means whenvModelText.beforeUpdateruns, the props (including@inputhandler) haven't been updated yet.But wait, the
vModelTextdirective'screatedhook adds the event listener viaaddEventListener. And the@inputevent handler is managed by Vue's event system. Let me think about the order of operations more carefully.When the input event fires:
- The
vModelTexthandler (registered viaaddEventListenerincreated) fires and callsel[assignKey](el.value)- this calls theonUpdate:modelValuehandler which triggers model update - The
@input="onInput"handler fires and setsmodel.value = 'a'
But wait, the order depends on how they're registered. The
@input="onInput"is registered via Vue's event system (patchEvent), which creates an invoker and adds it via addEventListener. ThevModelTexthandler is also added via addEventListener.The key question is: which event handler fires first? Since both useaddEventListener, it depends on the order they were registered.Let me think about the order of registration:
- During patch, props are processed first (which registers the
@inputhandler) - Then directives'
createdhook is called (which registers thevModelTexthandler)
So the
@inputhandler is registered FIRST, then thevModelTexthandler. This means when the input event fires:@input="onInput"handler fires first - setsmodel.value = 'a'vModelTexthandler fires second - readsel.value(the actual DOM value, e.g. "b") and callsel[assignKey]("b")
So the
vModelTexthandler always reads the DOM value and pushes it to the model, overwriting the 'a' that was set in step 1.BUT WAIT - this happens on Chrome too. The issue says it works on Chrome but not on Firefox. So there must be something else going on.
Let me look at the
beforeUpdateofvModelTextmore carefully:beforeUpdate(el, { value, oldValue, modifiers: { lazy, trim, number } }, vnode) { el[assignKey] = getModelAssigner(vnode) if ((el as any).composing) return const elValue = (number || el.type === 'number') && !/^0\d/.test(el.value) ? looseToNumber(el.value) : el.value const newValue = value == null ? '' : value if (elValue === newValue) { return } const rootNode = el.getRootNode() if ( (rootNode instanceof Document || rootNode instanceof ShadowRoot) && rootNode.activeElement === el && el.type !== 'range' ) { if (lazy && value === oldValue) { return } if (trim && el.value.trim() === newValue) { return } } el.value = newValue },When the model is updated to 'a' (step 1 above), a re-render is triggered. During this re-render,
beforeUpdateruns withvalue = 'a'(the new model value). But at this point:el.valueis still the old DOM value (e.g., "b" - what the user typed)- So
elValue !== newValue(i.e., "b" !== "a") - The element is focused (activeElement === el)
- None of the early return conditions apply (no lazy, no trim)
- So it sets
el.value = 'a'✓
But then the
vModelTexthandler from step 2 fires and readsel.valuewhich is now 'a' (because we just set it). So it callsel[assignKey]('a')which... doesn't change anything because the model is already 'a'.Wait, that can't be right. The issue is about the order of operations within a single event handler callback vs. the re-render.
Let me reconsider. When the
inputevent fires:- Both event handlers fire synchronously in the same event loop
@input="onInput"fires:model.value = 'a'→ this triggers the customRef setter →i.emit('update:modelValue', 'a')→ the parent updates3.vModelTexthandler fires: readsel.value(still 'b' because no re-render has happened yet) → callsel[assignKey]('b')→ emitsupdate:modelValue('b')
So after the event handler callback, the parent has
modelValue = 'b'(the last emit wins).Then a re-render happens:
vModelText.beforeUpdateruns withvalue = 'b'el.valueis 'b' →elValue === newValue→ returns early, no DOM update
So the input shows 'b', not 'a'. But the user says it works on Chrome...
Actually wait, I need to think about this more carefully. The
model.value = 'a'inonInputdoesn't directly emit. Let me trace through the customRef setter again:set(value) { const emittedValue = options.set ? options.set(value) : value if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return } const rawProps = i.vnode!.props if ( !( rawProps && (name in rawProps || camelizedName in rawProps || hyphenatedName in rawProps) && (`onUpdate:${name}` in rawProps || `onUpdate:${camelizedName}` in rawProps || `onUpdate:${hyphenatedName}` in rawProps) ) ) { // no v-model, local update localValue = value trigger() } i.emit(`update:${name}`, emittedValue) // ... },So when
model.value = 'a'is called:emittedValue = 'a'- At this point,
localValuewas already updated to 'b' (by the watchSyncEffect that ran when the vModelText handler set the model via theonUpdate:modelValueevent)
Wait, no. The order matters. Let me re-trace:
User types "b":
inputevent fires@input="onInput"fires FIRST (registered first):model.value = 'a'- customRef setter called with 'a'
localValueis currently whatever it was before (let's say '' - empty)hasChanged('a', '')is true- v-model is present (from parent), so it does NOT update localValue directly
- emits
update:modelValue('a')to parent
vModelTexthandler fires SECOND (registered second): readsel.value= 'b', callsel[assignKey]('b')- This calls
onUpdate:modelValue('b')which goes through to... wait, actually theonUpdate:modelValuehandler for the native<input>is compiled fromv-model="model"and is the assigner fromgetModelAssigner. It callsmodel.value = 'b'through the parent's update mechanism.
- This calls
Hmm wait, I need to think about this differently. The
<input v-model="model" @input="onInput" />is inside the component that usesdefineModel(). So:The template
<input v-model="model" @input="onInput" />is compiled to something like:createVNode("input", { value: model.value, onInput: [onInput, ...maybe other], "onUpdate:modelValue": $event => (model.value = $event) }, ...)Wait, for native elements with
v-model, the compiler uses thevModelTextdirective which internally registers aninputevent listener that callsel[assignKey]('el.value')whereel[assignKey]isgetModelAssigner(vnode)which returns theonUpdate:modelValuehandler.But the
@input="onInput"is also present, so the compiled result has:{ value: model.value, onInput: onInput, "onUpdate:modelValue": $event => (model.value = $event) }Wait actually let me look at how the compiler handles this. When both
v-modeland@inputare on the same element, they merge the event handlers.Actually wait,
v-modelon a native element is handled by thevModelTextdirective. The directive creates its owninputevent listener viaaddEventListenerin thecreatedhook. The@input="onInput"is handled by Vue's event system viapatchEvent. These are two separate event listeners.So when the user types:
- First event handler fires (whichever was registered first)
- Second event handler fires (whichever was registered second)
Since props are patched before directives'
createdhook runs (seepatchElement- it invokes directive hooks duringbeforeUpdateandupdated, but for initial mount, it's in the mount path), let me check the mount order: - Assistant
Let me look at the mount sequence for native elements.
- Assistant
Let me now focus on understanding the problem with how
beforeUpdateworks in the vModelText directive. Looking at thebeforeUpdatehook, I can see the key comparison:const elValue = (number || el.type === 'number') && !/^0\d/.test(el.value) ? looseToNumber(el.value) : el.value const newValue = value == null ? '' : value if (elValue === newValue) { return }The issue is about what happens during the event handler flow. Let me look at the actual order of operations by examining how events are registered on the input element:
- Assistant
Now I understand the reproduction code. Let me trace through the flow:
App.vuepassesv-model="amount"toCompComp.vueusesdefineModel()and has<input v-model="model" @input="onInput" />onInputsetsmodel.value = 'a'
The problem is likely related to event handler ordering between the
v-modeldirective's internalinputhandler and the@inputuser handler. Let me compile the component to see the actual output: - Assistant
Let me try to use the compiler-dom directly.
- Assistant
Let me look more closely at how the vModelText directive handles input events and think about the Firefox issue:
- Assistant
Let me look at recent changes related to vModel or event handling:
- Assistant
Let me now focus on the core issue. Looking at the vModelText directive's
beforeUpdatehook, the issue is likely about what happens when the input element is focused and the model value is updated. Let me check thebeforeUpdatelogic more carefully: - Assistant
Let me look at the vModel tests to understand the expected behavior better:
- Assistant
Let me look at how component updates are determined:
- Assistant
Let me look more carefully at the key issue. When both
v-modeland@inputare on the same native input element, the order of event handlers matters. Let me trace through what happens: - Assistant
Let me look at the #11656 fix and the #8546 fix since they seem relevant:
- Assistant
Let me look at the specific change in #11656:
- Assistant
Let me look at the component update flow to understand how
patchComponentworks: - Assistant
Let me now try to understand the issue by looking at how the
beforeUpdateof vModelText interacts with the event handler flow. Let me also look at recent PRs related to this: - Assistant
Let me now look at the
shouldUpdateComponentlogic to understand when child component updates are skipped: - Assistant
Let me now focus on the actual issue. Let me re-read the issue and the reproduction code:
The reproduction shows:
<!-- Comp.vue --> <script setup lang="ts"> const model = defineModel() function onInput(event: Event) { let modifiedValue = 'a' model.value = modifiedValue } </script> <template> <input v-model="model" @input="onInput" /> </template>When you type in the input:
- The
inputevent fires - Two handlers fire:
- The
vModelTextdirective's internal handler (registered viaaddEventListenerincreated) - The
@input="onInput"user handler
- The
The key question is: what order do they fire?
Looking at how the element is mounted:
// In mountElement (renderer.ts): if (dirs) { invokeDirectiveHook(vnode, null, parentComponent, 'created') // vModelText handler registered here } // ... if (props) { for (const key in props) { if (key !== 'value' && !isReservedProp(key)) { hostPatchProp(el, key, null, props[key], namespace, parentComponent) // @input handler registered here } } }So
vModelText's handler is registered FIRST, then@input="onInput"is registered SECOND.When the user types:
vModelTexthandler fires: readsel.value(typed text), callsel[assignKey](el.value)which callsonUpdate:modelValue→ this triggersmodel.value = typedValue@input="onInput"handler fires: callsmodel.value = 'a'
But wait -
el[assignKey]is theonUpdate:modelValuefunction. Forv-model="model"wheremodelis fromdefineModel(), this handler is$event => ((model).value = $event).So step 1 sets
model.valueto the typed value. Step 2 setsmodel.valueto 'a'.But here's the critical thing:
modelis acustomReffromuseModel. Whenmodel.value = typedValueis called in step 1, theuseModelsetter fires, which emitsupdate:modelValue(typedValue)to the parent. Then whenmodel.value = 'a'is called in step 2, theuseModelsetter fires again, emittingupdate:modelValue('a')to the parent.So the parent gets both events. The parent's ref ends up as 'a'. Then the parent re-renders, and
watchSyncEffectinuseModeldetects the change, updateslocalValueto 'a', and triggers. ThenvModelText.beforeUpdatefires withvalue = 'a',el.valueis still the typed value, so it setsel.value = 'a'.This should work... but the issue says it doesn't work on Firefox.
Wait, let me think about this more carefully. Maybe the issue is about the
beforeUpdatecomparison. Let me look at the code path again:In
vModelText.beforeUpdate:const elValue = (number || el.type === 'number') && !/^0\d/.test(el.value) ? looseToNumber(el.value) : el.value const newValue = value == null ? '' : value if (elValue === newValue) { return // Skip update }If
el.valueis already 'a' (because something else set it), andnewValueis 'a', thenelValue === newValueand the update is skipped. But who would have setel.valueto 'a'?Unless... the
beforeUpdateitself is settingel.value = 'a'in a previous cycle, and then on the next keystroke,el.valuestarts with 'a' and then the typed character is appended.Wait, let me think about the timeline more carefully:
First keystroke:
- User types 'b'.
el.valuebecomes 'b'. vModelTexthandler: readsel.value= 'b', emits 'b' to parent@inputhandler: setsmodel.value = 'a', emits 'a' to parent- Parent re-renders with 'a'.
watchSyncEffectruns. Child re-renders. vModelText.beforeUpdate:value = 'a',el.value = 'b'(still),elValue !== newValue, setsel.value = 'a'
OK so after first keystroke,
el.value = 'a'.Second keystroke:
- User types 'c'.
el.valuebecomes 'ac'. vModelTexthandler: readsel.value= 'ac', emits 'ac' to parent@inputhandler: setsmodel.value = 'a', emits 'a' to parent- Parent re-renders... but
amountwas already 'a'! Does the parent actually re-render?
Hmm, in step 2, parent's
amountgoes from 'a' to 'ac'. In step 3, it goes back to 'a'. Vue batches the updates. The final value is 'a'. Since the value didn't actually change from the last render ('a' → 'a'), the parent might not re-render the child.But wait, Vue's reactivity tracks that the ref was set. Even if the final value is the same, the effect might still be triggered. Let me think...
When
amount.value = 'ac'is called, the ref's value changes, triggering the parent's render effect. Thenamount.value = 'a'is called, value changes again, but the effect is already queued. The effect runs once with the final value 'a'.In the parent's render, it creates a vnode for
CompwithmodelValue = 'a'. If the previous vnode also hadmodelValue = 'a', thenshouldUpdateComponentmight skip the update.Let me check
shouldUpdateComponent: - The
- Assistant
Now I need to understand the exact issue better. The problem is:
vModelTextdirective adds aninputevent handler increatedhook@input="onInput"adds anotherinputevent handler via props- When user types, both fire - vModelText's handler reads
el.valueand passes it to model first, thenonInputsets model to 'a' - On subsequent keystrokes, the parent's value doesn't actually change (was already 'a'), so the child doesn't re-render, and
el.valueis never corrected
Let me verify this theory by examining how
shouldUpdateComponentworks: - Assistant
Now I have a clear picture of the issue. Let me trace through the exact problem:
- User types in
<input v-model="model" @input="onInput" /> vModelText's handler fires FIRST (registered increatedbefore props): readsel.value= typed text, callsmodel.value = typedText→ emits to parent@input="onInput"fires SECOND: setsmodel.value = 'a'→ emits to parent- Parent value ends up as 'a' (same as before typing on subsequent keystrokes)
- Since parent value didn't change →
shouldUpdateComponentreturns false → child doesn't re-render →el.valueis never corrected
The fix needs to be in
useModel- when the model is set to the same value it already has (but was set to a different value earlier in the same tick), we need to force a local trigger so the child re-renders andvModelText.beforeUpdatecan correctel.value.Let me implement this fix:
- User types in
- Assistant
Now I can see the exact code. Let me implement the fix. The issue is that when
model.valueis set to the same value it already has (e.g., 'a'), but was previously set to a different value in the same synchronous event handler chain (e.g., the typed text fromvModelText), theuseModelsetter emits to the parent but doesn't trigger a local update. Since the parent's value didn't change, the child doesn't re-render, andel.valueis never corrected byvModelText.beforeUpdate. - Assistant
Let me use the edit command directly:
- Assistant
Now I need to edit the file to add the fix. Let me add the condition after the
#10279check. - Assistant
Good, the edit looks correct. Now let me run the existing tests to make sure the fix doesn't break anything:
- Assistant
All 14 tests pass. Let me also run the vModel tests:
- Assistant
All tests pass. Let me also run the defineModel tests:
- Assistant
All tests pass. Now let me create a test script to verify the fix works for the specific issue described:
- Assistant
Let me write a proper test that simulates the exact issue scenario using the existing test infrastructure:
- Assistant
The test file needs to be inside the packages directory. Let me place it correctly:
- Assistant
The path is wrong. Let me fix it.
- Assistant
Let me look at how the existing test imports useModel:
- Assistant
Let me check how the existing test imports useModel.
- Assistant
The path should be
../../src/helpers/useModel. Let me fix the test file: - Assistant
The test passes. Now let me clean up the test file and run all the relevant tests to make sure nothing is broken:
- Assistant
Now let me run all the relevant tests to make sure nothing is broken:
- Assistant
All 54 tests pass. Let me run a broader set of tests to make sure nothing else is broken:
- Assistant
All 1019 tests pass. Let me also run the runtime-dom tests:
- Assistant
All tests pass. Let me verify the final state of the changed file:
- Assistant
Let me check the diff to confirm the change:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →