Skip to content

SSR Hydration resulting in duplicate elements #2611

Closed

Description

@Paul-Hebert

Stencil version:

I'm submitting a:

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

When using the generated hydrate script in an SSR-rendered app some elements are incorrectly duplicated. (The blue square to the right of my button is a duplicated, empty button)

incorrect

Expected behavior:

Hydrated components would only render once.

correct

Steps to reproduce:

I've had trouble narrowing this down to a specific cause. You can reproduce it using this repo I put together: https://github.com/cloudfour/stencil-ssr-bug-reproduction

This is a simplified version of a project I'm working on which is why it is structured the way it is. I have a subdirectory housing my stencil components. Adjacent to that I have another subdirectory housing my express app. From the root directory you can run a number of commands.

In order to build my express app, I build my stencil components, copy the output into the express app, and then run the express app.

  1. Check out that repo
  2. Install dependencies for the components: cd stencil-components, npm i, cd .. (from root)
  3. Install dependencies for my app: cd express-app, npm i, cd .. (from root)
  4. Build my components and copy them into my express app npm run build (from root)
  5. Run my express app: npm run start:express (from root)
  6. Navigate to express app (http://localhost:3000/)
  7. See that the button has been duplicated

I had also reproduced this using the prerender option in my project, but didn't set up a sample repository for that. Let me know if that would be helpful.

Related code:

See repro directory above

Other information:

I originally reported this as a comment on #2366 . @peterpeterparker was kind enough to take a look and help me debug it. He suggested a change to vdom-annotations which seemed to work for me: #2366 (comment)

He asked me to open a new issue since his original issue had been resolved.

Thanks for the great library! I love working with Stencil and am excited to get my SSR working! Let me know if you need any other info from me 🙂

Activity

  1. chenyulun commented on Dec 7, 2020

    @chenyulun

    "@stencil/core": "^2.3.0",
    If the parent tag is larger than the child tag, the child tag repeats,for example

    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="/stencil/stencil-components.js"></script>
    </head>
    
    <body>
      <c4-page>
          <c4-button>I'm a c4-button!</c4-button>
      </c4-page>
      <c4-q>
          <c4-page>I'm a c4-page!</c4-page>
      </c4-q>
      <c4-button>
          <c4-page>I'm a c4-page!</c4-page>
      </c4-button>
      <c4-button>
          <c4-q>I'm a c4-q!</c4-q>
      </c4-button>
    </body>
    </html>

    Tag size relationship: c4-q > c4-page > c4-button
    The tag that causes the error is nested as follows:

    <c4-page>
          <c4-button>I'm a c4-button!</c4-button>
      </c4-page>
    <c4-q>
          <c4-page>I'm a c4-page!</c4-page>
      </c4-q>

    The tags that can be executed correctly are nested as follows:

    <c4-button>
          <c4-page>I'm a c4-page!</c4-page>
      </c4-button>
    <c4-button>
          <c4-q>I'm a c4-q!</c4-q>
      </c4-button>

    First of all, record the scene of repetition. If I have time, I will study the reason(先记录一下复现的场景,有空的话我会去研究一下原因)

  2. chenyulun commented on Dec 8, 2020

    @chenyulun

    Repair methods

    Add a judgment to 'if (childNodeType)' on 'clientHydrate'. The current 'parentVNode' node will not be added to it
    (在clientHydrate方法上的添加一个if (childNodeType)上添加一个判断 if (hostElm !== node && childNodeType),当前node节点不加入到当前parentVNode节点上)

    const clientHydrate = (parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node, hostId) => {
        let childNodeType;
        let childIdSplt;
        let childVNode;
        let i;
        if (node.nodeType === 1 /* ElementNode */) {
            childNodeType = node.getAttribute(HYDRATE_CHILD_ID);
            if (hostElm !== node && childNodeType) { // // Waiting for validation
        }
    // ...
    }

    I don't know if there will be other effects

    by the way: find a bug

    1. c-id does not have $index$, Is this the slot tag content?If so, there is no need to deal with their children
    2. If the children are in custom tags or slots, there is no need to iterate over their children,Can improve performance
    parentVNode.$children$[childVNode.$index$] = childVNode; // childVNode.$index$ === undefined
     <c4-page class="hydrated" s-id="1">
                <!--r.1-->
                <!--o.0.2-->
                <div c-id="1.0.0.0">
                    <!--s.1.1.1.0.-->
                    <c4-button class="sc-c4-button-h hydrated" s-id="2" c-id="0.2">
                        <!--r.2-->
                        <!--o.0.4.-->
                        <button class="sc-c4-button sc-c4-button-s" c-id="2.0.0.0">
                        <!--s.2.1.1.0.-->
                        <!--t.0.4-->
                        I'm a c4-button!</button>
                    </c4-button>
                </div>
            </c4-page>
    // parentVNode = <div c-id="1.0.0.0"></div>
    // childVNode = <c4-button class="sc-c4-button-h hydrated" s-id="2" c-id="0.2"></div>
    const clientHydrate = (parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node, hostId) => {
        let childNodeType;
        let childIdSplt;
        let childVNode;
        let i;
        if (node.nodeType === 1 /* ElementNode */) {
            childNodeType = node.getAttribute(HYDRATE_CHILD_ID);
            if (hostElm !== node && childNodeType) {
                // got the node data from the element's attribute
                // `${hostId}.${nodeId}.${depth}.${index}`
                childIdSplt = childNodeType.split('.');
                if (childIdSplt[0] === hostId || childIdSplt[0] === '0') {
                    childVNode = {
                        $flags$: 0,
                        $hostId$: childIdSplt[0],
                        $nodeId$: childIdSplt[1],
                        $depth$: childIdSplt[2],
                        $index$: childIdSplt[3],
                        $tag$: node.tagName.toLowerCase(),
                        $elm$: node,
                        $attrs$: null,
                        $children$: null,
                        $key$: null,
                        $name$: null,
                        $text$: null,
                    };
                    childRenderNodes.push(childVNode);
                    node.removeAttribute(HYDRATE_CHILD_ID);
                    // this is a new child vnode
                    // so ensure its parent vnode has the vchildren array
                    if (!parentVNode.$children$) {
                        parentVNode.$children$ = [];
                    }
                    // add our child vnode to a specific index of the vnode's children
                   // childVNode.$index$ === undefined,
                    parentVNode.$children$[childVNode.$index$] = childVNode;
                    // this is now the new parent vnode for all the next child checks
                    parentVNode = childVNode;
                    if (shadowRootNodes && childVNode.$depth$ === '0') {
                        shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
                    }
                }
            }
          if(hostElm !== node && (node.componentOnReady || (childVNode && !childVNode.$index$))) return; // Waiting for validation
            // As the parent traverses, it is possible to delete the C-ID in the slot content element of the child node (custom tag),
            // recursively drill down, end to start so we can remove nodes
            for (i = node.childNodes.length - 1; i >= 0; i--) {
                clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.childNodes[i], hostId);
            }
            if (node.shadowRoot) {
                // keep drilling down through the shadow root nodes
                for (i = node.shadowRoot.childNodes.length - 1; i >= 0; i--) {
                    clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.shadowRoot.childNodes[i], hostId);
                }
            }
        }
  3. chenyulun commented on Dec 8, 2020

    @chenyulun

    One more question:
    I mentioned earlier that the order in which tags are executed causes problems,

    <c4-page><c4-button></c4-button><c4-page>

    Somehow, the small tag is always executed first, and then the large tag is executed, for example,c4-button > c4-page;
    This small tag reads its own c-id and if into the child vNode;
    If the label order is as follows

    <c4-button><c4-page></c4-page></c4-button>

    First execute the small tag ,, and delete the c-id of when changing the quantum node. In this way, will not be added to vnode as a child node when traversing itself again,

  4. chenyulun commented on Dec 8, 2020

    @chenyulun

    @peterpeterparker Come and have a look

  5. chenyulun commented on Dec 9, 2020

    @chenyulun

    new bug: shadow:false,The default content in the slot element cannot be processed correctly

    <body>
      <c4-page>
      </c4-page>
    </body>
    import { Component, h, Prop} from "@stencil/core";
    
    @Component({
      tag: "c4-page",
      // shadow: true,
    })
    export class C4Page {
      @Prop() testStr = 'testStr'
      render() {
        return (
          <div>
            <slot>
        <p><span>insade def slot{this.testStr}</span></p>
            </slot>
            <slot name="test"><span>insade test slot</span></slot>
          </div>
        );
      }
    }
    index-b6879c1b.js:1227 Uncaught (in promise) DOMException: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.
        at renderVdom (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1227:43)
        at updateComponent (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1348:17)
        at http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1324:32
        at then (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1547:61)
        at dispatchHooks (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1324:12)
        at Array.dispatch (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1292:28)
        at consume (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:2747:21)
        at flush (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:2800:9)
  6. chenyulun commented on Dec 9, 2020

    @chenyulun

    new bug: shadow:false,The default content in the slot element cannot be processed correctly

    <body>
      <c4-page>
      </c4-page>
    </body>
    
    import { Component, h, Prop} from "@stencil/core";
    
    @Component({
      tag: "c4-page",
      // shadow: true,
    })
    export class C4Page {
      @Prop() testStr = 'testStr'
      render() {
        return (
          <div>
            <slot>
        <p><span>insade def slot{this.testStr}</span></p>
            </slot>
            <slot name="test"><span>insade test slot</span></slot>
          </div>
        );
      }
    }
    index-b6879c1b.js:1227 Uncaught (in promise) DOMException: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.
        at renderVdom (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1227:43)
        at updateComponent (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1348:17)
        at http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1324:32
        at then (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1547:61)
        at dispatchHooks (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1324:12)
        at Array.dispatch (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:1292:28)
        at consume (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:2747:21)
        at flush (http://localhost:3000/stencil/stencil-components/index-b6879c1b.js:2800:9)

    fix

    // initializeClientHydrate(elm, cmpMeta.$tagName$, hostId, hostRef);
    initializeClientHydrate(elm, cmpMeta.$tagName$.toUpperCase(), hostId, hostRef);
  7. chenyulun commented on Dec 9, 2020

    @chenyulun

    In the non-shadow elements, during the generation of vnodes, slot-fb can be resolved into slot, so that there is no need to regenerate slot-fb elements when comparing rootvnodes to improve performance

    fix code

           if (hostElm !== node && childNodeType) {
                // got the node data from the element's attribute
                // `${hostId}.${nodeId}.${depth}.${index}`
                childIdSplt = childNodeType.split('.');
                if (childIdSplt[0] === hostId || childIdSplt[0] === '0') {
                    let tag = node.tagName.toLowerCase()
                    let name = null
                    if (!hostElm.shadowRoot && tag === 'slot-fb') {
                        tag = 'slot'
                        name = node.getAttribute('name') || null;
                    }
                    childVNode = {
                        $flags$: 0,
                        $hostId$: childIdSplt[0],
                        $nodeId$: childIdSplt[1],
                        $depth$: childIdSplt[2],
                        $index$: childIdSplt[3],
                        $tag$: tag,
                        $elm$: node,
                        $attrs$: name ? { name } : null,
                        $children$: null,
                        $key$: null,
                        $name$: name,
                        $text$: null,
                    };
              // other code ...
            }
          }
  8. added
    ionitron: stale issue
    on Jan 8, 2021
  9. ionitron-bot commented on Jan 8, 2021

    @ionitron-bot

    Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Stencil, please create a new issue and ensure the template is fully filled out.

    Thank you for using Stencil!

  10. locked and limited conversation to collaborators on Jan 8, 2021
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

    ionitron: stale issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions