Skip to content

Server HTML leaves a v-pre element's :x attribute to the browser #394

Closed

Description

@JuroOravec

What happens

In a component that uses Vue, an element inside v-pre that keeps Vue syntax as written, such as :x="y", is not written by the server. Its parent becomes a shell, an element marked data-allow-mismatch="children", and Vue builds the parent's children in the browser:

<main><table v-pre><tr><td><b :x='y' title='t'>A</b></td></tr></table></main>

The server HTML, on main at cada47d9f:

<td data-allow-mismatch="children"><b title="t">A</b></td>

hydration_admission records ('unsupported-attribute', ':x', 'shell', 'td'). A <div v-pre> behaves the same way. Once Vue runs, the page is right: Vue builds <b :x="y" title="t">A</b> in the browser. Only the first paint lacks the attribute, and the shell's contents are rebuilt instead of hydrated.

Why: this is deliberate

  • Hydration does not set plain attributes. It applies only listeners, .prop keys, a few value keys and the keys the render marks as dynamic (hydrateElement in runtime-core). It trusts the server HTML for every other attribute.
    • So if the server wrote the element without :x and let Vue hydrate it, the attribute would stay missing for good.
    • Declining is what keeps the page right.
  • The hydration writer declines the name. client_attribute (crates/citry_vue_compiler/src/server_render.rs) writes only names that start with a lowercase ASCII letter and continue with lowercase letters, digits, -, _ or :. Any other name takes "paths the server does not model", so the part is declined.
  • The shell writer leaves it out. The writer for a shell's contents (StaticWriter) leaves out any name for which is_static_attribute_name is false, which needs a leading ASCII letter. So the shell shows <b title="t"> without :x.

Possible improvement

A name that starts with : could be written in both writers:

  • the HTML tokenizer reads :x="y" as the attribute :x exactly as written;
  • Vue's client sets it with setAttribute(":x", "y"), because : has no meaning in patchProp and ":x" in el is false.

That would let a v-pre element with bound-looking attributes render fully on the server and hydrate. Names starting with @ or # should stay declined: they are not valid XML names, and the browser's setAttribute refuses them.

Before changing it, check:

  • Render code: what Vize's render code holds for each of these names under v-pre.
  • Hydration: that it adopts the element without a mismatch warning in Vue's dev build.
  • Static parts: that a v-pre element Vue compiles to a static string (createStaticVNode) is unaffected.

Priority

Low. Only first-paint markup inside v-pre is affected, and the final page is correct.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions