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 analyze the issue carefully. The problem is about
defineModelnot working correctly in Firefox, but works in Chrome. Let me start by exploring the repository structure.Assistant
This is the Vue.js core repository. Let me explore the structure of packages.
Assistant
Let me look at the
useModelfunction in the helpers:Assistant
Now let me look at the playground link content to understand the example. Let me look at the encoded part:
Assistant
Now I understand the issue. Looking at the reproduction:
<!-- 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><!-- App.vue --> <script setup lang="ts"> import { ref} from 'vue' import Comp from './Comp.vue'; const amount = ref('') </script> <template> <Comp v-model="amount"/> </template>So the user enters something in the input field, the
onInputhandler runs which setsmodel.value = 'a', and the model is bound to the input viav-model. The user expects that only "a" should remain.The issue is in
useModel(the implementation ofdefineModel). Looking at it 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 && // check if parent has passed v-model (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) // #10279: if the local value is converted via a setter but the value // emitted to parent was the same, the parent will not trigger any // updates and there will be no prop sync. However the local input state // may be out of sync, so we need to force an update here. if ( hasChanged(value, emittedValue) && hasChanged(value, prevSetValue) && !hasChanged(emittedValue, prevEmittedValue) ) { trigger() } prevSetValue = value prevEmittedValue = emittedValue },Let's trace through what happens when the user types "abc":
- The input fires "input" event with value "abc"
- The
v-model="model"updatesmodel.value = 'abc' - In the setter,
value = 'abc',emittedValue = 'abc', no options.set - Check:
hasChanged('abc', '')is true (initial empty value), so doesn't return early i.emit('update:modelValue', 'abc')- Parent receives the update, sets
amount.value = 'abc' - The watchSyncEffect in the customRef fires because
props.modelValuechanged to 'abc' - Now
localValue = 'abc', trigger is called - Now
@inputhandler runs, setsmodel.value = 'a' - In setter:
value = 'a',emittedValue = 'a' hasChanged('a', 'abc')is true (current localValue is 'abc')- Wait... but at this point, the
@inputhandler runs afterv-modelhas setmodel.value = 'abc'. So localValue should be 'abc'. But maybe in Firefox, the order is different?
Let me re-look at the
<input v-model="model" @input="onInput" />. In Vue, when you dov-model="model", it compiles to::value="model" @input="model.value = $event.target.value"Wait, but in Vue 3,
v-modelcompiles to:modelValue="model"and@update:modelValue="model = $event". Sincemodelis a ref, the compiler would usemodel.value = $eventas the update handler.Actually, looking at the v-model directive compilation for the input element, it should compile to:
<input :value="model" @input="$event => model = $event.target.value">Where
modelis the ref. So when the user types, the input event fires, and Vue calls the update handler which setsmodel.value = .... Then, the@input="onInput"also fires (or fires before).Hmm, the order matters here. Let's look at the input event listeners. In Vue, when you bind multiple handlers to an event using
v-modeland@input, the order is:- First, the
v-modelhandler (because it's compiled first into the template) - Then, the explicit
@inputhandler
Wait, actually, looking at Vue's compiler:
v-model="model"compiles to:modelValue(or:value) and@update:modelValue(or@input)- If you add
@input="onInput"to the same element, both should fire
Actually, the order of event handlers might depend on how Vue compiles the template. Let me check.
Actually, looking at this more carefully:
<input v-model="model" @input="onInput" />
When you use
v-modelon a regular<input>, Vue compiles it as::value="model"and@input="model = $event.target.value"
(For text inputs,
v-modelis mapped tovalueprop andinputevent, notmodelValue/update:modelValue.)So in this case:
- User types something (e.g. "abc")
- The native
inputevent fires - Both listeners are called:
a.
model = $event.target.value→ setsmodel.value = 'abc'b.onInput($event)→ setsmodel.value = 'a'
The order matters! In Chrome, maybe order a comes first, in Firefox order b comes first?
Actually, looking at Vue's behavior:
- All event handlers attached via
@are called in the order they appear in the template - But for
v-model, the binding is internal
Let me think about this. The compiled template might be:
h('input', { value: model.value, 'onUpdate:modelValue': $event => model.value = $event, // OR onInput: $event => model.value = $event.target.value, onInput: onInput })Wait,
v-modelon<input>compiles tovalueandonUpdate:modelValuefor components, but for native input, it compiles tovalueandonInput(or just DOM eventproperties).Let me check the compiler to understand:
Assistant
Now let me look at how Vue patches elements and processes directives to understand the order:
Assistant
Let me look at the renderer mountElement to understand the order of operations:
Assistant
Let me look at watchSyncEffect:
Assistant
Let me look at the v-model compiler for native inputs:
Assistant
Let me check what happens when both
onUpdate:modelValueandonInputare present on the element:Assistant
So
onUpdate:modelValueis not a reserved prop, so it would be patched. Let me check what happens:Looking at
runtime-dom/src/patchProp.ts:Assistant
Let me search for existing tests for useModel:
Assistant
Let me look at the watch implementation to understand watchSyncEffect:
Assistant
OK, so
flush === 'sync'means there's no scheduler, and the effect runs immediately. Let me see what that means for props.Let me check
baseWatch:Assistant
Let me actually try to reproduce this issue with a test script:
Assistant
Let me look at how to run the tests:
Assistant
Let me write a test script that simulates the issue:
Assistant
OK
defineModelis a compile-time macro. Letme useuseModeldirectly:Assistant
Let me look at how props are updated:
Assistant
Let me search for related GitHub issues or PRs in the code:
Assistant
Let me check the most recent commit related to v-model andactiveElement:
Assistant
Wait, let me re-read the beforeUpdate code carefully:
Assistant
Let me re-run the test with more debugging to understand the flow:
Assistant
Let me fix the test to include the proper compiled output:
Assistant
Let me add logging to the useModel to trace the issue:
Assistant
Let me modify useModel to add debug logging:
Assistant
Let me check how props are made reactive:
Assistant
The test isn't quite reproducing the issue. The parent doesn't track
amountreactively in my test. Let me fix this:Assistant
Wait, my test now passes! The issue I created was in my test setup. Let me restore the original useModel and verify:
Assistant
Let me search for any code that handles this kind of issue:
Assistant
Let me look at the recent commits and CHANGELOG to understand the issue:
Assistant
Let me check the changelog for recent changes related to this:
Assistant
Let me look at the vModelText directive and the test that already exists:
Assistant
Let me search for any tests related to Firefox or input value behavior:
Assistant
Let me look at the git log for vModel.ts:
Assistant
Let me look at the fix for issue #8546 and #2302:
Assistant
Let me look at the fix for #8547:
Assistant
Let me check the GitHub issue tracker or related PRs more systematically:
Assistant
Let me check the issue more carefully by searching for related code:
Assistant
Let me try compiling the actual template to see the output:
Assistant
Let me use vitest with a simpler test to compile the template:
Assistant
Let me look at the createElement function in runtime-dom:
Assistant
Let me check the useModel at the time of Vue 3.5.17:
Assistant
The useModel is the same as the current code. So the issue must be in vModelText or somewhere else.
Let me check the vModelText at 3.5.17:
Assistant
Let me check the issue tracker by searching for related comments in code:
Assistant
Let me write a more comprehensive test that simulates multiple input events (which is what happens when a user types multiple characters):
Assistant
Now I understand the bug! Let me trace through the issue:
When the user types 'abc' character by character:
- Type 'a': DOM='a', model='a' (via onInput)
- Type 'b' (DOM becomes 'ab'): v-model listener sets model='ab', emit. Then onInput sets model='a', emit. Parent receives 'a' (same as current parent amount). Parent doesn't re-render (value didn't change from parent's perspective). Child doesn't re-render. DOM stays 'ab'.
- Type 'c' (DOM becomes 'abc'): Same issue, DOM stays 'abc'.
The DOM is out of sync because the child doesn't re-render when the parent's value is the same as before. Let me look at the existing trigger logic and propose a fix:
Assistant
Let me verify the bug and try a fix. First, let me look at the full useModel code one more time:
Assistant
Now I understand the bug. Let me make a fix. The cleanest fix is to trigger the customRef at the end of the setter, which will notify the render effect, causing the child component to re-render. This will ensure
vModelText'sbeforeUpdateis called to sync the DOM.Let me apply the fix:
Assistant
Let me apply the fix. The issue is that the child doesn't re-render when the parent's prop is the same as before, even though the DOM is out of sync. I'll trigger the customRef in the setter to force a re-render.
Let me first verify my understanding by writing a test that demonstrates the issue is fixed:
Assistant
Let me apply the fix. I'll trigger the customRef in the setter to ensure the child re-renders and the DOM is updated:
Assistant
The fix works! Now after typing 'abc', the DOM is correctly 'a'. Let me run all the existing tests to make sure I didn't break anything:
Assistant
My fix is too aggressive. It causes extra re-renders. Let me look at the failing tests:
Assistant
I see, my fix is too aggressive. Let me revert it and apply a more targeted fix:
Assistant
Now let me apply a more targeted fix that only triggers when needed:
Assistant
All existing useModel tests pass. Let me verify the bug is fixed:
Assistant
Let me run all the related tests to ensure nothing is broken:
Assistant
All tests pass. Let me also run the vue package tests to be safe:
Assistant
Now let me clean up my test file before submitting:
Assistant
Let me run all the tests one final time to make sure everything works:
Assistant
All tests pass. Let me verify the final fix:
Assistant
Let me verify the fix one more time with a comprehensive test:
Assistant
The fix works correctly. Let me clean up the test file:
Assistant
Let me verify the final state of useModel.ts:
Assistant
Let me also run a broader test to make sure nothing else is broken:
Assistant
All tests pass. Let me provide a summary of the work:
Summary
Issue
defineModeldoesn't work correctly on Firefox when usingv-modelwith an@inputhandler that resets the value. When a user types characters (e.g., "abc") in an input field with<input v-model="model" @input="onInput" />whereonInputsetsmodel.value = 'a', the input should display 'a' but instead shows the typed value.Root Cause
When the user types multiple characters, the input event fires for each character. For each character:
v-modellistener callsmodel.value = <typed value>(e.g., 'ab')@inputlistener callsmodel.value = 'a'- Both emit
update:modelValueto parent
The parent's value ends up being 'a' (same as the initial value), so the parent doesn't re-render the child with a new prop. The child's
shouldUpdateComponentreturns false, so the child component doesn't re-render. As a result,vModelText'sbeforeUpdatehook doesn't run, and the DOM stays at the typed value (e.g., 'abc') instead of being updated to 'a'.Fix
Added an
else ifbranch to the setter inuseModel.tsthat triggers the customRef when the emitted value equals the current parent prop value. This forces the child component's render effect to run, which callsvModelText'sbeforeUpdatehook to update the DOM.The condition
!hasChanged(emittedValue, props[camelizedName])ensures we only trigger when:- The setter is emitting a value
- The emitted value is the same as the parent's current prop value (meaning the parent won't re-render in a way that updates the child)
This is complementary to the existing
#10279trigger condition (which handles the transformer case). Theelse ifensures we don't trigger twice.Checklist
- If an error was reported in the issue description, I have successfully reproduced it. - Reproduced the bug using a test that simulates user typing character by character.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error. - After applying the fix, the test shows
input.value: "a"after typing 'abc', which is the expected behavior. - I have thought about edge cases and made sure my fix handles them as well. - Tested:
- The original kebab-case tests (no unnecessary re-renders)
- The #10279 force local update test (existing behavior preserved)
- The parent limiting child value test
- Cases where the value is the same as before (handled by early return)
- Cases where the parent re-renders correctly (handled by parent's update mechanism)
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. - All 1451+ tests pass across runtime-core, runtime-dom, vue, vue-compat, and reactivity packages.
Assistant
The fix is minimal and focused - only 10 lines added to
packages/runtime-core/src/helpers/useModel.ts. Let me submit:
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 →