GSP Resources - Reference Documentation
Authors:
Version: 0.4.4
Table of Contents
1 Overview
This plugin allows you to reference GSP-generated files as cacheable static resources. Consider the following:fibonacci.js.gsp
<% fib = {n ->; return n <= 1 ? n : fib(n-1) + fib(n-2)} %> var fibonacci = [<%= (0..10).collect{ fib.call(it) }.join(',') %>];
fibonacci.js
var fibonacci = [0,1,1,2,3,5,8,13,21,34,55];
2 Installation
The preferred method for installing plugins is to add a plugin dependency ingrails-app/conf/BuildConfig.groovy
, i.e.:plugins { // Other plugin dependencies here // . . . // . . . runtime ':gsp-resources:latest.integration' }
grails install-plugin gsp-resources
3 Usage
In your project resources file, define GSPs as resources like any other resource type:'style' { resource url:'css/test.css.gsp' }
'style' { resource url:'somefile.gsp', attrs:[rel: 'stylesheet/css', type:'css'] }
attrs
parameter, which is used to tell
the resources plugin the type of the resource being generated. (In this example,
the compiled file will simply be named 'somefile' with no extension.)
4 Limitations
Since this plugin is meant to serve static resources, there is no inherent data-watching within GSPs. Changes to a GSP file itself will trigger recompilation, but changes to the data referenced within a GSP will not.5 Advanced Configuration
5.1 Delayed Processing
One limitation of theresources
plugin in connection with GSP compilation
is that the processing of static resources takes place during server startup.In fact, the processing could take place before a lot of the core grails
infrastructure has been loaded. If your GSP resources contain <g:include />
tags, for example, it is possible the grails support for loading other groovy
pages will not yet have been loaded when the resources
plugin starts processing
your GSP. This will result in errors!To get around this, the gsp-resources
plugin provides an additional configuration
option, for delaying the start of resources processing during server startup. The
following configuration option should be placed in grails-app/conf/Config.groovy
:grails.resources.processing.startup = "delayed"
grails.resources.processing.startup = false
Bootstrap.groovy
:class BootStrap { // Automatically injected def grailsResourceProcessor def init = { servletContext -> // Do whatever preparation you need first // E.g. create some objects that your GSPs will need // . . . // . . . // Finally, start resources processing grailsResourceProcessor.start() } def destroy = { }}
If nograils.resources.processing.startup
configuration option is found inConfig.groovy
, or an invalid value is used, then the resources plugin defaults to its normal startup.
5.2 Simple HTML Cache
One potential use of thegsp-resources
plugin is to cache the HTML output
of some controller action as a static .html
file. Note that the .html
file
will be created on server startup, and will not change after that.Example
We are going to cache the output ofhttp://localhost:8080/myapp/about/me
1. Create a new directory under web-app
We will use web-app/cache
in this example, but any directory name is fine.2. Configure resources plugin to include new directory
Make the following change tograils-app/conf/Config.groovy
// Note we have added '/cache/*' to the existing list grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*', '/cache/*']
3. Configure resources plugin to delay processing
Make the following change tograils-app/conf/Config.groovy
grails.resources.processing.startup = "delayed"
4. Create GSP to cache controller output
Add the below content toweb-app/cache/aboutme.html.gsp
<g:include controller="about" action="me"/>
5. Add new GSP to resources
plugin configuration
Make the following change to grails-app/conf/ApplicationResources.groovy
modules = { aboutme { resource url:'/cache/aboutme.html.gsp' } }
6. Check results
You should now be able to view the cached file athttp://localhost:8080/myapp/cache/aboutme.html
You will need to restart your server first, so that theresources
plugin incorporates the config change setting made in Step #2. Unfortunately, the current version ofresources
(v1.2) cannot hotload changes to this setting.Note, this server restart is only required once - i.e. when first adding the new/cache/*
adhoc pattern to the resources config. From then on, theresources
plugin will automatically intercept URLs of the formmyapp/cache
and map them to the actual URL of the requested resource.
6 Version History
Version | Date | Description |
---|---|---|
0.4.4 | Sep 29, 2013 | - Bugfix: incompatible with resources 1.2.1 - Bugfix: .css.gsp resources broken on Windows due to JVM bug |
0.4.3 | Sep 18, 2013 | Bugfix: ensure plugin loads after resources plugin |
0.4.2 | Sep 13, 2013 | Enhancements/Fixes: - Allow rendered GSPs in bundles - Fix broken dependencies due to grails 2.3 - Fix compatibility with Servlet 2.5 and 3.0 |
0.4.1 | Jul 6, 2012 | Bugfix: preserve type from Resources.groovy configuration |
0.4 | Jun 25, 2012 | Enhancements/Fixes: - Support all types of GSP output (e.g. HTML) without special configuration - Support grails.resources.debug config setting- Generated files now saved in ~/.grails , like other resources plugins |
0.31 | Mar 23, 2012 | Made compatible with Servlet 2.5 (already 3.0) |
0.3 | Mar 21, 2012 | Enhancements/Fixes: - Fix bug causing createLink to break after using gsp-resources- Fix bug causing resource(dir:"/") to render incorrect context path- Automatically detect compiled file type from filename - Support <r:require/> , <g:include/> etc. in GSPs- Support GSP resource files located in plugins - Allow delaying resources processing until bootstrap phase - Process resources in order of dependsOn config |
0.2.1 | Oct 25, 2011 | Set included resources version to 1.1.1, dependsOn to '1.0.2 > *' |
0.2 | Oct 22, 2011 | Fixed g:createLink Made plugin compatible with 2.0RC1. |
0.1 | Oct 15, 2011 | Initial implementation |