Skip to main content

JAWR and RequireJS

  4 posts   Feedicon  
Replies: 3 - Last Post: June 27, 2014 18:29
by: brienwheeler
showing 1 - 4 of 4
Posted: June 24, 2014 21:40 by brienwheeler
Hi, I've been using JAWR for a long time and love it.

I want to have JAWR serve up bundled, minified JS for a Backbone-based client-side MVC app that uses RequireJS to manage dependencies.

However, the gzip_Nxxxxxx prefix for the bundled JS is throwing off RequireJS (I've been led to understand that RequireJS needs a constant URL and that cache busting should be done with a query parameter instead).

Is there a way to configure JAWR to use a URL like /bundles/foo.js?cb=gzip_Nxxxxxx instead of /gzip_Nxxxxx/bundles/foo.js?

Thanks in advance.
Brien
Posted: June 26, 2014 14:17 by brienwheeler
I solved this myself by extending JavascriptBundleTag/JavascriptHTMLBundleLinkRenderer to conditionally (based on a tag attribute "useQueryParam=true") render the links as /bundles/foo.js?jawrHC=gzip_N1234 instead of /gzip_N1234/bundles/foo.js and extending JawrSpringController to detect the query parameter and rewrite the request URI/URL back to its original form before sending in to JawrRequestHandler.

I made another enhancement to the Javascript tag to allow the tag consumer to specify other attributes that should be copied into the resulting tag. For example, <blwjawr:jawrscript src="/bundles/foo.js" otherAttrs="data-main=&quot;src/app.js&quot;"/> results in an HTML output tag of <script src="/gzip_N1234/bundles/foo.js" data-main="src/app.js"/>. I needed this for supporting a Backbone application.

If the JAWR project wants to incorporate these enhancements I can send along diffs. I didn't update the JAWR servlet for the useQueryParam functionality (since I only use the JawrSpringController), but that would be trivial.

Cheers,
Brien
Posted: June 27, 2014 08:04 by icefox
Hi Brien,

Thanks for providing your feedback on this.

I'm interested in increasing the support for RequireJS in Jawr if possible.

I've got few questions.

As RequireJS works as a client-side library, how do you reference your bundle on the client side?
How does it works in debug mode as you don't have one bundle, but a bunch of javascript files?

Your help is really appreciated.

Cheers,
icefox
Posted: June 27, 2014 18:29 by brienwheeler
Hi icefox,

For other reasons I am including one dependent library as a separate JSP tag and then trying to prevent RequireJS from re-downloading it:

<blwjawr:jawrscript src="/bundles/lib1.js" userQueryParam="true"/>
<blwjawr:jawrscript src="/bundles/myApp.js" otherAttrs="data-main=&quot;app/main.js&quot;"/>

where the Backbone app in /bundles/myApp.js has a RequireJS specifier for /bundles/lib1.js.

At the moment I haven't completely ironed out my RequireJS integration. Right now the first tag correctly renders a script link as "/bundles/lib1.js?jawrHC=gzip_N1234" and the browser downloads the library. But then RequireJS comes along and asks for "/bundles/lib1.js" which gets a not found error.

My idea is that instead of returning not found for /bundles/lib1.js, to implement a redirect from /bundles/lib1.js to /gzip_N1234/bundles/lib1.js. (Essentially this is saying that if you ask for an unversioned copy of a bundle, JAWR will redirect you to the current version.)

If this still downloads twice (because the browser thinks one is /bundles/lib1.js?jawrHC=gzip_N1234 and another is /gzip_N1234/bundles/lib1.js) then I will change my JawrSpringController integration to also redirect instead of transparently rewriting the URL and servicing the first request. I will do some further exploration early next week and if I get to a solution I'm happy with I'll contact you again.

And I do still have to figure out something reasonable for debug mode.

Cheers,
Brien

Replies: 3 - Last Post: June 27, 2014 18:29
by: brienwheeler
 
 
Close
loading
Please Confirm
Close