flash service

High_Performance_Web_Sites

Issue link: https://read.uberflip.com/i/1146286

Contents of this Issue

Navigation

Page 109 of 169

88 | Chapter 12: Rule 12: Remove Duplicate Scripts echo ' "; } ?> The first time a script is inserted, we'll reach pushInserted. This adds the script to the alreadyInserted list for the page. If that script is accidentally inserted again, the test for alreadyInserted will not add the script again, thus solving the duplicate script issue. If this script has dependencies, those prerequisite scripts are inserted. In this exam- ple, menu.js might depend on events.js and utils.js. You can capture these depen- dency relationships using a hash or database. For simpler sites, dependencies can be maintained manually. For more complex sites, you may choose to automate the generation of dependencies by scanning the scripts to find symbol definitions. Finally, the script is echoed to the page. A key function here is getVersion. This func- tion looks up the script (in this case menu.js) and returns the filename with the appropriate version appended (e.g., menu_1.0.17.js). In Chapter 3, I mention the advantage of adding a version number to a component's filename; when using a far future Expires header the filename has to be changed whenever the file contents are changed (see the section "Revving Filenames" in Chapter 3). Centralizing this func- tionality inside insertScript is another benefit of this script management module. Whenever a script is modified, all the pages start using the new filename after a sim- ple update to the getVersion code. Pages start using the new version immediately without having to modify any of the PHP templates. Make sure scripts are included only once.

Articles in this issue

Archives of this issue

view archives of flash service - High_Performance_Web_Sites