5 Advanced Configuration - Reference Documentation
Authors:
Version: 0.4.4
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.