Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

HMR duplicating the app root when changing html or scss, working normal when changing ts ONLY #678

Closed
btmadison opened this issue Feb 14, 2017 · 5 comments

Comments

@btmadison
Copy link

if I change and save a typescript file, HMR works normally. Whenever I modify html or scss though, it keeps adding a new app root to the dom above the previous app root. Additionally the newly added app that is added does not have the properties defined in the app in the original index.cshtml. To explain more clearly, if i change html I can end up with the following:

<body> <app ng-version="2.4.5">...</app> <app prop-from-index="some-val" injected-token="somehash" ng-version="2.4.5">...</app>

when instead I would expect to always only see:

<body> <app prop-from-index="some-val" injected-token="somehash" ng-version="2.4.5">...</app>

which is the case if i change typescript only while HMR is active.

thanks...

@stmart76
Copy link

Also seeing this, did a fresh install just last night.

@dillenmeister
Copy link

Me too. I have the same problem if I change .ts files as well.

In boot-client.ts I added some code to remove the element and it seems to work but is not the correct solution to the problem...

if (module['hot']) {
    module['hot'].accept();
    module['hot'].dispose(() => {
        // Before restarting the app, we create a new root element and dispose the old one
        const oldRootElem = document.querySelector(rootElemTagName);
        const newRootElem = document.createElement(rootElemTagName);
        oldRootElem.parentNode.insertBefore(newRootElem, oldRootElem);
        platform.destroy(); // This doesn't seem to remove the <app> element. I assume is should?
        oldRootElem.parentNode.removeChild(oldRootElem); // Adding this line of code kind of works
    });
} else {
    enableProdMode();
}

@dillenmeister
Copy link

dillenmeister commented Feb 15, 2017

HMR works correctly with Angular 2.4.7 but not with Angular 2.0.2. See this commit

@stevetayloruk
Copy link

Please see: #665

@btmadison
Copy link
Author

I have resolved my issue by changing the code to the following:

const oldRootElem = document.querySelector(rootElemTagName);
const newRootElem = oldRootElem.cloneNode(false);
oldRootElem.parentNode.insertBefore(newRootElem, oldRootElem);

// tslint:disable-next-line:no-use-before-declare
platform.destroy();

instead of createElement, by changing it to clongNode(false) it retains the custom property attributes injected into the index.html. If you are not doing any custom app properties then this is unnecessary.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants