Skip to content

Neither connectedCallback nor disconnectedCallback should be called when re-mapping causes a shuffle. #254

Open

Description

@theengineear

Conceptually, if you render [foo, bar] and then re-render [bar, foo], neither of those elements should be disconnected at all. However, because low-level DOM apis will cause a disconnect followed by a connect — there’s no great way to perform this shuffle without causing at least one disconnection.

A new API is being proposed called moveBefore — whatwg/dom#1307. And, Chrome has begun implementing it — https://chromestatus.com/feature/5135990159835136. This should directly address the shuffle-disconnect issue.

Activity

  1. theengineear commented on Feb 18, 2025

    @theengineear
    CollaboratorAuthor

    Note — we should look into adding a stub for connectedMoveCallback in the XElement base class once this all lands.

  2. theengineear commented on Oct 15, 2025

    @theengineear
    CollaboratorAuthor

    Firefox now supports moveBefore as well — https://www.firefox.com/en-US/firefox/144.0/releasenotes/

  3. klebba commented on Dec 6, 2025

    @klebba
    Contributor

    @theengineear I noticed

    x-element/x-template.js

    Lines 428 to 479 in c332566

    // TODO: #254: Future state where the “moveBefore” API is better-supported.
    // // Loops over given array of “entries” to manage an array of nodes.
    // static #commitContentArrayEntries(node, startNode, entries) {
    // const arrayState = TemplateEngine.#getState(startNode, TemplateEngine.#ARRAY_STATE);
    // if (!arrayState.map) {
    // // There is no mapping in our state — create an empty one as our base.
    // TemplateEngine.#clearObject(arrayState);
    // arrayState.map = new Map();
    // }
    //
    // const idsToRemove = new Set(arrayState.map.keys());
    // const ids = new Set(); // Populated in “parseArrayEntry”.
    // let reference = startNode.nextSibling;
    // for (let index = 0; index < entries.length; index++) {
    // const entry = entries[index];
    // const [id, rawResult] = TemplateEngine.#parseArrayEntry(entry, index, ids);
    // let item = arrayState.map.get(id);
    // if (item) {
    // // Update existing item.
    // idsToRemove.delete(id);
    // if (!TemplateEngine.#canReuseDom(item.preparedResult, rawResult)) {
    // const referenceWasStartNode = reference === item.startNode;
    // TemplateEngine.#recreateArrayItem(item, rawResult);
    // reference = referenceWasStartNode ? item.startNode : reference;
    // } else {
    // TemplateEngine.#update(item.preparedResult, rawResult);
    // }
    // } else {
    // // Create new item.
    // item = TemplateEngine.#createArrayItem(node, id, rawResult);
    // arrayState.map.set(id, item);
    // }
    // // Move to the correct location
    // if (item.startNode !== reference) {
    // const nodesToMove = [item.startNode];
    // while (nodesToMove[nodesToMove.length - 1] !== item.node) {
    // nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling);
    // }
    // TemplateEngine.#moveAllBefore(reference.parentNode, reference, nodesToMove);
    // }
    //
    // // Move our position forward.
    // reference = item.node.nextSibling;
    // }
    //
    // // Remove any ids which are not longer in the entries.
    // for (const id of idsToRemove) {
    // const item = arrayState.map.get(id);
    // TemplateEngine.#removeThrough(item.startNode, item.node);
    // arrayState.map.delete(id);
    // }
    // }
    commented out in the source — should we move that code into a PR ahead of the 2.0 publish? Or is it ready for prime time?

  4. theengineear commented on Dec 6, 2025

    @theengineear
    CollaboratorAuthor

    It’s officially in the specification (and has been on my Christmas list all year)… it appears to be supported by Chrome and Firefox (but not yet Safari).

    I can open up a PR and we can watch it… I suppose.

    👏 but 👏 we’re 👏 so 👏 close 👏

  5. theengineear commented on Dec 6, 2025

    @theengineear
    CollaboratorAuthor

    It does pass in FF Nightly now (but not latest, yet).

  6. added 4 commits that reference this issue on Dec 6, 2025
    a574389
    71a4955
    28e0b44
    c6c089f
  7. theengineear commented on Dec 8, 2025

    @theengineear
    CollaboratorAuthor

    @klebba — feel free to also pile on to that Safari bug ticket if you want :^)

    Seems like there has not been much action on that thread in a while.

  8. theengineear commented on Apr 15, 2026

    @theengineear
    CollaboratorAuthor

    Looks like there is a person assigned to that bug ticket now and things are being updated 🤞

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