(Quick Reference)

5 Advanced Configuration - Reference Documentation

Authors:

Version: 0.4.4

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.