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
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 statusor commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me analyze this issue. The user is reporting a bug with
defineModelin Vue 3.5.17 on Firefox. The issue is that when entering text in a field, it should be blocked and only show "a", but on Firefox it doesn't work as expected.Let me first explore the repository structure to understand what we're working with.
Assistant
This looks like the Vue 3 core repository. Let me look at the Vue SFC playground URL to understand the issue better, and then explore the relevant source code.
Assistant
Let me decode the Vue SFC playground URL to understand the reproduction case:
Assistant
The base64 string from the URL might have been URL-encoded. Let me try decoding it properly - the URL has #eNqF... which is the encoded payload. Let me try with proper URL decoding first.
Assistant
Let me try a different approach to decode the URL - Vue playground typically uses lz-string encoding:
Assistant
Let me just look at the issue description more carefully. The key point is:
defineModelon Firefox doesn't work as expected- User enters text in a field, it should be blocked and only show "a", but on Firefox it doesn't work
- The issue seems to be a race condition or Firefox not updating at the same time as Chrome
Let me explore the
defineModelimplementation in the Vue codebase:Assistant
The issue is about
defineModelruntime behavior on Firefox. Let me look at theuseModelfunction that powersdefineModel:Assistant
Now let me understand the issue better. The bug is about Firefox not properly updating the model value. Let me look at
watchSyncEffect:Assistant
Let me understand the issue better. The issue seems related to
customRefand the timing oftrigger()andtrack()calls. In Firefox, there might be a race condition where the DOM update happens before the reactivity system properly updates.Let me look at the
customRefimplementation:Assistant
Now I understand the code structure. Let me look at the issue more carefully. The issue is about
defineModelwith Firefox. Looking at the Vue playground URL and the description, the user expects that when they type something, only "a" should be shown (blocking other input). But on Firefox, the input isn't being blocked.This is likely a timing issue where the
watchSyncEffecttriggers the update, but the DOM hasn't been updated yet on Firefox. Let me look at thesetmethod inuseModel:The key issue is in the
sethandler ofuseModel. When a value is set:- It emits the update event to the parent
- The parent receives the update and updates the prop
- The
watchSyncEffectdetects the change and triggers
But if the parent's update handler modifies the value (like keeping only "a"), there could be a race condition on Firefox where the DOM updates before the reactivity system catches up.
Let me search for any related issues or tests:
Assistant
Now let me look at the test for #10279 more carefully. This test is about forcing a local update when a setter formats a value to the same emitted value. The issue described in the bug report seems similar - it's about Firefox not properly syncing the value when input is being validated/filtered.
Let me look at how v-model is implemented in Vue's DOM runtime:
Assistant
Now I understand the code better. Looking at the issue - when you type in an input field, there's a sequence:
- Input event fires → the value is set on the model ref via
useModel useModelemitsupdate:modelValueevent- Parent receives the event and updates its local state
- The prop flows back down to the child component
watchSyncEffectinuseModeldetects the prop change and triggers
The problem is on Firefox, there could be a timing issue where the DOM input element's value is set by the browser before the Vue reactivity system has a chance to update it back.
Let me look more closely at the
setfunction inuseModel.ts:- Input event fires → the value is set on the model ref via
Assistant
Let me look at the git history and changelogs for any recent fixes related to this issue:
Assistant
Let me look at the most recent fixes, especially
c0418a3b8:Assistant
Let me look at the CHANGELOG for any recent defineModel fixes:
Assistant
Let me look at the Vue SFC playground code. Based on the issue description, I need to understand what the user is doing. The issue says:
- Enter anything in the field
- It should be blocked and only show "a"
This sounds like there's a custom setter that only allows "a" through. Let me try to reproduce the issue by looking at the reproduction code. Let me try a different approach to decode the playground URL:
Assistant
The hash is gzip compressed. Let me try to properly decompress it:
Assistant
The first bytes are
78dawhich is a zlib header (deflate with zlib wrapper), not gzip. Let me try with raw inflate:Assistant
The decompression keeps failing. Let me try a different approach - I'll look for the Vue SFC playground's compression format or simply search for existing issues related to defineModel on Firefox:
Assistant
Let me think about the issue more carefully. The reproduction code from the Vue SFC playground shows that:
- User enters text in a field
- The text should be blocked and only show "a"
- On Firefox, it doesn't block the input properly
This is likely related to how the
customReftriggers updates and howwatchSyncEffectsyncs props. The problem on Firefox could be that when setting the model value through a setter with validation, thelocalValueincustomRefdoesn't update immediately, causing the input element to still show the user's raw input.Looking at the
setfunction inuseModel.ts:set(value) { const emittedValue = options.set ? options.set(value) : value if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return // <-- EARLY RETURN if emitted value equals localValue } ... i.emit(`update:${name}`, emittedValue) ... if ( hasChanged(value, emittedValue) && hasChanged(value, prevSetValue) && !hasChanged(emittedValue, prevEmittedValue) ) { trigger() // <-- Only triggers if value was converted by setter } ... }Assistant
Interesting! This is the fix for #10279. Now let me check if there have been any changes since then that might have broken this:
Assistant
Let me look at the commits that came after the #10279 fix:
Assistant
Interesting! This commit
f1bb0aef0added a checkif (!hasChanged(value, localValue)) { return }at the beginning of thesetfunction. This would prevent emitting when setting to the same value. But let me look at the next commit:Assistant
Now let me look at the current state of the useModel file and understand the issue better. The problem might be related to when
localValuehasn't been updated yet when the setter is called, but the input element has already been updated by the browser (Firefox).Assistant
Now I have a better understanding. Let me look at the current code and see if I can understand the Firefox-specific issue:
Assistant
Assistant
Let me run the existing tests first to make sure everything passes:
Assistant
Let me try running vitest directly with a timeout and see if tests pass.
Assistant
Assistant
I've confirmed the bug. When
prevSetValueisEMPTY_OBJ(first user input) and the filtered value equalslocalValue, the function returns early without triggering an update.The fix is to add an additional check:
!hasChanged(value, localValue)- we should only return early if the user's actual input equals the local value. If the user typed something different that got filtered to the same value, we should still process it and trigger an update.Let me implement the fix:
Assistant
Now let me open the file and make the fix:
Assistant
Now I understand the issue. The problem is in the early return condition at lines 61-66:
if ( !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) ) { return }The issue is: when
prevSetValue === EMPTY_OBJ(first user input), the second part!(prevSetValue !== EMPTY_OBJ && ...)evaluates to!(false && ...) = true. Combined with the first part!hasChanged(emittedValue, localValue)being true (when the filtered value equals localValue), the condition istrue && true = true, causing an early return.The fix should be: we should NOT return early when the user typed something different from
localValue, even if the filtered result equalslocalValue. The early return should only happen when the actual input value equalslocalValue.Assistant
Wait, let me reconsider my fix. Adding
!hasChanged(value, localValue)would mean we only return early if the value the user typed equalslocalValue. But this might cause issues with the original intent of the early return check.Let me trace through the scenarios again:
Original condition (before fix):
!hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))My fix:
!hasChanged(emittedValue, localValue) && !hasChanged(value, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))Let me trace through different scenarios:
-
User types same value as localValue (no change needed):
value === localValue- Old: && = depends
-
Assistant
All 14 tests pass. Now let me verify my fix with the test script:
Assistant
The fix works correctly. Let me also run a broader set of tests to make sure I didn't break anything:
Assistant
Excellent! All 1019 tests in the runtime-core package pass! Let me also run some related tests to make sure the fix doesn't break anything else:
Assistant
All runtime-dom tests pass too, including the vModel tests. Let me also run the compiler-sfc tests since they involve defineModel compilation:
Assistant
The tests passed. Now let me verify the fix is correct and check the diff to make sure everything looks good before submitting.
Assistant
The fix looks correct. Let me summarize what I've done:
Summary
Bug Analysis
The issue was in the
useModelhelper function inpackages/runtime-core/src/helpers/useModel.ts. When usingdefineModelwith a custom setter that filters/transforms input values, there was a race condition on Firefox where the input would not be properly updated.The problem occurred when:
prevSetValue === EMPTY_OBJ(first user input after component mount)- User types a value that gets filtered to the same value as
localValue - The early return condition was incorrectly triggered, skipping the necessary DOM update
The Fix
Added
!hasChanged(value, localValue)to the early return condition. This ensures that we only skip processing when the user's actual input equalslocalValue, not just when the filtered/emitted value equalslocalValue.Tests Passed
- ✅ All 14 useModel tests
- ✅ All 1019 runtime-core tests
- ✅ All 231 runtime-dom tests (including vModel tests)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.