You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
select v-model does not set option selected attribute in rendered HTML using SSR #5339
🍰 p2-nice-to-havePriority 2: this is not breaking anything but nice to have it addressed.🐞 bugSomething isn't workinghas workaroundA workaround has been found to avoid the problemscope: ssr
Create a template with <select v-model="..."> and some <option :value="..."> elements. And then look at the rendered HTML. There will never be a selected attribute on the option elements.
When loading the SSR rendered page with disabled JavaScript it will always select the first option, because no option in the HTML is actually selected.
What is expected?
Similar behavior to <input v-model="...">. For input elements this will insert a value="..." attribute.
Though <select v-model="..."> should insert a selected attribute for the selected option element. (Or elements when using <select multiple>).
What is actually happening?
No selected attribute is added to the SSR rendered HTML.
This is a bit tricky as the current implementation of v-model on select elements does set the selected attribute on mounted, and do so on purpose, because it has to wait for the options to be rendered to then pick the right one to set the attribute on.
So in that sense, this works as intended and it's not really a bug.
Though I see the challenge when JS is disabled on the client. So far I don't have a bright idea how this could be improved, besides the workaround already documented.
@LinusBorg Thanks for the explanation. This sounded to me like <input type="radio"> should have the same issue. But it works perfectly well for radio buttons and disabled JavaScript. Or is this something different?
Radio buttons are different, as Vue can evaluate each one on their own during render, and thus can set the checked attribute before mounting the element.
The select element on the other hand needs to compare its v-model value to the options children that are created independently from it.
Not saying this is not solvable with another approach, just describing the current one and why it works the way it does.
🍰 p2-nice-to-havePriority 2: this is not breaking anything but nice to have it addressed.🐞 bugSomething isn't workinghas workaroundA workaround has been found to avoid the problemscope: ssr
Activity
LinusBorg commentedon Feb 2, 2022
This is a bit tricky as the current implementation of v-model on
selectelements does set theselectedattribute onmounted, and do so on purpose, because it has to wait for the options to be rendered to then pick the right one to set the attribute on.So in that sense, this works as intended and it's not really a bug.
Though I see the challenge when JS is disabled on the client. So far I don't have a bright idea how this could be improved, besides the workaround already documented.
benneq commentedon Feb 2, 2022
@LinusBorg Thanks for the explanation. This sounded to me like
<input type="radio">should have the same issue. But it works perfectly well for radio buttons and disabled JavaScript. Or is this something different?This will correctly set the
checkedattribute to theinputwithvalue="b"with disabled JavaScriptLinusBorg commentedon Feb 2, 2022
Radio buttons are different, as Vue can evaluate each one on their own during render, and thus can set the checked attribute before mounting the element.
The select element on the other hand needs to compare its v-model value to the options children that are created independently from it.
Not saying this is not solvable with another approach, just describing the current one and why it works the way it does.
edison1105 commentedon Mar 3, 2023
duplicate of #7392 and will be fixed via #7432