Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS/JS bundle in single file in mvc when publish with release option

I have created MVC application. When I publish the application on Azure with release option, all css and js file load in a single bundle in page. (Open view source of page then displays a single link for css).

When I publish a site with Debug option in publish profile then all CSS load individual.

My problem is when publish site with release option theme not load correctly, but with debug option theme loads correctly. I want to publish my application with Release option only. If anyone face this issue before and get any solution then please help me.

like image 423
mmpatel009 Avatar asked Jul 18 '14 10:07

mmpatel009


People also ask

Which of the following is the correct way to bundle CSS files in MVC?

The following example shows how to combine multiple CSS files into a bundle. public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles. Add(new StyleBundle("~/bundles/css"). Include( "~/Content/bootstrap.

What bundle would you use to minify CSS and/or JavaScript on a production environment?

Click on wwwroot folder, select the css file you want minified, then right click and choose bundler & minifier. Then from popup minify file. It will be the same file name with the minified version.


1 Answers

I have experienced this before when using bundling.

Say for instance your css file is located at: /Content/css/css.css

This css file then makes a reference to another file, or for example an image at /Content/images/image1.png via url('../images/image1.png').

You then set up your css bundle @ /bundles/css.

All appears great in debug mode. However, when you set <compilation debug="false" .... in your web.config, suddenly the references made in the css file breaks. If you open your console in Firebug/Chrome dev tools and check the network tabs, you'll see resources failing to load, from an incorrect URL.

This happens because when debug mode is off, all the files are bundled and minified like they would be in production. In this case, the CSS file would be bundled and served from the URL /bundles/css. This results in the relative URL reference breaking. Where it once referenced /Content/images/image1.png, it now references /images/image1.png.

You have a few options to solve this:

  1. Serve your bundled css files from the same folder as the actual css files. eg. /Content/css/cssbundle. This can become very tedious quickly.
  2. Change all relative references in your css files to absolute references. eg. ../images/image1.png would become /Content/images/image1.png. This does mean you can't use a lot third party CSS bundled out of the box, you would have to check/change relative references if you wanted to bundle them.
  3. Use the BundleTransformer nuget package. It automatically transforms relative urls to absolute ones during the bundling process.

    The main differences of StyleTransformer and ScriptTransformer classes from a standard implementations: ability to exclude unnecessary assets when adding assets from a directory, does not produce the re-minification of pre-minified assets, support automatic transformation of relative paths to absolute in CSS-code (by using UrlRewritingCssPostProcessor), etc.

I personally recommend 3 as it is the easiest to maintain long term.
like image 70
Sean Amos Avatar answered Sep 19 '22 11:09

Sean Amos