Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async & defer for script tags #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion org/cfstatic/core/Package.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
var media = "";
var ie = "";
var shouldBeRendered = "";
var async = "";
var defer = "";

switch( minification ){
case 'none': case 'file':
Expand All @@ -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;
}
Expand Down Expand Up @@ -163,6 +168,44 @@
</cfscript>
</cffunction>

<cffunction name="getJSAsync" access="public" returntype="string" output="false" hint="I get the target async property of the JS files for the entire package. All static files within a single package should have the same async property (when minifiying all together), an exception will be thrown otherwise..">
<cfscript>
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;
</cfscript>
</cffunction>

<cffunction name="getJSDefer" access="public" returntype="string" output="false" hint="I get the target defer property of the JS files for the entire package. All static files within a single package should have the same defer property (when minifiying all together), an exception will be thrown otherwise..">
<cfscript>
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;
</cfscript>
</cffunction>

<cffunction name="getCssMedia" access="public" returntype="string" output="false" hint="I get the target media of the css files for the entire package. All static files within a single package should have the same media (when minifiying all together), an exception will be thrown otherwise.">
<cfscript>
var files = getOrdered();
Expand Down
46 changes: 45 additions & 1 deletion org/cfstatic/core/PackageCollection.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
var media = "";
var ie = "";
var shouldBeRendered = "";
var async = "";
var defer = "";

switch( minification ){
case 'none': case 'file': case 'package':
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -407,6 +413,44 @@
</cfscript>
</cffunction>

<cffunction name="_getJSAsync" access="public" returntype="string" output="false" hint="I get the target async property of the JS files for the entire package. All static files within a single package should have the same async property (when minifiying all together), an exception will be thrown otherwise..">
<cfscript>
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;
</cfscript>
</cffunction>

<cffunction name="_getJSDefer" access="public" returntype="string" output="false" hint="I get the target defer property of the JS files for the entire package. All static files within a single package should have the same defer property (when minifiying all together), an exception will be thrown otherwise..">
<cfscript>
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;
</cfscript>
</cffunction>

<cffunction name="_dependencyIsFullPath" access="private" returntype="boolean" output="false">
<cfargument name="dependency" type="string" required="true" />

Expand Down
5 changes: 4 additions & 1 deletion org/cfstatic/core/StaticFile.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@
<cfscript>
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 );
}
</cfscript>
</cffunction>
Expand Down
4 changes: 3 additions & 1 deletion org/cfstatic/util/Base.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@
<cffunction name="$renderJsInclude" access="private" returntype="string" output="false" hint="I return the html nevessary to include the given javascript file">
<cfargument name="src" type="string" required="true" />
<cfargument name="ieConditional" type="string" required="false" default="" />
<cfargument name="async" type="string" required="false" default="false" />
<cfargument name="defer" type="string" required="false" default="false" />

<cfreturn $renderIeConditional( '<script type="text/javascript" src="#src#"></script>', ieConditional ) & $newline() />
<cfreturn $renderIeConditional( '<script type="text/javascript" src="#src#" #arguments.async EQ "true" ? "async" : ""# #arguments.defer EQ "true" ? "defer" : ""# ></script>', ieConditional ) & $newline() />
</cffunction>

<cffunction name="$extractUrlFromRenderedInclude" access="private" returntype="string" output="false">
Expand Down