RequireJS and Cassette Assets

383 views
Skip to first unread message

Barend Bootha

unread,
May 16, 2013, 5:52:57 PM5/16/13
to cass...@googlegroups.com
Hi Guys

Andy first of all if you see this. Fantastic work mate! We've been using Cassette since V1 and very happy with it, will try and get a donation sorted for your project.

td;lr: Using RequireJS want to reference asset without Hash as require has it's own cache bust method.
 
For those of you that have worked with RequireJS would know that RequireJS loads assets from a base url in our case.

<script>
var require = {
baseUrl: '/assets/js',
urlArgs: 'bust=@MvcApplication.BuildVersion' 
}
</script>
 

I can tell Cassette to Bundle all my JavaScript files individually like so

bundles.AddPerIndividualFile<ScriptBundle>("assets/js", new FileSearch
{
SearchOption = SearchOption.AllDirectories
});

That's great because I can now access my asset's like so

http://mycoolproject.localhost/cassette.axd/script/dw46zWvJYSO5BVmBloyvI7zMOmc=/assets/js/mycoolscript.js 

(Yes that is V2 beta 6?) 

However path can't be used with RequireJS, I understand the Hash is a cache buster, but RequireJS has it's own cache buster built in.

What can I do or  How can I access the minified version of my "/assets/js/mycoolscript.js" ?

If we could access the minified script (but it's not cache friendly) like so http://mycoolproject.localhost/cassette.axd/script/assets/js/mycoolscript.js", That is a path that is RequireJS friendly and we don't have to do any trickery in our Apps to use any RequireJS bundlers, nor does our scripts need to have any references, etc etc... Keeping it very very simple, but effective :)

Cheers,
Barend


Daniel Lidström

unread,
May 17, 2013, 5:23:00 AM5/17/13
to cass...@googlegroups.com
I had a similar question a while ago. I wanted to control how the url's are generated. Perhaps you can do the same thing. Here's the suggested method:

 
It is possible to create a class that implements IUrlModifier.

Then register it using:

public class CustomServices : Cassette.ServicesConfiguration
{
    public CustomServices()
    {
        UrlModifierType = typeof(MyHostNamePrepender);
    }
}

Hope this helps!

Barend Bootha

unread,
Jun 9, 2013, 6:49:19 PM6/9/13
to cass...@googlegroups.com
Thanks Daniel

But that solution doesn't quite work for me, not sure what I might be doing wrong.

As soon as I change the URL from

http://mycoolproject.localhost/cassette.axd/script/dw46zWvJYSO5BVmBloyvI7zMOmc=/assets/js/mycoolscript.js 

to 

http://mycoolproject.localhost/cassette.axd/script/assets/js/mycoolscript.js 

with the IUrlModifier the script is not found...

Daniel Lidström

unread,
Jun 10, 2013, 4:57:50 AM6/10/13
to cass...@googlegroups.com
Links in my project are generated like this (normally):


I then added a url modifier like this:

    public class CustomServices : ServicesConfiguration
    {
        public CustomServices()
        {
            UrlModifierType = typeof(MyUrlModifier);
        }
    }

    public class MyUrlModifier : IUrlModifier
    {
        public string Modify(string url)
        {
            var indexOf = url.LastIndexOf('?');
            return indexOf > 0 ? url.Substring(0, indexOf) : url;
        }
    }

Links are now generated like this:


I don't know what your specific problem is, but this appears to work fine for me. I'm using Cassette 2.1.0.

/Daniel


2013/6/10 Barend Bootha <barend...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Cassette" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cassette/3oS2sgCubOQ/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to cassette+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Daniel
Reply all
Reply to author
Forward
0 new messages