Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dde3fda

Browse files
atcastlealxhub
authored andcommitted
feat(common): upgrade warning to logged error for lazy-loaded LCP images using NgOptimizedImage (angular#52004)
Upgrade the existing warning so it now logs an error instead, when an LCP element is determined to not be usings the `priority` attribute. Error is logged, not thrown. PR Close angular#52004
1 parent 5269cae commit dde3fda

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
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 logs 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
logMissingPriorityError(imgSrc);
7878
}
7979
if (img.modified && !img.alreadyWarnedModified) {
8080
img.alreadyWarnedModified = true;
@@ -118,9 +118,9 @@ export class LCPImageObserver implements OnDestroy {
118118
}
119119
}
120120

121-
function logMissingPriorityWarning(ngSrc: string) {
121+
function logMissingPriorityError(ngSrc: string) {
122122
const directiveDetails = imgDirectiveDetails(ngSrc);
123-
console.warn(formatRuntimeError(
123+
console.error(formatRuntimeError(
124124
RuntimeErrorCode.LCP_IMG_MISSING_PRIORITY,
125125
`${directiveDetails} this image is the Largest Contentful Paint (LCP) ` +
126126
`element but was not marked "priority". This image should be marked ` +

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ describe('NgOptimizedImage directive', () => {
2929

3030
// Make sure that only one warning 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);
32+
const logs = await collectBrowserLogs(logging.Level.SEVERE);
33+
expect(logs.length).toEqual(1);
3434
// Verify that the error code and the image src are present in the error message.
3535
expect(logs[0].message).toMatch(/NG02955.*?a\.png/);
36-
expect(logs[1].message).toMatch(/NG02964.*?logo-500w\.jpg/);
3736
});
3837
});

0 commit comments

Comments
 (0)