Search code examples
grailsgrails-plugingrails-2.0

Placement Of JS Resource Via <r:external />


I'm using the Resources plugin in Grails 2.0.1. The issue I'm having is getting a JavaScript resource specified using r:external to be placed after all of the other script previously declared using either r:require or r:external, all in the deferred disposition. Currently the resource specified using r:external it is being output in the location where the r:external tag is placed.

I have a layout file that contains an r:require tag to fetch some core resources:

<html>
<head>
    ...
    <r:require module="core" />
</head>
....
</html>

Then a GSP that includes another r:require tag followed by an r:external tag:

<head>
    ...
    <r:require module="forms" />
    <r:external dir="js" file="page-specific-resource.js" /> %{-- specifying disposition="defer" has no effect --}%
    ....
</head>
...

My expectation is each of the JavaScript resources I'm trying to include would be output in the deferred disposition with the core resources first, the forms resources next, and the page-specific resource last. The actual results are that the core and forms resources are output as expected in the deferred disposition, but the page-specific resource is output in head, where the r:external tag is placed (Specifying disposition="defer" seems to have no effect).

Is my expectation incorrect or is this a legitimate issue? Is there another way to specify page-specific resources (I'm trying avoid declaring these types of resources in the resource DSL) and have the positioned after all previously declared resources?


Solution

  • As answered by Marc Palmer on the Grails User mailing list (http://grails.1312388.n4.nabble.com/Placement-Of-JS-Resource-Via-lt-r-external-gt-td4506074.html#none):

    I'm afraid your expectation is incorrect.

    r:external is just for rendering links where you invoke it.

    You need to declare your dependencies, or put your r:external at the end of the page.

    Declaring modules for "functional areas" of your app is profitable. It means you can also bundle them together if need be, or have fine grained control and the page no longer needs to be aware of that.