Skip to content

Support v-once and v-memo in component templates #166

Open

Description

@JuroOravec

(Opus 5.5)

Problem

Templates can't use Vue's v-once or v-memo; Citry rejects them when the template loads. Both are useful for skipping work on parts of a component that rarely change.

Why they're rejected today

Citry renders compiled Vue templates without Vue's render cache, and the parts needed for these directives don't line up yet:

  • Citry clears a component's cached render output only when its compiled template changes. A server response that only changes values would keep showing the old v-once / v-memo output, so a server re-render would not reset them.
  • Citry passes Vue its own cache array, so when Vue removes a cached node it clears the wrong entry (a v-once inside a toggled v-if would likely reuse a removed node — inferred from the code, not reproduced).
  • The browser runtime doesn't include the helpers these directives compile to (setBlockTracking, withMemo), and the compiler output check forbids them.

Proposal

Support both with this behavior: they follow Vue's rules between server re-renders, and any server re-render of the component resets them.

  1. Clear the component's render cache on every server update to that component.
  2. Use Vue's own per-instance cache array.
  3. Add the helpers to the browser runtime and allow them in the compiler output check.
  4. Tests: v-once/v-memo keep their output between client updates, reset after a server re-render, and behave inside toggled v-if and keyed lists; hydration still matches.

Activity

  1. JuroOravec commented on Oct 25, 2026

    @JuroOravec
    MemberAuthor

    Parked for now (low priority). This is the design note worked out so far, kept here so the work can resume from it. The two decisions at the end are still open.

    Syntax

    These are Vue's own directives, written as in any Vue template. Citry adds no syntax:

    <header v-once>{{ title }}</header>
    <li v-for="row in rows" :key="row.id" v-memo="[row.id, row.selected]">...</li>
    

    v-once takes no value, and v-memo takes an array expression read in the browser.

    Behavior

    • On the server: the first render writes the HTML as without the directive. Both only skip work on later client renders, and hydration compares the same nodes. The server renderer (server_render.rs) treats them as transparent, writing the element normally.
    • In the browser, between server updates: Vue's rules apply. A v-once part renders once per component instance. A v-memo part re-renders only when one of the values in its array changes (Vue's isMemoSame).
    • When the server re-renders the component: that resets both. Citry clears that component instance's render cache whenever a server update changes its prepared data or template, so v-once shows the new server values. This is the issue's rule, and it matches what a reader expects from "server is the source of truth".
    • The changes needed:
      • give each instance Vue's own cache array, so that a node removed inside a toggled v-if clears its own entry;
      • add setBlockTracking and withMemo to the browser runtime's helper contract (client.js HELPER_CONTRACT, runtime.js);
      • remove them, and _cache[, from the compiler output check (_FORBIDDEN_CODE in _vue/compiler.py).

    Error modes

    • v-memo with a value that is not an array: Vue's dev build warns; Citry raises at template load with the line, like other Vue syntax errors.
    • v-memo inside v-for without :key: Vue's compiler refuses it. Citry passes its message through citry check and raises at load.
    • v-once or v-memo on a <c-*> tag or a c- binding element: refused at load, because Citry renders those parts on the server and the browser cache would hide server updates. It names the tag and suggests moving the directive to a plain element.
    • v-memo around c- bindings or Events listeners: the memoized vnode keeps stale handler identities, so it is refused with the same message. Vue listeners in the browser are fine.
    • Hydration mismatch risk: none from the directives themselves. The server writes what Vue's first client render writes.

    Tests

    • Output is kept between client updates.
    • A server re-render resets it, including inside a toggled v-if and in keyed lists.
    • Hydration still matches.
    • The parity check covers pages using both directives.
    • The refusals above.

    Decisions for you

    1. What resets the cache: any server update to the component, or only one that changes its prepared data? I propose any update, which is simpler and matches the issue.
    2. Refusing v-memo around c- bindings and Events listeners: I propose refusing for now, since the memoized handlers would go stale; widen it later if needed.
  2. added
    next-sprint
    on Oct 25, 2026
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

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions