Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:00:51+00:00 2026-06-09T11:00:51+00:00

I am currently trying to bundle my javascript so I can receive a higher

  • 0

I am currently trying to bundle my javascript so I can receive a higher score in page speed/yslow. But i am running into walls with this approach. I am currently using this TUTORIAL as guidance to bundle the js files. I am calling the js files but in the firefox tool is showing that the file bundle.js does not exists which is true but with the htaccess i am chaning the file from bundle.php to bundle.js. But I am not getting any results. Can someone help me pinpoint the issue or is there a better approach to bundle js files?

HERE is my EXAMPLE. When working properly with the js files it should show 4 input fields for file uploads.

This is how I am calling the js files

<script src='core/bundle.js?js=js/jquery-ui-1.8rc2.custom.min.js,js/globalFunctions.js,js/uploaderPreviewer.js,js/itemForm.js&amp;m=4948599067'>
</script>

.htacces to change file type*

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:00:53+00:00Added an answer on June 9, 2026 at 11:00 am

    Here’s what I’ve been using successfully for many years (no .htaccess needed):

    In html:

    <script type = "text/javascript" src = "js/scripts.php?build=12345&load=script1,script2,script3,folder/script4"> </script> <!-- do not specify the .js extension -->
    

    In php (file scripts.php):

    <?php
    error_reporting(E_ERROR);
    // see http://web.archive.org/web/20071211140719/http://www.w3.org/2005/MWI/BPWG/techs/CachingWithPhp
    // $lastModifiedDate must be a GMT Unix Timestamp
    // You can use gmmktime(...) to get such a timestamp
    // getlastmod() also provides this kind of timestamp for the last
    // modification date of the PHP file itself
    function cacheHeaders($lastModifiedDate) {
        if ($lastModifiedDate) {
            if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModifiedDate) {
                if (php_sapi_name()=='CGI') {
                    Header("Status: 304 Not Modified");
                } else {
                    Header("HTTP/1.0 304 Not Modified");
                }
                exit;
            } else {
                $gmtDate = gmdate("D, d M Y H:i:s \G\M\T",$lastModifiedDate);
                header('Last-Modified: '.$gmtDate);
            }
        }
    }
    
    // This function uses a static variable to track the most recent
    // last modification time
    function lastModificationTime($time=0) {
        static $last_mod ;
        if (!isset($last_mod) || $time > $last_mod) {
            $last_mod = $time ;
        }
        return $last_mod ;
    }
    
    lastModificationTime(filemtime(__FILE__));
    cacheHeaders(lastModificationTime());
    header("Content-type: text/javascript; charset: UTF-8");
    
    ob_start ("ob_gzhandler");
    
    foreach (explode(",", $_GET['load']) as $value) {
        if (is_file("$value.js")) {
            $real_path = mb_strtolower(realpath("$value.js"));
            if (strpos($real_path, mb_strtolower(dirname(__FILE__))) !== false || strpos($real_path, mb_strtolower(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR)) !== false) {
                lastModificationTime(filemtime("$value.js"));
                include("$value.js");echo "\n";
            } 
        }
    }
    ?>
    

    This compresses, merges and caches the js files

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am trying to add a preference activity into my application but found
I'm currently trying to get a simple bundle containing a Service Factory running. This
So i'm trying to make tabs with the JQuery UI framework, but I can't
I'm currently trying to write an app for one special device running Android 2.3.4,
I am currently trying to simply insert a new row into my SQLite3 database
Currently trying at add ajax to a site, after much reading I discovered that
Im currently trying to downlaod a audio track from a WCF, i need some
Im currently trying to learn some stuff about encryption, it's algorithms and how it
I am currently trying to write some code that will accept some FTP details,
I'm currently trying out Aptana Studio 3 for PHP development (I'm pretty new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.