﻿#### 

#### Introduction

There's a common way to optimize the startup of your Sitecore XP website: precompile the views with [RazorGenerator.MsBuild](https://www.nuget.org/packages/RazorGenerator.MsBuild). 

It can really speed up the startup time of your website.
And there are already plenty of articles how to implement it in Sitecore application: 

- [Precompiled Razor Views](https://bigredmachine.github.io/post/sitecore-precompiled-razor-views-fix) by Ian Jones.
- [Precompiled Views and Custom Theme Engine for Sitecore](https://errorcotidianam.wordpress.com/2021/04/23/precompiled-views-and-custom-theme-engine-for-sitecore) by José Neto.
- [Sitecore 9 View Precompilation](https://olmecdev.com/2019/Precompiling-Sitecore-Views) by Olivier McNicoll.
- [Precompiled Views with Sitecore 8.2](https://kamsar.net/index.php/2016/09/Precompiled-Views-with-Sitecore-8-2) by Kam Figy.

I followed the approach described in all the articles, but there were some adjustments required to make it all working in our project. 

So I decided to share my findings with you. Here you will find all the steps required to make Sitecore XP MVC Views Precompilation work in your project.

#### Steps

The steps are pretty easy:

##### 1. Add RazorGenerator.MsBuild NuGet package

First of all, you need to add the [RazorGenerator.MsBuild](https://www.nuget.org/packages/RazorGenerator.MsBuild) NuGet package to your projects.

In all your projects (`.csproj`) where you have `.cshtml` files, add the following line:

```
<PackageReference Include="RazorGenerator.MsBuild" Version="2.5.0" PrivateAssets="all" />
```

*Note*: We use `Packages.props` file with [`Microsoft.Build.CentralPackageVersions`](https://github.com/microsoft/MSBuildSdks/blob/main/src/CentralPackageVersions/README.md) package in our solution, so we added it as this:

**Packages.props**

```
<ItemGroup>
    <!-- ... Other imports ... -->
    <PackageReference Update="RazorGenerator.MsBuild" Version="2.5.0" />
</ItemGroup>
```

**`.csproj` file**

```
<ItemGroup>
    <!-- ... Other imports ... -->
    <PackageReference Include="RazorGenerator.MsBuild" />
</ItemGroup>
```

##### 2. Sitecore settings

Ensure `Mvc.UsePhysicalViewsIfNewer` Sitecore setting is set to `true` for **production** environment. But for **development** environment it should be set to `false` (so you can upload updated views to the server, so changes will be reflected immediately).

#### Notes

That's the way you can make Sitecore XP MVC Views Precompilation work in your project. But there are some potental issues that you should be aware of.

##### If `web.config` namespaces are not working

You can define default namespaces for your views in the `Views\web.config` file: [more details](https://stackoverflow.com/a/7586619/8918395). E.g.:

```
<?xml version="1.0"?>
<configuration>
  <!-- ... -->
  <system.web.webPages.razor>
    <!-- ... -->
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Sitecore.Mvc" />
        <add namespace="Sitecore.Mvc.Presentation" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <!-- ... -->
</configuration>
```

But it may happen it won't work with Views precompilation because of Razor implementation details.
If it doesn't work and you get compilation errors, you can try to add the following line anywhere in `.csproj` file:

```
<!-- System.Web.Mvc, Version=5 -->
```

I descibed this in RazorGenerator Github issue: [#211](https://github.com/RazorGenerator/RazorGenerator/issues/211#issuecomment-2539934760).

The full working import in `csproj` file:

```
<ItemGroup>
    <!-- ... Other imports ... -->

    <!-- System.Web.Mvc, Version=5  DO NOT REMOVE: razorgenerator picks this up as an mvc project -->
    <PackageReference Include="RazorGenerator.MsBuild" />
</ItemGroup>
```

##### Duplicated views in Solution Explorer

I faced the following issue: all the views were shown multiple times in the Solution Explorer.

![duplicated views in Rider](https://www.brimit.com/-/media/images/blog/sitecore-views-precompilation/duplicated-views.png)

If you also have duplicated views in Solution Explorer (like I had in Rider), you can disable precompilation by default, but enable it only when you deploy the solution to the server.

I used `Directory.Build.props` file in the root of the solution to achieve this:

```
<Project>
    <!-- this is used to hide duplicated items in the solution explorer -->
    <PropertyGroup>
        <PrecompileRazorFiles Condition=" '$(PrecompileRazorFiles)' == '' ">false</PrecompileRazorFiles>
    </PropertyGroup>
</Project>
```

This will disable precompilation by default, but when the solution is deployed, it should be enabled.

- If you are using `msbuild` cmd to deploy, pass this param to it:

    ```
    /p:PrecompileRazorFiles=true
    ```
- or with `.pubxml` file:

    ```
    <Project>
      <PropertyGroup>
          <PrecompileRazorFiles>true</PrecompileRazorFiles>
      </PropertyGroup>
    </Project>
    ```

#### Results

Enabling views precompilation significantly reduced the startup time of the website (in all environments: local, dev, staging, production).

Here are some examples of the startup times:

- **PROD CM** now: 77 sec, before: 669 sec
- **PROD CD** now: 165 sec, before: 1094 sec

As you can see, precompilation really improved performance of the website. If you are not using it already - you should try it out.

###### Author

[!\[Igor Zharikov - Senior Sitecore Developer, Sitecore MVP\](https://www.brimit.com/-/jssmedia/feature/blogs/authors/igor-zharikov-2.jpg?h=700&amp;iar=0&amp;w=700&amp;hash=292A84E8CB3E5CE57CA743385CFB1464)
Igor Zharikov
Sitecore MVP / Senior Sitecore Developer](https://www.brimit.com/blog/author?authors=Igor%20Zharikov)

#### More on Sitecore

[!\[How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2024/vercel_cover-image.png)
#Guides#How-toDXPE-commerce
##### How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects
Optimize and accelerate the development and deployment of complex multisite Sitecore projects.
Alexei Vershalovich on July 17, 2024](https://www.brimit.com/blog/how-vercel-will-help-you-save-effort-when-deploying-sophisticated-sitecore-projects)

[!\[Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2023/sitecore-mentoring---cover-image.png)
#How-toDXP
##### Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story
How to participate in the Sitecore Mentor program and help younger colleagues jump-start a career in Sitecore development.
Sergey Baranov on October 2, 2023](https://www.brimit.com/blog/training-up-tomorrows-sitecore-mvps)

[!\[Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2022/headless/adobestock_456986731.jpg)
#How-toDXPE-commerce
##### Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)
Discover how a headless CMS can benefit organizations that use Sitecore.
Daniil Raschupkin, Palina Trokhautsava on September 15, 2022](https://www.brimit.com/blog/going-headless-part-2-when-a-headless-cms-is-your-best-bet-if-you-have-sitecore)

![](https://bat.bing.net/action/0?ti=187017043&amp;tm=gtm002&amp;Ver=2&amp;mid=091a6ac1-7ff0-46ce-b4bb-5074e41e1d00&amp;bo=2&amp;gtm_tag_source=1&amp;pi=0&amp;lg=en-US&amp;sw=800&amp;sh=600&amp;sc=24&amp;nwd=1&amp;tl=Sitecore%20XP%20MVC%20Views%20Precompilation%3A%20Make%20Startup%204%2B%20Times%20Faster&amp;kw=sitecore,mvc,razor,precompilation&amp;p=https%3A%2F%2Fwww.brimit.com%2Fblog%2Fsitecore-mvc-views-precompilation&amp;r=&amp;lt=259&amp;evt=pageLoad&amp;sv=2&amp;asc=D&amp;cdb=AQAY&amp;rn=742168)