-
Notifications
You must be signed in to change notification settings - Fork 12k
HMR configuration - Live Reload still enabled #17324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey, I recently tried to run HMR on Angular and it worked fine... before upgrading Angular from My symptoms are that the HMR in WDS claims to be working, browser even properly detects HMR changes. The problem is that the changes don't seem to be downloaded. I didn't manage to nail down the issue :( Any idea why this could break? BTW, I know that the last official claim is that HMR in Angular is not supported, but @angular Team, are there any considerations about supporting HMR? Don't you use it internally? :) It's available OOB in Vue, React, Flutter etc. |
I found that adding
P.S. This may suggest that HMR entry point may have been removed in a new version of angular, but I'm not sure. |
After some discussion with the rest of the CLI team, the current way to use HMR with Angular isn't really supported and requires a very specific knowledge of the magic incantations to make work. We also haven't really tested this flow in some time, so how it plays with v9 and Ivy is somewhat unknown. We definitely want to support HMR better and that will take a good chunk of work. The best way to move forward is probably:
@alan-agius4, can you (or someone else) take a stab at the first two? |
I really appreciate your answer @dgp1130. It's worth adding that deprecated HMR solution only reinstantiates the whole app module which is not true HMR. True HMR works on component level and in the best case is able to hot-swap style or template-only changes of a component while preserving its state. I'd be really interested in following the topics you've listed. |
I also ran into the issue described by @damienix, and bisecting through my git history pointed to my upgrade from 9.0.7 to 9.1.0 as the cause of the hot reloading no longer working. I have been using the ngxs/hmr-plugin to get hmr working with angular, and have found it to be a great boon to my productivity as the browser reloads much faster than a full refresh, even if the state is not retained. I would really like to see an OOB hmr solution for angular in the cli. |
Hi @jadengis , after adding that ngxs/hmr-plugin do we need to alter something at angular side ? Yes we really need HMR in angular every framework has that feature hot reload without notice (silently) |
@indraraj26 The @ngxs/hmg-plugin no longer works with Angular 9.1. I'm not sure what happened on the Angular side, but it looks like the angular app is somehow ignoring the the hot reload. The webpack server still generates the hot update modules when you make a code change, but the angular app doesn't listen to that event and trigger the hot module reload. I'm not sure why because I'm not familiar with the CLI code. |
if it's in such an unstable state, why it's available in the it's part of doc in v9.1.3 |
@mhamri, that's part of the action items here. We need to figure out ourselves what a user should expect in v9 and how we can get it to a more consistent state. That might mean just adjusting documentation, it might mean it won't work and we should just remove the flag until it does, or it might mean there are some simple fixes we can make to get it back to a usable state. We still need to do the relevant investigation to figure out the best way of moving forward. |
@dgp1130 I'm totally fine with it. investigating and figuring out is part of software development. my concern is, if it's a research item on v9 and the angular team is aware of it then why it doesn't reflect in the documentation? not everyone refers to here to figure out what is working and what is not working. an update to the documentation would be nice and if you like I can make the PR |
I also vote for support HMR OOB for Angular CLI. I use HMR a lot during development because it speeds up significantly comparing to full page reload, especially when doing styling. |
@dgp1130 Is this something (HMR) which is in work now or we cannot expect HMR working with Angular 9+ any time soon? |
@damienix can you pls provide an example on how you solved it? with custom builder. I cant seem to make it work. The whole page still reloads. I even downgraded angular to 9.0.7 still any change css,html or code, reloads the whole app. |
@hhammoud do you have Ivy enabled? The solution with the builder still was not working properly for me with Ivy. |
Why can such a problem exist for so long and cannot be solved? I haven't got HMR to work normally since angular 4 |
The steps to reproduce are simple
|
@Y2zz, as per the warning when running
This is because to make HMR work for JS you will need to do some changes to your application based on which HMR modules you use example: https://www.npmjs.com/package/@angularclass/hmr, https://www.npmjs.com/package/@ngxs/hmr-plugin etc... I've setup a new application and configured it to use HMR using Here's the steps I followed:
Replace import { enableProdMode, NgModuleRef, ApplicationRef } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { createNewHosts } from '@angularclass/hmr';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
if (environment.production) {
enableProdMode();
}
declare var module: any;
async function hmrBootstrap(bootstrap: () => Promise<NgModuleRef<AppModule>>) {
module.hot.accept();
const ngModule = await bootstrap();
module.hot.dispose(() => {
const appRef = ngModule.injector.get(ApplicationRef);
const elements = appRef.components.map(c => c.location.nativeElement);
const makeVisible = createNewHosts(elements);
ngModule.destroy();
makeVisible();
});
}
const bootstrapCall = () => platformBrowserDynamic().bootstrapModule(AppModule);
if (module['hot']) {
hmrBootstrap(bootstrapCall);
} else {
bootstrapCall();
}
References: Based on the above, HMR seems to be working as expected. |
@alan-agius4 No it doesn't. It worked prior to Angular 9.1, but now the reload stucks all the time / has other quirks described above. Try it for yourself with more or less complex app (not just AppModule). |
@mtuzinskiy, while I do agree that we should document and support HMR better. We need a more information in which use-case HMR is not working for you. Your application structure and architecture might also effect the outcome of HMR. Without a reproduction that demonstrates the broken behaviour it's hard to tell what issue you are experiencing because of the number of variables that play a role in HMR, such as AOT/JIT compilation, Rendering engine (VE or Ivy), type of change, and application structure and architecture. I did however continue to look at this and in Ivy JIT mode, I get an error sometimes (angular/angular#35265);
This seems to be caused by the module.hot.dispose(() => {
const appRef = ngModule.injector.get(ApplicationRef);
const elements = appRef.components.map(c => c.location.nativeElement);
const makeVisible = createNewHosts(elements);
ɵresetCompiledComponents();
ngModule.destroy();
makeVisible();
}); Let me chime in @JoostK who did some HMR fixes in the past in the FW. |
@alan-agius4 This is exactly what happens for me now:
Except that I don't use @ngxs/hmg-plugin. I made HMR work in Angular 9 using the approaches that you have mentioned, but after update to Angular 9.1 the app started to ignore the hot reload.
https://github.com/angular/angular-cli/wiki/stories-configure-hmr |
The documentation in https://github.com/angular/angular-cli/wiki is for Angular CLI version 1 - 6 and hence the reason why that documentation is deprecated and no longer maintained. We'll be removing this documentation in the coming weeks because it's no longer relevant in many cases. At the time we created the new documentation, we felt that it's best not to provide documentation for 3rd party solutions. That being said, maybe we can offer a simple documentation on how to use HMR for most usecases or a schematic for some basic use cases when angular/angular#37474 API is public. Where eventually users will be able to enhance if they use state management libraries. I also think that in many cases the below should suffice: (Un-tested) import { enableProdMode, ɵresetCompiledComponents } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
if (environment.production) {
enableProdMode();
}
declare var module: any;
if (module['hot']) {
module['hot'].accept();
module['hot'].dispose(() => ɵresetCompiledComponents());
}
platformBrowserDynamic().bootstrapModule(AppModule); |
@alan-agius4 Schematic would be awesome! 👏 |
@alan-agius4 That |
The Failing to invoke this, will result in an error because the component will be referenced in multiple NgModules, the module instance before the change and the instance after the change. |
…ement (HMR) With this change we configure HMR internally and therefore users which want to use basic HMR functionality will not longer be required to change their application code. This is important because previously a lot of users missed out on HMR or reported a broken behaviour. This also gives novice users a better chance to appreciate HMR and Angular because of the zero effort required to use HMR. Closes #17324
@alan-agius4 I see you closed this ticket with a PR enabling HMR out of the box. Thanks so much for this! This is exactly what I've been looking for. I would just like to ask when can we expect to see this functionality in the CLI? |
This will be available in v11.0.0-next.4, which will should be released on NPM next Wednesday. When it’s released you can test it by updating your application to use the pre-release version using the below command
|
Does it mean it will be true HMR which will e.g. keep the state like in React or Vue or we will still be loosing state even without full page reload? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Hi all,
i've just tried to setup HMR for my angular 9 application- which is working fine for changes within my SCSS files (... stylesheets are updated without page reload).
But it doesn't work out for markup/component files - changes there still trigger a reload of the whole page.
I assume, that I've done sth. wrong, but I could not figure out what (due to this message during startup):
My configuration is done accordingly to the 'official' but deprecated docs: https://github.com/angular/angular-cli/wiki/stories-configure-hmr
angular.json:
package.json:
"hmr": "ng serve --configuration=hmr",
and env. file (src\environments\environment.hmr.ts)
I couldn't figure out, that there is sth. obvious wrong. Could you advise me there?
Thanks in advance!
The text was updated successfully, but these errors were encountered: