Skip to content

prerender: rehydrated components (pages) added twice to the DOM #2366

Closed

Description

@peterpeterparker

Stencil version:
"stencil/core": "^1.12.3"

I'm submitting a:
[X] bug report

Current behavior:
When I upgrade my apps to Stencil v1.12.3 and run a prerender build (npm run build --prerender), the components (pages) are added twice to the DOM!

I can confirm that if I run npm run start these are correctly added only once.

Not sure if the problem is linked to Stencil or the Ionic router, which I'm using.

Expected behavior:
Component added only once to the DOM.

Steps to reproduce:

I provide a sample repo.

git clone https://github.com/peterpeterparker/twice-component
npm run build

and then servewww with a local web server.
Screenshot:

Capture d’écran 2020-04-13 à 09 54 57

Activity

  1. changed the title [-]pretender add component (pages) twice to the DOM[/-] [+]prerender add component (pages) twice to the DOM[/+] on Apr 13, 2020
  2. changed the title [-]prerender add component (pages) twice to the DOM[/-] [+]prerender: component (pages) added twice to the DOM[/+] on Apr 13, 2020
  3. changed the title [-]prerender: component (pages) added twice to the DOM[/-] [+]prerender: rehydrated components (pages) added twice to the DOM[/+] on May 9, 2020
  4. Paul-Hebert commented on May 13, 2020

    @Paul-Hebert

    @peterpeterparker , were you able to resolve this in your project? Any info on a workaround? Thanks!

  5. peterpeterparker commented on May 13, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert I wasn't. Still unsure if something not correctly configured or "just" an issue in case of re-hydratation when the Ionic router is used.

    I actually reported it today and they (Ionic team) might have a look at it.

  6. Paul-Hebert commented on May 13, 2020

    @Paul-Hebert

    Thanks @peterpeterparker ! I'm also in the awkward spot of not knowing whether I'm doing something wrong or if there's a bug. Would you mind updating me if you learn more? I'll post back here if I can figure it out. Thanks!

  7. peterpeterparker commented on May 14, 2020

    @peterpeterparker
    ContributorAuthor
  8. Paul-Hebert commented on May 14, 2020

    @Paul-Hebert

    By the way, here's a simplified example that reproduces this issue for me:

    My simplified components

    import { Component, h } from "@stencil/core";
    
    @Component({
      tag: "c4-button",
      shadow: true,
    })
    export class C4Button {
      render() {
        return (
          <button>
            <slot />
          </button>
        );
      }
    }
    import { Component, h} from "@stencil/core";
    
    @Component({
      tag: "c4-page",
    })
    export class C4Page {
      render() {
        return (
          <div>
            <slot />
          </div>
        );
      }
    }

    My simplified document I'm hydrating

    <!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>
    </body>
    </html>

    My Node/Express code running the hydrate script

    app.engine("stencil-ssr", function (filePath, options, callback) {
      fs.readFile(filePath, function (err, content) {
        if (err) return callback(err);
    
        const htmlString = content.toString();
    
        return renderToString(htmlString).then((resp) => {
          return callback(null, resp.html);
        });
      });
    });

    (Though I see the same issue when prerendering instead of doing SSR)

    The output

    Note how there's a second button with an empty slot. It's invisible in this screenshot, but when I have button CSS applying padding/backgrounds, etc. it becomes visible:

    Screen Shot 2020-05-13 at 2 26 57 PM

    If I remove shadow DOM from the c4-button, I get this similar but different issue. Now the duplicate buttons are nested:

    Screen Shot 2020-05-13 at 2 36 35 PM

    If anyone could point me towards a solution or something I'm doing wrong I'd really appreciate it.
    I'd love to improve the performance of my Stencil app with preloading but this is a major blocker in that effort.

    Thanks! 😁

  9. Paul-Hebert commented on May 26, 2020

    @Paul-Hebert

    I just played with this again. In my actual app there are a number of different components with this issue, but there's not a clear pattern of why it happens. Sometimes it happens when it's just a wrapper div. When it happens these things seem to be consistent

    • the elements use Shadow DOM
    • the direct child of the Shadow DOM gets duplicated
    • the duplicate does not get any slot content (if there is a slot)
    • the duplicate does not get the classes Stencil adds during hydration (e.g. sc-c4-container)

    Hopefully that extra detail is helpful 🙂

  10. peterpeterparker commented on Jun 10, 2020

    @peterpeterparker
    ContributorAuthor

    I did some experiment and found something I documented on slack, just gonna copy it here it that can help.

    After build my dummy app with the ion-router and ion-nav, my index.html contains:

    <ion-nav s-id=14>
         <!--s.14.0.0.0.-->
         <app-home s-id=15>
    

    This leads to the rehydratation issue.

    But if I correct manually the elements rendering information as following:

    <ion-nav s-id=14>
         <!-- Remore comment, important, and add c-id attribut -->
         <app-home s-id=15 c-id=14.0.0.0>
    

    Then the problem is solved!

    Also, without removing the comment it would actually work too but then thenc-id has to be incremented

    <ion-nav s-id=14>
         <!--s.14.0.0.0.-->
         <app-home s-id=15 c-id=14.0.0.1>
    
  11. peterpeterparker commented on Jun 12, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert can you give a try to the PR @ #2508 I just provided?

    It does fix my issue but I would be really happy to hear some second thoughts and tests.
    It is my try out to bring the subject forward.

  12. added a commit that references this issue on Jun 17, 2020
  13. added a commit that references this issue on Jun 17, 2020
  14. peterpeterparker commented on Jun 17, 2020

    @peterpeterparker
    ContributorAuthor

    This should now be solved (any feedback appreciated).

    The PR #2517 has been merged.

  15. 10 remaining items

  16. peterpeterparker commented on Jun 24, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert cool, thx for the repo and details!

    I can reproduce the problem too and do see why too

    In the prerendered index.html the button is parser as following

    <c4-button class="sc-c4-button-h hydrated" s-id=2 c-id=0.2>
    

    but its parent is s-id=1. if I correct the c-id as following

    <c4-button class="sc-c4-button-h hydrated" s-id=2 c-id=1.2>
    

    then prerendering works out. so the question is why 0.2 instead of 1.2.

    I've to add some console.log to stencil, let me try

  17. peterpeterparker commented on Jun 24, 2020

    @peterpeterparker
    ContributorAuthor

    Ok so the PR I implemented kick in and calculate the right c-id but later it is overidden, not sure why yet

    [ INFO ]  Hydrate
               DAVID PR, C4-BUTTON, 1.1.0.1
    
    [ INFO ]  Hydrate
               LATER, C4-BUTTON, 0.2
    
  18. peterpeterparker commented on Jun 24, 2020

    @peterpeterparker
    ContributorAuthor

    Did not find the solution tonight. A bit busy with clients' projects next days but I'll comeback to it, I can reproduce and see where the heck is happening.

  19. Paul-Hebert commented on Jun 24, 2020

    @Paul-Hebert

    Awesome, thank you @peterpeterparker !

  20. peterpeterparker commented on Jun 26, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert can you open a new issue?

    I do have a solution but it breaks all tests, that's why I am a bit confuse. I do see with your test repo that the problem is this indexation and the solution is modifying 0.2 to 1.2 or 1.0.2. Therefore I do not what I am missing or if the indexing in the test can be updated. It needs input from the team, I do not want to submit a PR in which I modify all existing tests and break something.

    My solution would be the following. In vdom-annotations modify the hostId of the orgLocationNodes to not always be 0 be rather reflect the s-id of the node, if one was set during parseVNodeAnnotations.

    Is:

    if (hostId == null) {
              hostId = 0;
    

    My suggestion:

    if (hostId == null) {
         hostId = nodeRef.nodeType === NODE_TYPE.ElementNode && 
                       nodeRef.hasAttribute('s-id') 
                      && !isNaN(parseInt(nodeRef.getAttribute('s-id'))) ? 
                             parseInt(nodeRef.getAttribute('s-id')) - 1 : 0;
    

    What do you think, works for you?

  21. Paul-Hebert commented on Jul 1, 2020

    @Paul-Hebert

    Hey @peterpeterparker,

    Sorry for the delay. I've been out of the office doing some socially-distant dispersed camping in the woods. I'm focused on client work today but will try out that solution tomorrow and open a new issue.

    Thank you for all your help on this!

  22. peterpeterparker commented on Jul 1, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert no worries, busy too these days and next weekend too. Ping me when you've got time, no rush.

  23. guidoknoll commented on Jul 30, 2020

    @guidoknoll

    Hello,

    i'm running into that issue too. Any news to this problem?

    I'd be very thankful for a fix, sadly i can't really provide any help to the suggested solution above.

  24. Paul-Hebert commented on Aug 3, 2020

    @Paul-Hebert

    Hey @peterpeterparker , thanks again for all your help here! Sorry for the super long delay in trying this out.

    I think this fixed the issue, but I had to test it in a kind of roundabout way.

    I was planning to do the following:

    1. Check out the stencil core repo
    2. Make your change
    3. Build the project
    4. Copy the build output into my bug repro repo, replacing my existing copy of Stencil core.

    However, it's not clear to me the correct way to build the stencil core project. I tried build, build.prod and release.prepare. Each of these generated a /build directory with a different structure than I expected. It didn't match the structure of the Stencil Core in my project's Node Modules folder.

    I feel like I'm probably missing something obvious there, so if you could provide some guidance on the proper way to build that I can try it out that way.

    What I Did Instead

    I ended up making this change directly in the package in my Node Modules package. When I made the change here it appears to fix my problem! 🎉

    (I had to switch NODE_TYPE.ElementNode to 1 for this to work. Based on inline comments it looks like this change is automatically made during compilation.)

    Next Steps

    Let me know if there's anything else you'd like me to do to test this.

    You also mentioned creating a new issue about this. Is there any specific info I should include there? I was planning to include the following:

    1. A description of the issue
    2. Screenshots
    3. A link to my bug repro repo
    4. A link to your comment describing a fix.

    Thanks David!

  25. peterpeterparker commented on Aug 4, 2020

    @peterpeterparker
    ContributorAuthor

    @Paul-Hebert nice to hear from you and happy to hear that you were able to try it out.

    I am not sure anymore if I use the build or build:pro command.

    Anyway yes I think we should open a new issue, because this one begins to be quite long and as I said in the workaround, it breaks all tests therefore it definitely needs inputs from the Ionic/Stencil team.

    Regarding content of the issue, I think what you describe is what's needed. Please do post the link to the new issue in this one, so I can follow ;)

    And thx in advance 👍

  26. Paul-Hebert commented on Aug 5, 2020

    @Paul-Hebert

    Here you go @peterpeterparker: #2611

    Thanks again for all your help!

  27. peterpeterparker commented on Aug 5, 2020

    @peterpeterparker
    ContributorAuthor
  28. CollinHerber commented on Jan 31, 2023

    @CollinHerber

    I just upgraded to Stencil 3.0 and I am noticing this issue. On the created link I am not seeing any resolution. Has there been any further development on this?

    Edit: One thing to point out is there is only a single container element created.
    This is what is observed

    <my-component> 
        <div> component markup </div>
        <div> component markup </div>
    </my-component>

    So based on this it does appear to be during the hydration of the component.

    Edit2:
    Further investigation suggests that this is only happening for component that have nested stencil components.
    So the hydrated markup looks something like this

    <my-component> 
        <nested-component>
            <div> component markup </div>
            <div> component markup </div>
        </nested-component>
    </my-component>
    
    <nested-component>
          <div> component markup </div>
    </nested-component>
  29. rwaskiewicz commented on Feb 9, 2023

    @rwaskiewicz
    Member

    @CollinHerber can you do me a favor and please create a new issue with a reproduction case for the team and myself?

  30. CollinHerber commented on Feb 9, 2023

    @CollinHerber

    We ended up finding out that switching our components to shadow:true fixed the issue and have been rolling with the large amount of changes that creates for us with switching to shadow dow. I hope to have some time in the next week or two to get that to you but we are coming up to release time here and it's very busy. I will tag you when that happens though.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions