0

I'm trying to implement bundling in Sitecore 8.2 and followed all the steps in this document.

The breakpoint is hit for the RegisterBundles method, but the assets are not rendered on page.

In the layout, it only renders this:

<link href="/bundles/css?v=" rel="stylesheet"/>

The processor specified in the document did not work for me. The breakpoint wasn't being hit. I changed it to this:

patch.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
   <pipelines>
      <initialize>
         <processor patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.ShowVersion, Sitecore.Kernel']"
            type="MyProject.Web.Pipelines.RegisterPlatformBundles, MyProject" />
      </initialize>
   </pipelines>
</sitecore>
</configuration>

RegisterPlatformBundles.cs

private void RegisterBundles(BundleCollection bundles)
        {
            /// Bundled CSS 
            bundles.Add(new StyleBundle("~/bundles/css").Include(
                    "~/Assets/css/main.css?v=202001236",
                    "~/Assets/css/dev.css?v=202001231"
                ));            
        }

Layout.cshtml

<head>
   @Styles.Render("~/bundles/css")
</head>

UPDATE:
@Mahendra's suggestion worked for me. I removed the versions from the css references.
Also, to fix the incorrect image/font paths, I changed the RegisterBundles method to this:

private void RegisterBundles(BundleCollection bundles)
{
   /// Bundled CSS 
   bundles.Add(new StyleBundle("~/bundles/css")
            .Include("~/Assets/css/main.css", new CssRewriteUrlTransform())
            .Include("~/Assets/css/dev.css", new CssRewriteUrlTransform()));            
}
5
  • 1
    did you try by adding ~ i.e ~/bundles/css? also, try by removing the query from the include section – Mahendra Shekhawat Jan 28 '20 at 14:51
  • @MahendraShekhawat. In the Layout.cshtml, that is how I added. Are you referring to something else? – Qwerty Jan 28 '20 at 14:54
  • Sorry for the confusion here, I mean here is ~//, a double slash in layout. are you getting any 404 error for the link? try to open <link href="/bundles/css?v=" rel="stylesheet"/> (href) in new tab – Mahendra Shekhawat Jan 28 '20 at 15:00
  • Removing the version worked. It now loads as <link href="/bundles/css?v=blNztwxsF5QM3OLfN6fFdLya8YpG5qMfrbHhfcIUFP81" rel="stylesheet"/>. But how do I add the versions for a particular file here. – Qwerty Jan 28 '20 at 15:04
  • glad it works, StyleBundle class required a physical file and do not accept any query string. updated the same in the answer. – Mahendra Shekhawat Jan 28 '20 at 16:42
5

I just had this happen, the issue is that Sitecore is picking on the bundling URL. Need to add your /bundles/css to the IgnoreUrlPrefixes setting

<setting name="IgnoreUrlPrefixes" value="/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/sitecore/service/xdb/disabled.aspx?|/bundles/css" />
2

ASP.NET MVC API includes StyleBundle class that does CSS minification and bundling. If it is not working, then there a few things which we need to check -

  • Make sure all files exists as a physical file, a virtual path will not work since it is not created yet
  • Styles.Render have a proper path in the layout
  • Make sure render link (virtual path) is not throwing any error, if it is then there may be some error in code.
  • Remove any version query from the file path

you can easily check the all included files by enabling and disabling the Bundling and minification by setting the value of the debug attribute debug="true" in the compilation element in the Web.config file.

7
  • Please advice how to add a version to a particular file (either css/jss) – Qwerty Jan 29 '20 at 5:00
  • ?v=* is used to makes sure you are getting the latest version of the CSS or js file from the server in a link tag and not a cached copy. Since StyleBundle runs server-side and includes files as physical locations, you can't get a file with a version by the query string. All you need to add the version in the file name such as "main_202001236.css". – Mahendra Shekhawat Jan 29 '20 at 7:44
  • Where to add ?v=* is unclear. Main_2020.css will make the server look for a file with that name. right, whereas the file present is main.css. Could you please clearly how to add a particular file with the latest version. – Qwerty Jan 29 '20 at 7:52
  • ?v=* will only work in <link> tag. yes, the server will look for the file name. now if the main.css file is there then it will be only and latest file there because OS will not allow the multiple files with the same name. – Mahendra Shekhawat Jan 29 '20 at 8:07
  • Your suggestion did not work. The actual file name is main.css. In the method, i changed it to main_2020.css & now it doesn't load. Is that what you suggested. – Qwerty Jan 29 '20 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.