David Ruckman, Senior Consultant
I wanted to share my experience precompiling views in my Sitecore 9.1 website in case someone else out there wants to try it. Spoiler alert - it's worth the effort.
I'm working on a site where almost every page is composed of dozens of dynamic MVC components, which made the pages unacceptably slow to load the first time you visit the page. This is because every view would be compiled at runtime on the first request for the view. In an effort to reduce this startup time I decided to try to pre-compile the entire website. Precompiling your site has several advantages including dramatically reducing page startup times, and improving code quality (because your views are checked for errors at compile time), and preventing rogue developers from pushing .cshtml changes to your production site.
On the downside it takes a bit longer to build and deploy the solution, but that was not a deal-breaker for me.
I decided to precompile my Content Delivery website and NOT my Content Management site, because precompiling the CM comes with it's own set of challenges (especially with SPEAK views), and my main concern was CD performance. I also decided that my local development site would NOT be precompiled to speed development. Any view compilation errors would be detected during continuous integration builds.
My site has the following characteristics that are relevant to my precompilation effort:
Helix solution architecture
Coveo Hive site search and search-driven components
Azure pipelines to build and deploy
First, I did the easy part - updating my Azure pipeline to precompile the site. This was accomplished by adding a couple more MSBuild arguments to my CD build pipeline. Both of these params are required:
PrecompileBeforePublish=true -- Directs MSBuild to precompile all the views found in the solution.
EnableUpdateable=false -- Directs MSBuild to include the view's markup in the compiled assembly, thus preventing updates to the view after compilation.
You can read the full article by clicking here.