Skip to content

Commit a1bcfd0

Browse files
AndrewKushnirPawel Kozlowski
authored andcommitted
docs: add description and usage notes to the NgOptimizedImage directive (#47082)
This commit adds the docs for the NgOptimizedImage directive. As a part of this commit, we also remove an export of directive-related symbols previously exposed as public APIs (i.e. APIs can not be used directly). PR Close #47082
1 parent d5f7da2 commit a1bcfd0

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

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

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,96 @@ export const RECOMMENDED_SRCSET_DENSITY_CAP = 2;
5555
const ASPECT_RATIO_TOLERANCE = .1;
5656

5757
/**
58-
* ** EXPERIMENTAL **
58+
* The directive that helps to improve image loading performance by following best practices.
5959
*
60-
* TODO: add Image directive description.
60+
* The `NgOptimizedImage` ensures that the loading of the LCP image is prioritized by:
61+
* - Automatically setting the `fetchpriority` attribute on the `<img>` tag
62+
* - Lazy loading non-priority images by default
63+
* - Asserting that there is a corresponding preconnect link tag in the document head
64+
*
65+
* In addition, the directive:
66+
* - Generates appropriate asset URLs (if a corresponding loader function is provided)
67+
* - Requires that `width` and `height` are set
68+
* - Warns if `width` or `height` have been set incorrectly
69+
* - Warns if the image will be visually distorted when rendered
6170
*
6271
* @usageNotes
63-
* TODO: add Image directive usage notes.
72+
* The `NgOptimizedImage` directive is marked as [standalone](guide/standalone-components) and can
73+
* be imported directly.
74+
*
75+
* Follow the steps below to enable and use the directive:
76+
* 1. Import it into the necessary NgModule or a standalone Component.
77+
* 2. Configure a loader that you want to use.
78+
* 3. Update the necessary `<img>` tags in templates and replace `src` attributes with `rawSrc`.
79+
*
80+
* Step 1: import the `NgOptimizedImage` directive.
81+
*
82+
* ```typescript
83+
* import { NgOptimizedImage } from '@angular/common';
84+
*
85+
* // Include it into the necessary NgModule
86+
* @NgModule({
87+
* imports: [NgOptimizedImage],
88+
* })
89+
* class AppModule {}
90+
*
91+
* // ... or a standalone Component
92+
* @Component({
93+
* standalone: true
94+
* imports: [NgOptimizedImage],
95+
* })
96+
* class MyStandaloneComponent {}
97+
* ```
98+
*
99+
* Step 2: configure a loader.
100+
*
101+
* To use the **default loader**: no additional code changes are necessary. The URL returned by the
102+
* generic loader will always match the value of "src". In other words, this loader applies no
103+
* transformations to thr resource URL and the value of the `rawSrc` attribute will be used as is.
104+
*
105+
* To use an existing loader for a **third-party image service**: add the provider factory for your
106+
* chosen service to the `providers` array. In the example below, the Imgix loader is used:
107+
*
108+
* ```typescript
109+
* import {provideImgixLoader} from '@angular/common';
110+
*
111+
* // Call the function and add the result to the `providers` array:
112+
* providers: [
113+
* provideImgixLoader("https://my.base.url/"),
114+
* ],
115+
* ```
116+
*
117+
* The `NgOptimizedImage` directive provides the following functions:
118+
* - `provideCloudflareLoader`
119+
* - `provideCloudinaryLoader`
120+
* - `provideImageKitLoader`
121+
* - `provideImgixLoader`
122+
*
123+
* If you use a different image provider, you can create a custom loader function as described
124+
* below.
125+
*
126+
* To use a **custom loader**: provide your loader function as a value for the `IMAGE_LOADER` DI
127+
* token.
128+
*
129+
* ```typescript
130+
* import {IMAGE_LOADER, ImageLoaderConfig} from '@angular/common';
131+
*
132+
* // Configure the loader using the `IMAGE_LOADER` token.
133+
* providers: [
134+
* {
135+
* provide: IMAGE_LOADER,
136+
* useValue: (config: ImageLoaderConfig) => {
137+
* return `https://example.com/${config.src}-${config.width}.jpg}`;
138+
* }
139+
* },
140+
* ],
141+
* ```
142+
*
143+
* Step 3: update `<img>` tags in templates to use `rawSrc` instead of `rawSrc`.
144+
*
145+
* ```
146+
* <img rawSrc="logo.png" width="200" height="100">
147+
* ```
64148
*
65149
* @publicApi
66150
* @developerPreview

packages/common/src/private_export.ts

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

9-
export {IMAGE_LOADER as ɵIMAGE_LOADER, ImageLoader as ɵImageLoader, ImageLoaderConfig as ɵImageLoaderConfig, NgOptimizedImage as ɵNgOptimizedImage, PRECONNECT_CHECK_BLOCKLIST as ɵPRECONNECT_CHECK_BLOCKLIST, provideImgixLoader as ɵprovideImgixLoader} from './directives/ng_optimized_image/index';
109
export {DomAdapter as ɵDomAdapter, getDOM as ɵgetDOM, setRootDomAdapter as ɵsetRootDomAdapter} from './dom_adapter';
1110
export {BrowserPlatformLocation as ɵBrowserPlatformLocation} from './location/platform_location';

packages/core/test/bundling/image-directive/e2e/basic/basic.ts

Lines changed: 1 addition & 1 deletion
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 {ɵIMAGE_LOADER as IMAGE_LOADER, ɵNgOptimizedImage as NgOptimizedImage} from '@angular/common';
9+
import {IMAGE_LOADER, NgOptimizedImage} from '@angular/common';
1010
import {Component} from '@angular/core';
1111

1212
@Component({

packages/core/test/bundling/image-directive/e2e/image-distortion/image-distortion.ts

Lines changed: 1 addition & 1 deletion
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 {ɵNgOptimizedImage as NgOptimizedImage} from '@angular/common';
9+
import {NgOptimizedImage} from '@angular/common';
1010
import {Component} from '@angular/core';
1111

1212
@Component({

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

Lines changed: 1 addition & 1 deletion
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 {ɵNgOptimizedImage as NgOptimizedImage} from '@angular/common';
9+
import {NgOptimizedImage} from '@angular/common';
1010
import {Component} from '@angular/core';
1111

1212
@Component({

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

Lines changed: 1 addition & 1 deletion
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 {DOCUMENT, ɵIMAGE_LOADER as IMAGE_LOADER, ɵNgOptimizedImage as NgOptimizedImage} from '@angular/common';
9+
import {DOCUMENT, IMAGE_LOADER, NgOptimizedImage} from '@angular/common';
1010
import {Component, Inject} from '@angular/core';
1111

1212
@Component({

packages/core/test/bundling/image-directive/playground.ts

Lines changed: 1 addition & 1 deletion
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 {ɵNgOptimizedImage as NgOptimizedImage, ɵprovideImgixLoader as provideImgixLoader} from '@angular/common';
9+
import {NgOptimizedImage, provideImgixLoader} from '@angular/common';
1010
import {Component} from '@angular/core';
1111

1212
@Component({

0 commit comments

Comments
 (0)