Skip to content

Commit

Permalink
feat(common): upgrade warning to logged error for lazy-loaded LCP ima…
Browse files Browse the repository at this point in the history
…ges using NgOptimizedImage (#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 #52004
  • Loading branch information
atcastle authored and alxhub committed Oct 4, 2023
1 parent 5269cae commit dde3fda
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion aio/content/guide/image-directive.md
Expand Up @@ -65,7 +65,7 @@ Marking an image as `priority` applies the following optimizations:
* Sets `loading=eager` (read more about native lazy loading [here](https://web.dev/browser-level-image-lazy-loading))
* 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).

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.
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.

#### Step 5: Include Height and Width

Expand Down
Expand Up @@ -74,7 +74,7 @@ export class LCPImageObserver implements OnDestroy {
if (!img) return;
if (!img.priority && !img.alreadyWarnedPriority) {
img.alreadyWarnedPriority = true;
logMissingPriorityWarning(imgSrc);
logMissingPriorityError(imgSrc);
}
if (img.modified && !img.alreadyWarnedModified) {
img.alreadyWarnedModified = true;
Expand Down Expand Up @@ -118,9 +118,9 @@ export class LCPImageObserver implements OnDestroy {
}
}

function logMissingPriorityWarning(ngSrc: string) {
function logMissingPriorityError(ngSrc: string) {
const directiveDetails = imgDirectiveDetails(ngSrc);
console.warn(formatRuntimeError(
console.error(formatRuntimeError(
RuntimeErrorCode.LCP_IMG_MISSING_PRIORITY,
`${directiveDetails} this image is the Largest Contentful Paint (LCP) ` +
`element but was not marked "priority". This image should be marked ` +
Expand Down
Expand Up @@ -29,10 +29,9 @@ describe('NgOptimizedImage directive', () => {

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

0 comments on commit dde3fda

Please sign in to comment.