Search code examples
javascriptcssf#websharper

Managing resources in a sitelet


I'm beginning my trek in learning WebSharper, and I'm finding managing resources (e.g. css and javascript files) a bit confusing.

The documentation on this gives an example resource declaration

type MyResource() =
    inherit Resources.BaseResource("http://my.cdn.net",
       "file1.js", "file2.js", "file3.css")

and you can place a [<Require>] attribute on the assembly to make the resource appear on every page on the site:

[<assembly: Require(typeof<MyResource>)>]
do ()

The documentation goes on to say that the attribute can be placed on modules, types and (module level) let bindings and WebSharper will build a dependency graph to figure out if a given page (Action) needs the dependency or not. I've tried a few things (using a Twitter Bootstrap resource declaration), but the only way I've gotten to make this work is the assembly attribute.

Can someone give an example (or provide a link) of how to do properly use the [<Require>] attribute so that it gets added on one Action, but not another?

For reference, here's by Twitter Bootstrap resource declaration:

[<Require(typeof<JQuery.Resources.JQuery>)>]
[<Sealed>]    
type BootstrapResource() =
    inherit Resources.BaseResource("https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/", 
                                   "css/bootstrap.min.css", "js/bootstrap.min.js")

(I get that the [<Require]> attribute here here makes it a dependency of the Bootstrap resource, although I think WebSharper includes JQuery anyways, which would make it redundant.)


Solution

  • Having [<Require(typeof<JQuery.Resources.JQuery>)>] is not redundant as it tells WebSharper to always include JQuery before the currently defined resource.

    Require attribute on assembly level in a referenced dll will not automatically add the resource to the page returned for a specific Action, only if the assembly referenced from one of the Web.Control types used for the page. It works the same for a smaller scope: [<Require(typeof<MyResource>)>] on a module, function, or type.