@@ -12,7 +12,10 @@ import {RuntimeErrorCode} from '../../errors';
1212import { isPlatformServer } from '../../platform_id' ;
1313
1414import { imgDirectiveDetails } from './error_helper' ;
15- import { IMAGE_LOADER } from './image_loaders/image_loader' ;
15+ import { cloudinaryLoaderInfo } from './image_loaders/cloudinary_loader' ;
16+ import { IMAGE_LOADER , ImageLoader , noopImageLoader } from './image_loaders/image_loader' ;
17+ import { imageKitLoaderInfo } from './image_loaders/imagekit_loader' ;
18+ import { imgixLoaderInfo } from './image_loaders/imgix_loader' ;
1619import { LCPImageObserver } from './lcp_image_observer' ;
1720import { PreconnectLinkChecker } from './preconnect_link_checker' ;
1821import { PreloadLinkCreator } from './preload-link-creator' ;
@@ -72,6 +75,9 @@ const ASPECT_RATIO_TOLERANCE = .1;
7275 */
7376const OVERSIZED_IMAGE_TOLERANCE = 1000 ;
7477
78+ /** Info about built-in loaders we can test for. */
79+ export const BUILT_IN_LOADERS = [ imgixLoaderInfo , imageKitLoaderInfo , cloudinaryLoaderInfo ] ;
80+
7581/**
7682 * A configuration object for the NgOptimizedImage directive. Contains:
7783 * - breakpoints: An array of integer breakpoints used to generate
@@ -385,6 +391,7 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
385391 if ( ! this . ngSrcset ) {
386392 assertNoComplexSizes ( this ) ;
387393 }
394+ assertNotMissingBuiltInLoader ( this . ngSrc , this . imageLoader ) ;
388395 if ( this . priority ) {
389396 const checker = this . injector . get ( PreconnectLinkChecker ) ;
390397 checker . assertPreconnect ( this . getRewrittenSrc ( ) , this . ngSrc ) ;
@@ -873,3 +880,35 @@ function assertValidLoadingInput(dir: NgOptimizedImage) {
873880 `To fix this, provide a valid value ("lazy", "eager", or "auto").` ) ;
874881 }
875882}
883+
884+ /**
885+ * Warns if NOT using a loader (falling back to the generic loader) and
886+ * the image appears to be hosted on one of the image CDNs for which
887+ * we do have a built-in image loader. Suggests switching to the
888+ * built-in loader.
889+ *
890+ * @param ngSrc Value of the ngSrc attribute
891+ * @param imageLoader ImageLoader provided
892+ */
893+ function assertNotMissingBuiltInLoader ( ngSrc : string , imageLoader : ImageLoader ) {
894+ if ( imageLoader === noopImageLoader ) {
895+ let builtInLoaderName = '' ;
896+ for ( const loader of BUILT_IN_LOADERS ) {
897+ if ( loader . testUrl ( ngSrc ) ) {
898+ builtInLoaderName = loader . name ;
899+ break ;
900+ }
901+ }
902+ if ( builtInLoaderName ) {
903+ console . warn ( formatRuntimeError (
904+ RuntimeErrorCode . MISSING_BUILTIN_LOADER ,
905+ `NgOptimizedImage: It looks like your images may be hosted on the ` +
906+ `${ builtInLoaderName } CDN, but your app is not using Angular's ` +
907+ `built-in loader for that CDN. We recommend switching to use ` +
908+ `the built-in by calling \`provide${ builtInLoaderName } Loader()\` ` +
909+ `in your \`providers\` and passing it your instance's base URL. ` +
910+ `If you don't want to use the built-in loader, define a custom ` +
911+ `loader function using IMAGE_LOADER to silence this warning.` ) ) ;
912+ }
913+ }
914+ }
0 commit comments