I have a site that dynamically generates Javascript. The generated code describes type-metadata and some server-side constants so that the clients can easily consume the server's services - so it's very cacheable.
The generated Javascript is served by an ASP.NET MVC controller; so it has a Uri; say ~/MyGeneratedJs
.
I'd like to include this Javascript in a Javascript bundle with other static Javascript files (e.g. jQuery etc): so just like static files I want it to be referenced separately in debug mode and in minified form bundled with the other files in non-debug mode.
How can I include dynamically generated Javascript in a bundle?
With
VirtualPathProviders
this is now possible. Integration of dynamic content into the bundling process requires the following steps:Writing the logic that requests / builds the required content. Generating content from Controller directly requires a bit of work:
Implement a virtual path provider that wraps the existing one and intercept all virtual paths that should deliver the dynamic content.
You also have to implement CacheDependency if you need it:
Register your virtual path provider:
Optional: Add Intellisense support to your views. Use
<script>
tags within your View and let them be removed by a custom ViewResult:Use an extension method or add an helper function to your controller:
The steps are similiar for other type of dynamic contents. See Bundling and Minification and Embedded Resources for example.
I added a proof of concept repository to GitHub if you want to try it out.
Darin is right, currently bundling only works on static files. But if you can add a placeholder file with up to date content, bundling does setup file change notifications which will detect automatically when the placeholder file changes.
Also we are going to be moving to using VirtualPathProviders soon which might be a way to serve dynamically generated content.
Update: The 1.1-alpha1 release is out now which has support for VPP
This is not possible. Bundles work only with static files.