(Quick Reference)

GSP Resources - Reference Documentation

Authors:

Version: 0.4.4

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(',') %>];

This is then cacheable and serveable as a javascript file as:

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 in grails-app/conf/BuildConfig.groovy, i.e.:

plugins {
    // Other plugin dependencies here
    // . . .
    // . . .

runtime ':gsp-resources:latest.integration' }

Alternatively, you can install manually with the following command:

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'
}

The GSP will be compiled to a file with exactly the same URI, but minus the '.gsp' extension. The type of the compiled file (i.e. in this case 'css') will be automatically detected from the filename. Should you wish to name your file with a file extension that is not recognised by the resources plugin, you can optionally explicitly define the file type of the compiled resource using the following example configuration:

'style' {
    resource url:'somefile.gsp', attrs:[rel: 'stylesheet/css', type:'css']
}

Note the additional 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 the resources 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"

This will delay resources processing until the bootstrap phase - by which point all required plugins and artefacts should be fully loaded.

For even finer control, you can disable resources processing during server startup entirely, with the following configuration:

grails.resources.processing.startup = false

You can then manually trigger the initial resources processing by adding the following code to your project's 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 no grails.resources.processing.startup configuration option is found in Config.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 the gsp-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 of http://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 to grails-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 to grails-app/conf/Config.groovy

grails.resources.processing.startup = "delayed"

4. Create GSP to cache controller output

Add the below content to web-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 at http://localhost:8080/myapp/cache/aboutme.html

You will need to restart your server first, so that the resources plugin incorporates the config change setting made in Step #2. Unfortunately, the current version of resources (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, the resources plugin will automatically intercept URLs of the form myapp/cache and map them to the actual URL of the requested resource.

6 Version History

VersionDateDescription
0.4.4Sep 29, 2013- Bugfix: incompatible with resources 1.2.1
- Bugfix: .css.gsp resources broken on Windows due to JVM bug
0.4.3Sep 18, 2013Bugfix: ensure plugin loads after resources plugin
0.4.2Sep 13, 2013Enhancements/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.1Jul 6, 2012Bugfix: preserve type from Resources.groovy configuration
0.4Jun 25, 2012Enhancements/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.31Mar 23, 2012Made compatible with Servlet 2.5 (already 3.0)
0.3Mar 21, 2012Enhancements/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.1Oct 25, 2011Set included resources version to 1.1.1, dependsOn to '1.0.2 > *'
0.2Oct 22, 2011Fixed g:createLink
Made plugin compatible with 2.0RC1.
0.1Oct 15, 2011Initial implementation

7 Special Thanks

Stefan Kendall for creating the original gsp-resources plugin, which was superb and inspired me to get involved to help make it even better!

Marc Palmer for the excellent resources plugin, without which this plugin would not be possible.