I am working on minifying js and css files in grails application. My original plan is to use the resources plugin to minify the resources (also had a look at jawr and performance-ui, but resources seems to be de facto standard these days).
Resources makes it easy to minify individual CSS files using YUI, but we have over 40 JS files, which we'd like to concatenate into a single file (and the files will need to be concatenated in the right order too) I haven't seen anything suggesting that Resources supports this out of the box, these are the approaches we have planned so far :
Add new grails taglib to concatenate the js and css files to create one js and one css file and minify using the resources plugin. A naive implementation will mean the yui-minify runs every time the page is served (!!) so we'd need to inytroduce caching somehow.
Use the BuildConfig 's grails.war.resources to minify the js and css. This would get round the caching issue, as the resource would only bebuilt and minified at build time, but will require us to use grails run-war to test locally, hence any minification-related errors won't get caught until later in the dev cycle.
This must be a fairly common problem. What are other people doing? Would like to hear about any other approaches or best practices I can use.
You can make all your resources using the same bundle, with this, you will have only one merged js. Example:
main {
resource id: 'mainjs', url: 'js/main.js'
defaultBundle: 'mybundle'
}
second {
resource id: 'secondjs', url: 'js/second.js'
defaultBundle: 'mybundle'
}
According to the docs:
The "bundle" mapper adds together resources of the same type to reduce
the number of files your client pages request.
The "bundle" mapper looks at the value of the "bundle" property on
resources and if found, will add the resource to a new synthetic
aggregated resource.
This aggregated resource is itself processed through mappers, so it is
subject to the other optimisations you apply to the kind of resource
the bundle is aggregating.
Bundles are always in the base directory of the static resources
folder - which means references to files inside the bundle must be
re-adjusted so they continue to refer to the same files. This is made
possible for CSS files by the csspreprocessor and cssrewriter mappers.
Related
I have at least 15+ javascript libraries & references in my MVC web application project. For each of these libraries they are independently bundled and minified. This means that when a page is requested the client browser is having to make 15+ connections to the server to retrieve resources.
Would it be considered bad practice to bundle all of these related files into a maximum of say 5 bundles so that the number of requests is kept low even though the scripts have nothing to do with each other and are completely unrelated?
Normally we would end up with few bundles. Some are
put all the scripts which are used again and again on ifferent pages as common.js
page type specific bundles, like in an eCommerce site we would potentially have productpage.js and checkout.js
In my Grails project, I use the Resource plugin to control and manage all resource files in application.
Its is embedded to my project like this:
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.8.3"
runtime ":resources:1.2"
}
But the problem is: this plugin just loads the resource files when the most first request comes up, so it make the response of that request (in my situation is the login request) is too long. So is there any solution to force the resource plugin to run all its own necessary processes right after the application started, no need to wait for the first request?
Thank you so much.
Resources Plugin allows you to manage your resources in an efficient and modular way. Meaning you can load resources that are required for each pages. If your resources are taking too long maybe you are loading unnecessary css,js on your login page.
Couple approaches you can take:
Optimize your resources. Use only what you need. In you SomethingResources.groovy, you can bundle your resources and load each bundle when needed. For example, required only css,js needed for login.
Use zipped-resources and cached-resources, or yui-minify-resources plugin. They help you to transfer or load resources faster.
Resources plugin allows to load resources in different part of the page, sometimes loading js files at the end of the page, simulates a faster loading page. disposition
Btw, Resources plugin pre-process css, js files at the start of your application, however unless there is a request from the client browser, resources would not be transferred to client. Optimizing them help to ease this transition.
I hope this will help
I've got a Grails 2.2.1 application that makes use of the Twitter-Bootstrap plugin, which supplies the basic CSS and javascript elements of Bootstrap to Grails applications, along with a tag lib and some other features.
The thing is, I want to use a custom CSS file that offers our company colors, and right now I can only do that by pulling in our own CSS from the app that overrides the elements in the plugin's own bootstrap.css file, meaning that the plugin loads its CSS file first and the app loads its own secondly. And these files are very big and heavy.
My users, especially those on mobile devices, don't need the burden of an extra 125k of CSS along with the extra processing that comes with doing each rule twice.
Short of building my own custom version of the plugin, is there anything I can do that will prevent the plugin's bootstrap.css file from getting into the web page with my own boostrap.css file?
The plugin uses resources to declare the twitter bootstrap files. Luckily resources have a option of overriding definitions of declared modules.
So in your ApplicationResources.groovy, add:
modules = {
overrides {
'bootstrap-css' {
resource id: 'bootstrap-css', url:'/css/mycustombootstrap.css'
}
}
}
I have an MVC 4.5 project that has most of the UI logic organized in jQuery plugins. I want to protect my code by minification and bundling (While I understand that minification will only do so much as far as protection, it's better than leaving formatted and documented source files on the server.)
Ideally, I want my dev server to work as is -- files are non-minified and separated. But, when I deploy to the production server, I want the source files to be removed and only minified bundles to be available. Also note, on many occasions my jQuery plugins load other plugins from JavaScript code (I use head.js), so I cannot use #Script.Render for that.
What technologies do I use -- built-in MVC bundling, SquishIt, Bundler or do I need to resort to MSBuild and Microsoft Axaj Minifier? To recap, I want to remove source JS files and just be left with minified bundles in production, and, preferably, find a way to not change head.js references based on whether files are minified or not.
Thanks for your advice.
Just thought I respond with what I ended up doing here:
To recap: I wanted to obfuscate my source files with minification while not exposing the source JS files in production. I also wanted for head.js to resolve source file URLs to bundle URLs:
Put all non-minified javascript files in a folder viewable only to Admin role
Used bundling built-in to ASP.NET MVC 4.5 to generate bundles
Pointed my head.js tag to an MVC controller that returned head.js code + a javascript array with an x-ref between raw URLs and bundle URLs (available from BundleTable static object)
Bundling occurs outside of ASP.NET membership, so bundles are generated and available to anonymous users even though the source files are in the folder only accessible by Admin. Then, the trick of dynamically augmenting head.js code with server-side generated bundle URLs takes care of calling bundles from JS files.
I understand how to use asp.net's new bundling and minification features. They are helpful during development.
Is there any benefit to using them in a production deployment though? Would the system perform better if you just placed the bundled/minified files on the web server? It seems that overall, less code would run if they were just static files.
Note: I understand the benefit of having js/css bundled and minified. I am only questioning the value of using an active runtime process to generate those files in a production system as opposed to simply storing them on disk and referencing them as static files.
Bundling and Minification is more useful in production than in development.
It can significantly improve your first page hit download time.
Bundling reduces the number of individual HTTP requests to server by combining multiple CSS files and Javascript files into single CSS file and javascript file.
Minification reduces the file download size of CSS and javascript files by removing whitespace, comments and other unnecessary characters.
Such small advantages are more pronounced in a production environment than in development. So it is better to go with Bundling and Minification in production.
Specific to your question there is no palpable benefit in bundling/minification during runtime. This feature is there just to make the developer's work easier. So it is even better to go with manually bundled/minified assets in production if you are sure about what you are doing.
Update:
According to MSDN there is a real benefit in bundling/minification during runtime
Bundling and minification in ASP.NET 4.5 is performed at runtime, so that the process can identify the user agent (for example IE, Mozilla, etc.), and thus, improve the compression by targeting the user browser (for instance, removing stuff that is Mozilla specific when the request comes from IE).`
The power of dynamic bundling is that you can include static JavaScript, as well as other files in languages that compiles into JavaScript.`
For example, CoffeeScript is a programming language that compiles into JavaScript
Bundling and minification provide 2 basic functionality in order to improve the performance of page load.
Bundling - Bundle all the provided scripts/ CSS in one file so that only browser need to load one file instead of multiple.
Note-> Generally browsers can may only 6 simultaneous requests to get resources from the server. Additional requests are queued by the browser for later processing. Hence, if we have multiple files then it may have to wait in the request queue.
Minification - Minification process generates a minified file by removing comments, extra white spaces and renames the variable names. So this reduces the file size and results in faster download.
Minification- smaller files, less kb on the wire, faster page load.
Bundling- browsers limit connection per http host. This means that a user goes to your page, and you have (let's say) 24 script and link (css) tags, your browser is handling them 6 (most browser's limitation) at a time - slowing the page load.
Bundling makes the browser treat all your files a single file - overriding this limitation.
Another benefit of bundling is it reduces caching issues. When we use bundling its loading to the page with a key, like below.
<script src="/bundles/jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1"></script>
Each time we change our scripts it generates different key. So the file will be cached if we change something. But when we don't use this since script file has the same name, sometimes we have to clear cache to see the change.