Programming Microsoft ASP.NET 4 - Dino Esposito [167]
Beyond this, consider that script files are rich with white spaces and blanks. Simply removing these characters can cut a significant percentage of software fat out of the file. This is just what minifiers are for.
A minifier is a tool that parses a given script file and rewrites it in a way that is functionally equivalent to the original but devoid of any unnecessary characters. The jQuery library is commonly served in its minified form. A minified script file is nearly impossible to read or understand for a human, so I don’t recommend using minified files during development.
Microsoft released a minifier tool; you can get it at http://aspnet.codeplex.com/releases/view/40584. This tool can work on script and CSS files. Also, in addition to removing white spaces and blanks, it safely attempts to reduce curly brackets and to make variable names shorter.
Note
You might want to look at build-time minifier tools such as Chirpy because having to manually minify several files can be a bit of a pain. See http://chirpy.codeplex.com.
Localized Scripts
Like other Web resources, scripts can be subject to localization. At the very end of the day, a script is a relatively long string of text, so there’s really nothing that prevents you from embedding a script into the application resources along with a plain RESX file.
The method GetWebResourceUrl on the ClientScript property of the Page class can be used to return the URL to any resource stored in a satellite (localized) assembly. In this way, you link your scripts from the assembly, deploy the localized assembly, and you’re done.
The only other alternative you have is maintaining different copies of the script and resolve the name programmatically. In ASP.NET 4, the ScriptManager control can streamline this task quite a bit. Here’s how to use the script manager component:
When the property EnableScriptLocalization is true, the The value of the page property UICulture determines the culture code being used to mangle the file name. When configuring the ScriptManager control, you indicate the supported cultures through the ResourceUICultures property on individual script references. If a related file is missing, you’ll get a 404 error for the request. Otherwise, the markup will be emitted to target the language-neutral script file. Using Cascading Style Sheets and Images The first consideration to make is that the more requests you make, the more your users are likely to wait to see the page. Aggregating multiple scripts in a single (but larger) file is relatively easy and effective. It is doable for CSS files too; but with images? How can you combine multiple images to be used in distinct areas of the page and then reference just the section you need and where you need it? Grouping Images into Sprites A sprite is a single image that results from the composition of multiple images that are stored side by side, forming a grid of any size you like. You then link the image URL to any
Cascading style sheets and images are the remaining two-thirds of the auxiliary static resources around most Web pages. Some consolidated techniques also exist to minimize the impact of these resources on your pages.
To reduce the number of HTTP requests that a page requires in order to fetch all the images it needs, you use sprites. tag where you need a section of it and use CSS styles to specify exactly which portion you want in a given place. Here’s an example: