From 95d4c0a98e566bf37457bc51eaf1a9f0acb75789 Mon Sep 17 00:00:00 2001 From: Chris Hopkins Date: Thu, 19 Sep 2024 16:29:48 +0100 Subject: [PATCH] Add async & defer for script tags annotations @async and @defer can control whether to add these attributes to script tags --- org/cfstatic/core/Package.cfc | 45 +++++++++++++++++++++++- org/cfstatic/core/PackageCollection.cfc | 46 ++++++++++++++++++++++++- org/cfstatic/core/StaticFile.cfc | 5 ++- org/cfstatic/util/Base.cfc | 4 ++- 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/org/cfstatic/core/Package.cfc b/org/cfstatic/core/Package.cfc index 22e5de5..61469e2 100644 --- a/org/cfstatic/core/Package.cfc +++ b/org/cfstatic/core/Package.cfc @@ -97,6 +97,8 @@ var media = ""; var ie = ""; var shouldBeRendered = ""; + var async = ""; + var defer = ""; switch( minification ){ case 'none': case 'file': @@ -119,7 +121,10 @@ media = getCssMedia(); return $renderCssInclude( src, media, ie ); } else { - return $renderJsInclude( src, ie ); + async = getJSAsync(); + defer = getJSDefer(); + + return $renderJsInclude( src, ie, async, defer ); } break; } @@ -163,6 +168,44 @@ + + + var files = getOrdered(); + var async = ""; + var i = 0; + + if ( ArrayLen( files ) ) { + async = getStaticFile( files[1] ).getProperty('async', 'false', 'string'); + for( i=2; i LTE ArrayLen(files); i++ ){ + if ( async NEQ getStaticFile( files[i] ).getProperty('async', 'false', 'string') ) { + $throw( type="cfstatic.Package.badConfig", message="There was an error compiling the package, '#_getPackageName()#', not all files define the same async property." ); + } + } + } + + return async; + + + + + + var files = getOrdered(); + var defer = ""; + var i = 0; + + if ( ArrayLen( files ) ) { + defer = getStaticFile( files[1] ).getProperty('defer', 'false', 'string'); + for( i=2; i LTE ArrayLen(files); i++ ){ + if ( defer NEQ getStaticFile( files[i] ).getProperty('defer', 'false', 'string') ) { + $throw( type="cfstatic.Package.badConfig", message="There was an error compiling the package, '#_getPackageName()#', not all files define the same defer property." ); + } + } + } + + return defer; + + + var files = getOrdered(); diff --git a/org/cfstatic/core/PackageCollection.cfc b/org/cfstatic/core/PackageCollection.cfc index 9d5ccd1..64ce2e5 100644 --- a/org/cfstatic/core/PackageCollection.cfc +++ b/org/cfstatic/core/PackageCollection.cfc @@ -78,6 +78,8 @@ var media = ""; var ie = ""; var shouldBeRendered = ""; + var async = ""; + var defer = ""; switch( minification ){ case 'none': case 'file': case 'package': @@ -109,7 +111,11 @@ media = _getCssMedia(); str.append( $renderCssInclude( src, media, ie ) ); } else { - str.append( $renderJsInclude( src, ie ) ); + + async = _getJSAsync(); + defer = _getJSDefer(); + + str.append( $renderJsInclude( src, ie, async, defer ) ); } break; } @@ -407,6 +413,44 @@ + + + var packages = getOrdered(); + var async = ""; + var i = 0; + + if ( ArrayLen( packages ) ) { + async = getPackage( packages[1] ).getJSAsync(); + for( i=2; i LTE ArrayLen( packages ); i++ ) { + if ( async NEQ getPackage( packages[i] ).getJSAsync() ) { + $throw( type="cfstatic.PackageCollection.badConfig", message="There was an error compiling the #_getFileType()# min file, not all files define the same Async property." ); + } + } + } + + return async; + + + + + + var packages = getOrdered(); + var defer = ""; + var i = 0; + + if ( ArrayLen( packages ) ) { + defer = getPackage( packages[1] ).getJSDefer(); + for( i=2; i LTE ArrayLen( packages ); i++ ) { + if ( defer NEQ getPackage( packages[i] ).getJSDefer() ) { + $throw( type="cfstatic.PackageCollection.badConfig", message="There was an error compiling the #_getFileType()# min file, not all files define the same Defer property." ); + } + } + } + + return defer; + + + diff --git a/org/cfstatic/core/StaticFile.cfc b/org/cfstatic/core/StaticFile.cfc index 002762c..bc93e8a 100644 --- a/org/cfstatic/core/StaticFile.cfc +++ b/org/cfstatic/core/StaticFile.cfc @@ -127,13 +127,16 @@ var media = getProperty( 'media', 'all', 'string' ); var ie = getProperty( 'IE', '', 'string' ); + var async = getProperty( 'async', 'false', 'string' ); + var defer = getProperty( 'defer', 'false', 'string' ); + var src = iif( minified, DE( _getMinifiedUrl() ), DE( _getUrl() ) ); if ( _getFileType() EQ 'css' ) { return $renderCssInclude( src, media, ie ); } else { - return $renderJsInclude( src, ie ); + return $renderJsInclude( src, ie, async, defer ); } diff --git a/org/cfstatic/util/Base.cfc b/org/cfstatic/util/Base.cfc index fa4214e..458693c 100644 --- a/org/cfstatic/util/Base.cfc +++ b/org/cfstatic/util/Base.cfc @@ -178,8 +178,10 @@ + + - ', ieConditional ) & $newline() /> + ', ieConditional ) & $newline() />