Grails Resources Plugin
Alidad Soleimani (Grails developer)Net Jets Inc.
alidadasb https://github.com/alidadasb
Static Resource
• CSS• JS• Images
/web-app/
<link rel="..." href=...>
<g:external dir="css" file="main.css" />
Static Resource
Challenges• Complex Applications• Plugins resources• Dependency between resources• Multiple copies of a resource• Different version of a resource• Order of resources
Managing Dependencies
• Mark Palmer’s blog
Optimization & Performance• Cache resources• Minifying resources• Zipping• Bundling Resources
Performance Golden Rule
• “80-90% of the end-user response time is spent on the frontend. Start there”, Steve Souders
• How can we improve web performance going forward?
14 Rules for Faster-Loading Web Sites
• Rule 1 - Make Fewer HTTP Requests • Rule 2 - Use a Content Delivery Network • Rule 3 - Add an Expires Header • Rule 4 - Gzip Components • Rule 5 - Put Stylesheets at the Top • Rule 6 - Put Scripts at the Bottom • Rule 7 - Avoid CSS Expressions • Rule 8 - Make JavaScript and CSS External • Rule 9 - Reduce DNS Lookups • Rule 10 - Minify JavaScript • Rule 11 - Avoid Redirects • Rule 12 - Remove Duplicate Scripts • Rule 13 - Configure ETags • Rule 14 - Make AJAX Cacheable • “will cut 25% to 50% off response time when users request a page”
Steve Souders
Grails’ static resource management
•2008 - UI Performance Plugin•2009 - Resources Plugin •Asset-pipeline
Introduction to resources plugin
http://grails.org/plugin/resources Authors: Mark Palmer, Luke Daley 2009 initial version 2011 Plugin in 1.3.x 2012 shipped with Grails 2.0 Current stable version 1.2.RC2 Organization : Grails Community
Resources Plugin
A plugin that provides a powerful streamlined asset pipeline
“This plugin represents a new way of declaring and linking to static resources in your application and plugins. Resource dependencies can be declared and pages simply indicate which resource modules they require. The plugin does the rest, and provides a processing pipeline for advanced optimizations.”(Colin Harrington)
Defining Modules• Define inside config.groovy
• Define inside *Resources.groovy
Declaring modulesMyResources.groovy
Referring to Resources
Page1.gsp
Defining Modules• Resources from a given module are loaded in the order they
are defined in the module
• Resources from modules are loaded in module dependency order
Tag Libs• r:external : This tag renders the right kind of links to external
resources, based on their type• r:img • r:layoutResources • r:require • r:resource: This tag returns the URL to use link to a resource.
g:resource will delegate to r:resource automatically
Defining Layout
Main.gsp
The r:require tag tells the framework which resource modules the current GSP requires.
The <r:layoutResources/> is a placeholder for where to include the resources
Referring to Resources
Page1.gsp
Ad-hoc resources• As of Grails 2.0 <img> is aware of resources
5 primary functions • Bundling of resources into modules with dependency
management.
• Processing of resources prior to serving them.
• Tag library to render links to resources in modules.
• Tag library to render links to other resources which are not in modules.
• Service of resources to clients based on appropriate requests.
Processing of resources• Extensible pipeline of mappers
Peter Ledbrook
Phases:
Images:
Debugging
println grailsResourceProcessor.dumpResources()
Other Plugins• Cached Resources• Zipped Resources• Less CSS Resources • Modernizr for Grails
Configuration
YSLOW
YSLOW
Asset-PipeLines• On the fly processing - No more waiting for your assets to
reload after making a change• Compiled assets by generator - No more hanging up
application boot times while processing files. `grails asset-precompile`
• Reduced Dependence - The plugin has compression, minification, and cache-digests built in.
• Easy Debugging - Makes for easy debugging by keeping files seperate in development mode.
• Simpler manifests and taglibs - Read on for more information.
Bundling of Resources into Modules with Dependency Management• define resource modules that have a name and contain one or
more resources• declared in separate files named like ModuleResources.groovy • separate modules block within Config.groovy• The resources can either be local to the application, and
declared with the usual map of attributes, such as dir: and file:, or external to the application, with an absolute url containing ://
Bundling of Resources into Modules with Dependency Management• require tag for the module