Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

Request for v1.8: Join all Javascript into one file

+3 votes
asked Jun 19, 2015 in Q2A Core by q2apro

For less server requests I would suggest to put all JS files into one main file (which could also be provided as a compressed version).

See qa-content/....js

With v.1.7.1 we have:

qa-admin.js (4 KB)
qa-ask.js (9 KB)
qa-page.js (4 KB)
qa-question.js (6 KB)
qa-user.js (4 KB)

Just do one script.js file: would only be 27 KB

It is already enough that each plugin must load its own javascript file, all are adding up to many server requests and make the site load slowly: not good for user experience, not good for seo.



Plus give the option to load Jquery from a CDN, local file qa-content/jquery-1.11.3.min.js 

Using an advanced theme it would be:

        function head_script()
            // insert jquery CDN script
            if (isset($this->content['script'])) {
                foreach ($this->content['script'] as $scriptline) {
                    // load CDN instead of local jquery file, with js fallback
                    if(strpos($scriptline, 'jquery') === false) {
                    else {
                        $this->output_raw('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>   <script type="text/javascript">window.jQuery || document.write(\'<script src="http://www.fallbackdomain.com/jquery.min.js"><\/script>\')</script>');
            // no default call
        } // end head_script


Q2A version: 1.7.1
commented Jun 19, 2015 by sama55
Your request is roughly correct. My FA2 package has minify controller and HTML compressor. Because of this feature, it is faster than Snow. The most significant factor is the number of requests.

Performance comparison data by GTMetrix:

This is the data that do not add plugins. In my experiment, if we added a lot of plugin which is necessary JS and CSS, performance difference will be even greater. In some cases, the difference was several fold. However, there is one problem. Although many of the JS and CSS can be combined, there are JS and CSS which can not be combined. For example, it is CKEditor.

2 Answers

0 votes
answered Jun 19, 2015 by Scott
I understand what you are saying, but actually there are not loads of server requests. No more than 2 of those JS files are ever loaded on any page. For example the admin page has qa-page.js & qa-admin.js; the user page has qa-page.js & qa-user.js. No page has qa-page.js & qa-admin.js & qa-user.js.

That's not to say it isn't worth doing though. There is also a bunch of JS that gets output direct into pages too and I think most of this could be reduced massively by using unobtrusive JavaScript instead of onclick attributes. It's something I plan to look into.
+1 vote
answered Jan 28 by q2apro
Finally implemented in q2a 1.8.0.