a way to combine all js into one file?

i'll preface this by saying that i am not the most accomplished coder. i apologize if there is an obvious answer to this. i also apologize if this would be better in feature requests or usage support.

discovery of the yslow and pagespeed plugins for firefox has tickled my OCD tendencies. i'm attempting to develop a theme that implements some of yahoo's best practices for speeding up web sites (http://developer.yahoo.com/performance/rules.html). implementing eTags, expires headers, gzip compression, and css sprites has helped performance a lot, especially after the first page load.

one of the rules i'm having trouble with is the saying to combine external javascript into one file in order to reduce http requests. since several plugins have their own js to load, i'd like to have a php file load all of those files into that one file and then call it from the theme pages. it was not too bad to get the jquery and zenphoto.js.php files into the file using simple `include()` calls. the trouble is trying to get any of the plugin scripts. when used outside of `zenjavascript()` (ie the php file i've made), the variable `$_zp_plugin_scripts` is empty.

any advice or ideas about getting the plugin javascript into my file would be appreciated. also if anyone can think of reasons not to do this (server time vs. client time, etc.) please share. it could also be that it doesn't matter much since eTags should be allowing the browser to cache scripts after the first page load that uses them.

Comments

  • acrylian Administrator, Developer
    `$_zp_plugin_scripts` is a global that probably is not setup. How does your file/script work, where is it located, in the theme folder?

    The problem is that the plugins provide no direct links to the js files but the ``setup so I think you would have to extract the actual url first before you can directly include them. Then you probably have to create a static cached page of that so you don't need to do the same all the time.
    I found this quickly online that might help you: http://www.thewebshop.ca/blog/2009/11/combining-css-or-javascript-files-on-the-fly-with-php/

    Generally I am not sure if this makes a noticable speed up for the actual visitor but I know about the recommendation to limit request.
    I would recommend to try to do a plugin for this so it is not theme dependent and maybe of use for all.

    You might also want to look at static_html_cache plugin that caches all theme pages.
  • If the `$_zp_plugin_scripts` variable is empty then you have not caused the plugins to be loaded.

    It is nice to have "rules" to follow to make things better. We have one as well--we do not modify third party scripts unless absolutely necessary. This prevents problems with support of their functionality. So, I guess your "rule" to make a website better is in direct conflict with a pretty basic rule of software development. Keep things modular and independent. Guess which rule will prevail with Zenphoto.
  • thank you both for your replies and opinions. it lets me know how to move forward. if i can get something working i'll be sure to share it.

    @acrylian - the static cache would probably speed things up on the backend so thank you for that additional piece of advice.

    @sbillard - i am trying to keep changes limited to the theme itself. it is my understanding that concatenating all of the js into one file should not change the behavior of how it is implemented. i do appreciate the reminder. it fits with acrylian's advice to make it a plugin. i don't think it would be tough to make the php on theme pages check for the plugin and just run the standard zenJavascript if it is not.

    thanks again to the whole zenphoto team. it's a great tool with a great community and developers.
  • acrylian Administrator, Developer
    The static html cache plugin is of course for the front end (= theme)...;-)
  • i meant server vs. browser. i really don't want to mess around with the back end of zp. you guys are probably better off that way.
  • Certainly you could put that check into your theme. Just replace the places where the code reads `<?php zenJavascript(); ?>` with whatever you want. But be aware that you will need to know what each plugin adds to the list dynamically. So, I think, this means you would have to create the consolidated js each time a page loaded if you want your plugin to be fool proof. Not much of a saving at all, I think.
  • @sbillard: i'm glad i posted here because the more i think about it the more i am agreeing with your responses. unless i could make a plugin that tracked active plugins and made a static cached file i don't think it would save much. even then it wouldn't save anything after the very first time each of the js files were loaded (as long as eTags and expires headers are set up properly on the server).
Sign In or Register to comment.