В настоящее время я пытаюсь связать свой javascript, чтобы получить более высокий балл по скорости страницы/yslow. Но я сталкиваюсь со стенами с таким подходом. В настоящее время я использую это РУКОВОДСТВО в качестве руководства по сборке js-файлов. Я вызываю файлы js, но в инструменте firefox показывает, что файл bundle.js
не существует, что верно, но с помощью htaccess я меняю файл с bundle.php
на bundle.js
. Но я не получаю никаких результатов. Может ли кто-нибудь помочь мне определить проблему или есть лучший подход к объединению файлов js?
ВОТ мой ПРИМЕР . При правильной работе с файлами js должно отображаться 4 поля ввода для загрузки файлов.
Вот как я называю файлы js
.htacces для изменения типа файла*
RewriteEngine on
RewriteRule ^bundle.js$ bundle.php [QSA,L]
core/bundle.php
include "JSMin.php";
$path = "../../";
$files = explode(",", $_GET['js']);
$missing = array();
$cache = '';
foreach ($files as $index => $file) {
if (strtolower(substr($file, -2)) != 'js') {
unset($files[$index]);
} else if (file_exists($path.$file)) {
$cache.= $file;
$cache.= filemtime($path.$file);
} else {
$missing[] = $file;
unset($files[$index]);
}
}
$cache = 'cache/'.md5($cache);
if (count($missing)) {
header("Content-type: text/javascript");
header("Pragma: no-cache");
header("Cache-Control: no-cache");
echo "alert('Could not load the following javascript source files:\\n\\n- ".implode("\\n- ", $missing)."\\n\\nJavascript not loaded / running!');";
exit;
}
if (count($files)) {
// create cached version if not present
if (!file_exists($cache)) {
$js = '';
foreach ($files as $file) {
$js.= JSMin::minify(file_get_contents($path.$file));
}
file_put_contents($cache, $js);
}
// calculate last-modified & etag send caching headers
$last_modified_time = filemtime($cache);
$etag = md5_file($cache);
header("Content-type: text/javascript");
header("Pragma: public");
header("Cache-Control: public");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: ".$etag);
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
readfile($cache);
}