Title: NgOptimizedImage makes the browser select each image source again during hydration · Issue #71873 · angular/angular · GitHub
Open Graph Title: NgOptimizedImage makes the browser select each image source again during hydration · Issue #71873 · angular/angular
X Title: NgOptimizedImage makes the browser select each image source again during hydration · Issue #71873 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? No Description During hydration, NgOptimizedImage makes the browser select the source of each image again. There are two separate causes. The directive w...
Open Graph Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? No Description During hydration, NgOptimizedImage makes the browser select the source of each image again. There ...
X Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? No Description During hydration, NgOptimizedImage makes the browser select the source of each image again. There ...
Opengraph URL: https://github.com/angular/angular/issues/71873
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NgOptimizedImage makes the browser select each image source again during hydration","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\ncommon\n\n### Is this a regression?\n\nNo\n\n### Description\n\nDuring hydration, `NgOptimizedImage` makes the browser select the source of each image again. There are two separate causes.\n\n**The directive writes its attributes again.** `setHostAttributes()` calls `renderer.setAttribute()` for `src`, `srcset`, `sizes`, and six other attributes. The server rendered the same values. A draft pull request skips the writes when the element already has the value.\n\n**The value of `sizes` changes twice on a lazy image.** The directive prepends `auto` to `sizes` on lazy images, so the value that it computes differs from the template. This happens only on a lazy image with a static `sizes` attribute:\n\n```text\nserver HTML sizes=\"auto, 50vw\"\nhydration, core sizes=\"50vw\" setupStaticAttributes() writes the template value\nhydration, directive sizes=\"auto, 50vw\" setHostAttributes() prepends auto again\n```\n\nThe directive computes the same value on the server and in the browser, but core's write runs before the directive exists. Each of these two writes really changes the value, so checking for an unchanged value doesn't remove them.\n\nThe browser also fires `load` again on images that loaded before hydration. Event replay delivers the original `load` event too, so a `(load)` handler runs twice on those images.\n\nOn a page with 200 images, where hydration starts after the window `load` event, hydration takes 35.0 ms today and 27.7 ms with the draft pull request. When the same page uses `[width]`, `[height]`, and `[sizes]` bindings, which core doesn't write to the DOM, hydration takes 9.9 ms with the draft pull request.\n\n### Please provide a link to a minimal reproduction of the bug\n\nhttps://github.com/jsaguet/ngimg-hydration-repro\n\nTo see the `(load)` handler run twice, run `npm ci`, `npm run build:variants`, and `npm run serve:stock`, and open `http://localhost:4000/?bootAfterLoad`. A badge under each image counts the calls. The README has the measurements.\n\n### Please provide the exception or error you saw\n\nNo exception.\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```\nAngular CLI : 22.2.6\nAngular : 22.2.5\nNode.js : 26.11.1\nPackage Manager : npm 11.20.0\nChrome : 156\n```\n\n### Anything else?\n\nUntil the `sizes` case is fixed, bind the input, for example `[sizes]=\"'50vw'\"`, so that core doesn't write it to the DOM.\n\nI don't have a proposal for the `sizes` case that I'm confident about. These are the options I looked at:\n\n- Don't prepend `auto` when the template already has it, so that a developer can write `sizes=\"auto, 50vw\"`. This is one condition in the directive. It helps only the templates that change.\n- Make the check in core treat `auto, X` in the DOM as equal to `X` in the template. This covers existing templates, but core would then know about something that the directive does.\n- Let a directive declare the host attributes that it writes itself, so that core doesn't write them. This covers existing templates, and it adds an internal mechanism for a single attribute.\n\nWhich direction would you prefer, if this case is worth fixing?\n","author":{"url":"https://github.com/jsaguet","@type":"Person","name":"jsaguet"},"datePublished":"2026-11-08T12:29:29Z","url":"https://github.com/angular/angular/issues/71873"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:53c32c1e-4c47-51a0-bc47-3bc7edaafc52 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1E8:EA06E:3970048:4901A92:6B01DEDD |
| html-safe-nonce | 63490f612d7d90ac4205b21c0139e6719f088c894aef705d6dc42bf25c1f4786 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUU4OkVBMDZFOjM5NzAwNDg6NDkwMUE5Mjo2QjAxREVERCIsInZpc2l0b3JfaWQiOiIzODQ1NzE4MTU4NjA3NTYxODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 225ac9e19b63548e387ab92fa36d8d526ee56624a0dbc9c32fd248d82ab285ea |
| hovercard-subject-tag | issue:6270916824 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/71873/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a72167046d40f7b1464a142e51002de8f50535b8e5d798548967ed69a6085c35/angular/angular/issues/71873 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a72167046d40f7b1464a142e51002de8f50535b8e5d798548967ed69a6085c35/angular/angular/issues/71873 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? common Is this a regression? No Description During hydration, NgOptimizedImage makes the browser select the source of each image again. There ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jsaguet |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width