mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FEATURE Added configurable Requirements::$write_js_to_body for performance improvements (turned off by default)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60396 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
8536a47b7c
commit
f9a1118b64
@ -81,6 +81,21 @@ class Requirements {
|
||||
*/
|
||||
public static $combine_js_with_jsmin = true;
|
||||
|
||||
/**
|
||||
* Put all javascript includes at the bottom of the template
|
||||
* before the closing <body> tag instead of the <head> tag.
|
||||
* This means script downloads won't block other HTTP-requests,
|
||||
* which can be a performance improvement.
|
||||
* Caution: Doesn't work when modifying the DOM from those external
|
||||
* scripts without listening to window.onload/document.ready
|
||||
* (e.g. toplevel document.write() calls).
|
||||
*
|
||||
* @see http://developer.yahoo.com/performance/rules.html#js_bottom
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public static $write_js_to_body = false;
|
||||
|
||||
/**
|
||||
* Register the given javascript file as required.
|
||||
* Filenames should be relative to the base, eg, 'sapphire/javascript/loader.js'
|
||||
@ -278,6 +293,8 @@ class Requirements {
|
||||
}
|
||||
}
|
||||
|
||||
// add all inline javascript *after* including external files which
|
||||
// they might rely on
|
||||
if(self::$customScript) {
|
||||
foreach(array_diff_key(self::$customScript,self::$blocked) as $script) {
|
||||
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n";
|
||||
@ -302,8 +319,9 @@ class Requirements {
|
||||
$requirements .= "$customHeadTag\n";
|
||||
}
|
||||
|
||||
if(isset($_GET['debug_profile'])) Profiler::unmark("Requirements::includeInHTML");
|
||||
|
||||
if(self::$write_js_to_body) {
|
||||
// Remove all newlines from code to preserve layout
|
||||
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
|
||||
|
||||
// We put script tags into the body, for performance.
|
||||
// If your template already has script tags in the body, then we put our script tags at the top of the body.
|
||||
@ -318,14 +336,18 @@ class Requirements {
|
||||
}
|
||||
|
||||
// Put CSS at the bottom of the head
|
||||
return eregi_replace("(</head[^>]*>)", $requirements . "\\1", $content);
|
||||
|
||||
$content = eregi_replace("(</head[^>]*>)", $requirements . "\\1", $content);
|
||||
} else {
|
||||
if(isset($_GET['debug_profile'])) Profiler::unmark("Requirements::includeInHTML");
|
||||
return $content;
|
||||
$content = eregi_replace("(</head[^>]*>)", $requirements . "\\1", $content);
|
||||
$content = eregi_replace("(</head[^>]*>)", $jsRequirements . "\\1", $content);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($_GET['debug_profile'])) Profiler::unmark("Requirements::includeInHTML");
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate several css or javascript files into a single dynamically generated
|
||||
* file (stored in {@link Director::baseFolder()}). This increases performance
|
||||
|
Loading…
x
Reference in New Issue
Block a user