The new static resources framework provides declarative resource management and optimization in Grails applications. The resources plugin allows resources like CSS, JavaScript, and images to be declared and then processed and optimized at runtime. This includes bundling, minification, caching, and more. The plugin uses a mapping pipeline to modify resources according to configurable mappers before delivery. This provides a major improvement over prior approaches by automating resource handling and optimization.
13. Only at startup!
Works in any environment
Can be bypassed
Supports reloading
Tiny trade-off
14. Installing it
marcmacbook:AwesomeApp marc$ grails install-plugin resources
Welcome to Grails 1.3.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /usr/local/grails-1.3.1
Base Directory: /Users/marc/Projects/AwesomeApp
Resolving dependencies...
Dependencies resolved in 1172ms.
Running script /usr/local/grails-1.3.1/scripts/InstallPlugin.groovy
Environment set to development
Installing zip ../checkout/Resources/grails-resources-1.0-RC1.zip... ...
[mkdir] Created dir: /Users/marc/.grails/1.3.1/projects/AwesomeApp/plugins/
resources-1.0-RC1
[unzip] Expanding: /Users/marc/Projects/checkout/Resources/grails-resources-1.0-
RC1.zip into /Users/marc/.grails/1.3.1/projects/AwesomeApp/plugins/resources-1.0-RC1
Installed plugin resources-1.0-RC1 to location /Users/marc/.grails/1.3.1/projects/
AwesomeApp/plugins/resources-1.0-RC1. ...
Resolving plugin JAR dependencies ...
Executing resources-1.0-RC1 plugin post-install script ...
Plugin resources-1.0-RC1 installed
marcmacbook:AwesomeApp marc$
29. We love the debug
Development reloads
Turn it all off: add ?_debugResources=y
Cache defeat: add ?_refreshResources=y
X-Grails-Resources-Original-Src:
/bundle-grailsDefaults.js, /js/application.js, /
js/prototype/prototype.js
35. CSS rewriting
Before
/css/main.css:
body {
Both CSS and image background-image:
url(../images/bg.png);
may be renamed }
and/or moved After
/bundle_main.css:
body {
background-image:
url(../changed.png);
}
38. Cached Resources
grails install-plugin cached-resources
Renames to SHA256 digest of contents
Shortens name to base62 encoding
Flattens directory structure
Sets Expires to 1 year