Skip to content

Angular 19: Issue with Style Flickering When Switching Views (ng serve) #59058

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

Closed
grzegorzszymonik opened this issue Dec 4, 2024 · 29 comments
Closed
Assignees
Labels
area: core Issues related to the framework runtime core: hot module replacement (HMR)
Milestone

Comments

@grzegorzszymonik
Copy link

grzegorzszymonik commented Dec 4, 2024

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

Yes

Description

Hello everyone,

The problem occurs when starting the project via npm run start (i.e. ng serve). When the project is built, the problem does not occur.

I'd like to inform you about a problem I've encountered after updating to Angular 19. When navigating from one view to another and back, I've noticed a flickering effect related to the styles of a specific component. As the component loads, it appears without any styles for a split second before the styles are applied and everything returns to normal.

This issue did not occur in Angular 18.

How to reproduce the problem:

Create a new project in Angular 19.
Create two routable components, e.g., view1 and view2.
Add HTML elements to each component, such as <h1>, <p>, and a with a routerLink navigating between view1 and view2.
Add styles to these components, like background, font-size, etc.
Run the application and navigate to the main page. If you refresh the page directly or using the refresh button, everything works correctly.
Navigate using the button with routerLink to the second view and back. You'll notice that for a moment, the components are displayed without styles, and then the styles load.

Additional Information:
Browser: Google Chrome 131.0.6778.70

Example in stackblitz: https://stackblitz.com/edit/stackblitz-starters-rb8wat

Seems like this bug is bound to the HMR of stylesheets cause once built, there is no bug anymore.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-rb8wat

Please provide the exception or error you saw

When navigating from one view to another and back, I've noticed a flickering effect related to the styles of a specific component. As the component loads, it appears without any styles for a split second before the styles are applied and everything returns to normal. As if there was a delay in loading the styles.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.0.3
Node: 20.13.1
Npm: 8.3.0

Angular: 19.0.3

@angular-devkit/build-angular: "19.0.3",
@angular/cli: "19.0.3",
@angular/compiler-cli: "19.0.3",

Anything else?

No response

@AndrewKushnir AndrewKushnir added core: hot module replacement (HMR) area: core Issues related to the framework runtime labels Dec 4, 2024
@ngbot ngbot bot added this to the needsTriage milestone Dec 4, 2024
@shannonvc
Copy link
Contributor

shannonvc commented Dec 5, 2024

We also had this in our project.
It is indeed caused by the way HMR loads styles seperately from the components.
In the repro link, running the app with ng serve --no-hmr (or changing he npm start script to include the flag) will work as it did before.

@otodockal
Copy link
Contributor

I just wanted to report this issue and have created a demo repository to reproduce it: https://github.com/otodockal/ng-host.

In my case, the flickering occurs when combining styles declared in :host and HMR (which is default now).

ng-host.mov

@WebTechnolog
Copy link

I encounter the same issue. And it also strangely affects mat-table with sticky columns.
I have a table in a dialog and 2 sticky columns positioned next to each other. But with this issue there are gaps between these columns. Their left positions are not properly calculated.
ng serve --no-hmr fixes the issue, but HMR is a cool feature of v19, and would be great to have it fixed.

@joewIST
Copy link

joewIST commented Dec 8, 2024

I am experiencing the same issue even with HMR off.

@e-oz
Copy link

e-oz commented Dec 8, 2024

@joewIST then it, perhaps, is a different bug and requires a different reproduction example.

@joewIST
Copy link

joewIST commented Dec 8, 2024

All I can see is that on v19, when a component is lazy loaded, there is an asynchronous request to load that component's stylesheet (even if it's already been loaded):

Image

This wasn't happening on v18:

Image

@RafalSzczuka
Copy link

It is worth noticing that this issue does not occur in Firefox browser. Turning off hmr flag helps for me as well.

@joewIST
Copy link

joewIST commented Dec 9, 2024

We still get the flickering on Firefox (which occurs while the styles are loaded). The only difference is that on Firefox, once the styles have been loaded, they are not reloaded whenever the component is reused. On Edge/Chrome, they seem to be loaded every time the component is requested, which causes the flicker to occur a lot.

For reference, this is only happening when a) a component is dynamically rendered (e.g. something in a dropdown that gets initialised when the dropdown is opened or b) a component is wrapped in an @if block and is only rendered after a user interaction (e.g. a loading state in a button).

@joewIST
Copy link

joewIST commented Dec 10, 2024

I have been able to remove the flickering, based on the asynchronous loading of styles, but for our project it only works when I set the "live-reload" flag to false. The "hmr" flag seems to have no impact. When I serve the application with this flag set, the styles are working as they did before upgrading to v19.

Obviously, there is a cost with this, as the app requires a manual refresh for any changes to be reflected, so it would be good to have this fixed!

Thanks

@hakimio
Copy link

hakimio commented Dec 10, 2024

@joewIST are you testing with --hmr false or --no-hmr flag?

@joewIST
Copy link

joewIST commented Dec 10, 2024

@hakimio I have tried it with both and neither have any impact. Like I said, the only thing that has made a difference is turning live reload off...

@AndSokolov
Copy link

AndSokolov commented Dec 11, 2024

I also created a similar issue #59129.
Another way to fix this is to replace builder in angular.json with "builder": "@angular-devkit/build-angular:browser"

@ZixZux
Copy link

ZixZux commented Dec 11, 2024

I have the same issue.

But it's the smallest of the problem - the key problem is that now ngAfterViewInit is called before the styles of component are loaded.

It's very huge impact on the application.

I've created separated issue: "Angular 19: Component initialization logic is broken by HMR #59151".

SebastianPodgajny added a commit to SebastianPodgajny/nx that referenced this issue Dec 12, 2024
For Angular > 19 schema hmr option was ignored, it was impossible to disable hmr as default value changed in ng19
angular/angular#59058
@SebastianPodgajny
Copy link

SebastianPodgajny commented Dec 12, 2024

FYI
Nx dev-server ignores hmr option for Angular@19
https://github.com/nrwl/nx/blob/master/packages/angular/src/builders/dev-server/lib/normalize-options.ts#L24
possible fix nrwl/nx#29322

leosvelperez pushed a commit to nrwl/nx that referenced this issue Dec 12, 2024
For Angular > 18 schema hmr option was ignored, it was impossible to
disable hmr and default value changed in ng19
angular/angular#59058

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
@JeanMeche JeanMeche marked this as a duplicate of #59199 Dec 16, 2024
russell-mason added a commit to russell-mason/clocks that referenced this issue Dec 16, 2024
- Basic update to Angular 19
- Minor SCSS updates for deprecated lighten/darken
- Moved the showing of the responsive overlay to config (off by default)
- Removed yarn file (redundant - left over from long past experiments)
- Temporarily disabled HMR due to to an open issue (angular/angular#59058)
@Tom-ITietz
Copy link

Hi Everyone i got the same Issue even after Deployment using ng build

@avicarpio
Copy link

Also having this issue.

@JeanMeche
Copy link
Member

Important

To help us investigate, please provide the specific version of the software you're using. This will help us pinpoint the issue more effectively

@abdurrahmanarikan
Copy link

abdurrahmanarikan commented Dec 28, 2024

Using v18 on my mac without nx, it happens when the main page is loading. After the loading, whenever I press refresh (f5) it also happens

@muhamedkarajic
Copy link

muhamedkarajic commented Dec 29, 2024

Under here I made a minimal reproduction when using the flag npm run serve and npm run serve-no-hmr the issue is obvious cause when hmr is enabled the red circle moves when opening the page or refreshing it while its not like that without hmr. The red circle is in my case a navbar and its not a great dev experience that the style transitions. I think that should somehow get patched if possible cause by me even it causes the smooth scroll to not work properly cause there is a layout shift.

All details of my OS, etc I wrote already under:
#59318

Update: When building it and running it from a server its not happening. Its purly in my case solved by not using hmr.

NOTE: On my website, when transitioning pages I have the hmr issue as well. So this is NOT only when opening the page for the first time and refreshing but also when navigating trough the pages e.g. home, about, etc.

@muhamedkarajic
Copy link

muhamedkarajic commented Dec 29, 2024

I spent so far 7h on this, updating node version, adding back standalone, making a open source version so someone can reproduce it, writing down everything.

I don't mind the time. However I'm afraid of the frameworks reputation cause people will update, devs will experience this issue and be lost. Maybe keep v18 again as I did in the first place.

I get that the milions of tests, that google has, has not catched this issue cause its a purly developer experience issue but on the other hand I never ever saw a developer updating the hole project and then first running its tests before opening the page once and looking at it. Maybe at least in some projects the tests have to be also executed serving the project not building it then this bug would have been noticed.

Please revert the HMR as a default or fix the issue but this can not stay until v20 like this, not even until 19.1 I think.

@muhamedkarajic
Copy link

muhamedkarajic commented Dec 29, 2024

FYI Nx dev-server ignores hmr option for Angular@19 https://github.com/nrwl/nx/blob/master/packages/angular/src/builders/dev-server/lib/normalize-options.ts#L24 possible fix nrwl/nx#29322

I have Nx v20, yesterday updated using latest flag. Had the issue as everyone else. So its not true that its ignoring it. At least not right now.

@CryshotX
Copy link

CryshotX commented Jan 6, 2025

I am experiencing the same issue.

Since I updated my project to Angular v19, I am experiencing style flickering for a split second, everytime I am navigating my application using the internal angular router. It also happens when refreshing the app with F5.

Disabling HMR fixes the issue, although I'd really like to use it.

@mk-16
Copy link

mk-16 commented Jan 6, 2025

i openened an issue, but was immidiatly closed because of duplication....
ViewContainerRef createComponent UI Flickering #59372

there, i point out source/underlying issue & quick workaround without compromising on project, enjoy

@hakimio
Copy link

hakimio commented Jan 15, 2025

This late/lazy HMR style loading breaks Material Sidenav. mat-sidenav-content margin is calculated incorrectly.

HMR enabled:

with style HMR 1

HMR disabled:

without HMR

The demo project with steps to reproduce the issue: Material Sidnav style HMR bug

@clydin it's crazy that some 6 year old bugs are not fixed because the fix causes Google internal tests to break. Yet it feels like this new style HMR was enabled by default without any testing at all.
Is there a fix coming anytime soon or is this considered a low priority issue?

@jamesstztu
Copy link

The issue went away for me when I turned hmr off.
You can do it via ng serve --hmr=false
or update your angular.json
"hmr": off

@vinuram1729
Copy link

vinuram1729 commented Jan 16, 2025 via email

@AndSokolov
Copy link

As an alternative you can change your builder in angular.json to "builder": "@angular-devkit/build-angular:browser". And the flickering will disappear.

@alan-agius4
Copy link
Contributor

Thanks for reporting this issue. Luckily, it has already been fixed in one of the recent releases. Please update to the most recent version to resolve the problem.

isaacplmann pushed a commit to nrwl/nx that referenced this issue Feb 11, 2025
For Angular > 18 schema hmr option was ignored, it was impossible to
disable hmr and default value changed in ng19
angular/angular#59058

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: hot module replacement (HMR)
Projects
None yet
Development

No branches or pull requests