40

HTML5 has an async attribute for script files, to enable async loading.

<script type="text/javascript" src="myScript.js" async></script>

I can take advantage of this with my MVC4 bundling by referencing the bundle like so.

<script type="text/javascript" src='@Scripts.Url("~/bundles/jquery")' async></script>

But what this does mean is my scripts are bundled even when in debug mode.

So how can I take advantage of bundling and the async attribute without loosing non-minification when in debug.

1 Answer 1

69

If you upgrade to the 1.1-alpha1 release, you can just add the async attribute to the tag format either via:

Scripts.DefaultTagFormat = @"<script src=""{0}"" async></script>"

or passing it where you want the async tag

Use following instead of Scripts.Render("~/bundles/jquery")

Scripts.RenderFormat(@"<script src=""{0}"" async></script>", "~/bundles/jquery")
Sign up to request clarification or add additional context in comments.

5 Comments

System.Web.Optimization 4.0 seems to have removed both DefaultTagFormat and RenderFormat, disallowing custom script tag output (using AssetManager.RenderScriptTag directly).
@E-Null The current release now supports both the above methods if you just do PM> Install-Package Microsoft.AspNet.Web.Optimization
Where do I need to write those lines ?( one of them..) And what need upgrade to 1.1 -alpha? the MVC version or the Web Optimization ? Thanks...
This breaks CDN fallbacks using CdnFallbackExpression. The fallback check will be executed before the deferred script is loading causing the fallback to always kick in.
I don't see an answer for the question "Where do I need to write those lines?" Specifically, where do we need to place the Scripts.DefaultTagFormat = line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.