Performance improvement: Plugins w/ precompiled razor views

6 months ago
This topic was posted a while back and there wasn't any progress...
https://www.nopcommerce.com/en/boards/topic/33123/precompile-razor-views

Was hoping if anyone here has had success loading precompiled razor views within a NopCommerce plugin.

So far I've been successful:
- Updating the Nop.Web project to precompile on publish -- works just fine.
- Updating a plugin to precompiled, and while it does compile the views into the plugin output assembly, the runtime fails to find the view when returning a view (even with custom IViewLocationExpander).

Has anyone been successful precompiling views within your plugin? Thanks for your consideration!

(I realize this post is very ambiguous since there are no code samples. If anyone is interested, I can setup a NopCommerce fork with an example so we can test it out)
5 months ago
Hello did you get it ?
5 months ago
No. It's still backlogged unless anyone here wants to work on it together.
5 months ago
... :( ... well on umbraco was quite easy ... but the solution i get for umbraco didnt work with Nop ...i will continue to work on this !

thank you

Angelo
5 months ago
ok i got the solution :)

1-Create a Startup.cs file in your Plugin project like this

namespace Your.Namespace.To.Your.Plugin
{
    public class Startup : INopStartup
    {                        
        public int Order => 0;
        public void Configure(IApplicationBuilder application)
        {          

        }        
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            _ = services.AddRazorPages().AddRazorRuntimeCompilation();
            var assembly = new EmbeddedFileProvider(this.GetType().Assembly);
            _ = services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
            {
                options.FileProviders.Add(assembly);
            });
        }
    }
}


2-Create your component view Inside ~\Views\Shared\Components\NameOfYourController\Default.cshtml

3-Your view component should only return
return View(model);


4-Your Default.cshtml view should be "Embedded Resource"  "Do not Copy"

and thats it !

please give me feedback if you get it !

Angelo
5 months ago
Sorry not "NameOfYourController"  but "NameOfYourViewComponent" :(

thank you

AS
5 months ago
Hi Angelo!

I looked at your solution, and it does appear to bundle the view with the assembly, however, I'm wondering if the JIT is just extracting the view and being invoked at runtime anyway.

Views, when precompiled, specifically as a Razor SDK project, it compiles them as a resource (not just embedded, but compiled):



When embedding a resource, its just an embedded file in the original format:


When its razor compiled, its my understanding that no additional preprocessing is required and the initial load doesn't have a bit performance hit from the JIT compiler.

When its an embedded resource, its my understanding that the resource is extracted in memory, then compiled by the JIT compiler, same way as if it was stored on disk.

It seems very odd that Nop's team doesn't have a plan for compiled views when the dotnetcore and razor sdk effectively make precompiled views a default within dotnetcore, but the plugin architecture is very much stuck having views stored on disk and not precompiled. In fact, with new versions of the dotnetcore sdk, you have to enable runtime compilation for views on disk to work.

Any thoughts on using the Razor SDK? I'm not stuck on using specifically Razor SDK, it's important to us that we pre-compile views (not necessarily what tooling we use to do so)
5 months ago
Hello there! 🙂

I don't have any specific thoughts on using the Razor SDK in relation to this topic. Additionally, I'm uncertain about the real impact on performance when employing a technique similar to the one mentioned in the documentation. The only difference I encountered in NOP was that I had to change it to 'Embedded Resource,' and unfortunately, I'm not sure why...

Angelo