Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute URL in ASP bundle

I use a jQuery library for Google Maps, and it depends on the Google scripts to be loaded first. I'd like to be able to include both in the bundle as such:

  bundles.Add(new ScriptBundle("myfoobundle").Include(
    "http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places",
    "~/scripts/jquery.fooplugin-{version}.js"
  ));

This doesn't seem to work (throws an exception complaining about the first string). And one may say that this shouldn't work because that absolute URL is not meant to be minified/bundled.

But the current approach is a hassle, as I need to ensure that the dependencies are correct, and that happens in different places (half the problem in the bundling code, the other half in the view).

Would be nice to have a 1-step solution as above. Do I have any options in this regard?

UPDATE:

To address the comments regarding using a CDN as a solution: if I specify bundles.UseCdn = true it has no effect, and I still get the exception The URL 'http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places' is not valid. Only application relative URLs (~/url) are allowed. Also I'm unsure what the implication of doing that is in the first place, because I already use CDN support for jQuery, etc., so unsure how that would conflict with my use case.

like image 885
Bobby B Avatar asked Dec 03 '12 11:12

Bobby B


3 Answers

If you are using a version of System.Web.Optimization >= 1.1.2, there is a new convenient way of overriding the url's for Styles and Scripts. In the example below, I am grabbing a CdnBaseUrl from web.config to use as the base url for all scripts and stylesheets:

public class BundleConfig
{
    private static readonly string BaseUrl = ConfigurationManager.AppSettings["CdnBaseUrl"];

    public static void RegisterBundles(BundleCollection bundles)
    {
        // This is the new hotness!!
        Styles.DefaultTagFormat = "<link href=\"" + BaseUrl + "{0}\" rel=\"stylesheet\"/>";
        Scripts.DefaultTagFormat = "<script src=\"" + BaseUrl + "{0}\"></script>";

        bundles.Add(new ScriptBundle("~/bundles/js").Include(
            "Your scripts here..."
        ));

        bundles.Add(new StyleBundle("~/bundles/css").Include(
            "Your css files here..."
        ));
    }
}

More info on static site (CDN) optimization

like image 124
Noah Heldman Avatar answered Nov 15 '22 02:11

Noah Heldman


Currently you would have to include a local copy of the jquery that you are depending on inside of the bundle, or you would have to manage the script tags as you mention. We are aware of this kind of depedency management issue and it falls under the category of asset management which we are tracking with this work item on codeplex

like image 42
Hao Kung Avatar answered Nov 15 '22 02:11

Hao Kung


Based on the MVC tutorials, your syntax is incorrect for creating a bundle from a CDN. And as others have said, ensure that you have the bundles.UseCdn = true; property set. Using the example on the MVC site - your code should reflect the following:

public static void RegisterBundles(BundleCollection bundles)
{
   bundles.UseCdn = true;   //enable CDN support
   //add link to jquery on the CDN
   var jqueryCdnPath = "http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places";
   bundles.Add(new ScriptBundle("myfoobundle", jqueryCdnPath).Include(
                "~/Scripts/jquery-{version}.js"));
}
like image 7
Tommy Avatar answered Nov 15 '22 03:11

Tommy