Skip to content

Commit fe2fd7e

Browse files
atcastlepkozlowski-opensource
authored andcommitted
feat(common): make the warning for lazy-loaded lcp image an error (angular#51748)
upgrade the warning for lazy-loaded lcp images when using NgOptimizedImage to an error BREAKING CHANGE: Previously when NgOptimizedImage directive detected that an LCP image is lazy-loaded, a console warning was produced. Now the directive throws an error to make it more discoverable in a console. If you receive this error, refer to this guide for additional information: https://angular.io/guide/image-directive#step-4-mark-images-as-priority PR Close angular#51748
1 parent 545db6d commit fe2fd7e

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

aio/content/guide/image-directive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Marking an image as `priority` applies the following optimizations:
6565
* Sets `loading=eager` (read more about native lazy loading [here](https://web.dev/browser-level-image-lazy-loading))
6666
* Automatically generates a [preload link element](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload) if [rendering on the server](/guide/universal).
6767

68-
Angular displays a warning during development if the LCP element is an image that does not have the `priority` attribute. A page’s LCP element can vary based on a number of factors - such as the dimensions of a user's screen, so a page may have multiple images that should be marked `priority`. See [CSS for Web Vitals](https://web.dev/css-web-vitals/#images-and-largest-contentful-paint-lcp) for more details.
68+
Angular throws an error during development if the LCP element is an image that does not have the `priority` attribute, as this can hurt loading performance significantly. A page’s LCP element can vary based on a number of factors - such as the dimensions of a user's screen, so a page may have multiple images that should be marked `priority`. See [CSS for Web Vitals](https://web.dev/css-web-vitals/#images-and-largest-contentful-paint-lcp) for more details.
6969

7070
#### Step 5: Include Height and Width
7171

packages/common/src/directives/ng_optimized_image/lcp_image_observer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {inject, Injectable, OnDestroy, ɵformatRuntimeError as formatRuntimeError} from '@angular/core';
9+
import {inject, Injectable, OnDestroy, ɵformatRuntimeError as formatRuntimeError, ɵRuntimeError as RuntimeError} from '@angular/core';
1010

1111
import {DOCUMENT} from '../../dom_tokens';
1212
import {RuntimeErrorCode} from '../../errors';
@@ -74,7 +74,7 @@ export class LCPImageObserver implements OnDestroy {
7474
if (!img) return;
7575
if (!img.priority && !img.alreadyWarnedPriority) {
7676
img.alreadyWarnedPriority = true;
77-
logMissingPriorityWarning(imgSrc);
77+
throwMissingPriorityError(imgSrc);
7878
}
7979
if (img.modified && !img.alreadyWarnedModified) {
8080
img.alreadyWarnedModified = true;
@@ -118,14 +118,14 @@ export class LCPImageObserver implements OnDestroy {
118118
}
119119
}
120120

121-
function logMissingPriorityWarning(ngSrc: string) {
121+
function throwMissingPriorityError(ngSrc: string) {
122122
const directiveDetails = imgDirectiveDetails(ngSrc);
123-
console.warn(formatRuntimeError(
123+
throw new RuntimeError(
124124
RuntimeErrorCode.LCP_IMG_MISSING_PRIORITY,
125-
`${directiveDetails} this image is the Largest Contentful Paint (LCP) ` +
126-
`element but was not marked "priority". This image should be marked ` +
127-
`"priority" in order to prioritize its loading. ` +
128-
`To fix this, add the "priority" attribute.`));
125+
`${directiveDetails} this image is the Largest Contentful Paint (LCP) element ` +
126+
`but was not marked "priority". This can have a strong negative impact ` +
127+
`on the LCP score of the entire application. This error can be fixed by ` +
128+
`adding the 'priority' attribute to all images which are possibly the LCP element.`);
129129
}
130130

131131
function logModifiedWarning(ngSrc: string) {

packages/core/test/bundling/image-directive/e2e/lcp-check/lcp-check.e2e-spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ describe('NgOptimizedImage directive', () => {
2727
srcB = await imgs.get(2).getAttribute('src');
2828
expect(srcB.endsWith('b.png')).toBe(true);
2929

30-
// Make sure that only one warning is in the console for image `a.png`,
30+
// Make sure that only one error is in the console for image `a.png`,
3131
// since the `b.png` should be below the fold and not treated as an LCP element.
32-
const logs = await collectBrowserLogs(logging.Level.WARNING);
33-
expect(logs.length).toEqual(2);
34-
// Verify that the error code and the image src are present in the error message.
35-
expect(logs[0].message).toMatch(/NG02955.*?a\.png/);
36-
expect(logs[1].message).toMatch(/NG02964.*?logo-500w\.jpg/);
32+
const logs = await collectBrowserLogs(logging.Level.SEVERE);
33+
expect(logs.length).toEqual(1);
34+
35+
expect(logs[0].message).toMatch(/RuntimeError: NG02955/);
3736
});
3837
});

0 commit comments

Comments
 (0)