Murphybro2
Murphybro2

Reputation: 2699

MVC return PartialView slow first time after edit

I imagine the answer to this is something cache related, but to better understand the reason I thought I'd at least ask.

In my MVC project I'm doing an AJAX request to the controller to return a html page as a partial view, I then inject that html into the page with. The reason for this is the model for the view changes depending on what's clicked.

Here is the AJAX: (Please note all code has been modified for simplicity)

$.ajax({
    type: 'GET',
    url: "../../Home/NewMembershipAlloc/",
    contentType: 'application/json',
    dataType: 'html',
    success: function (data) {
        $('#partial_div').html(data);
    }
});

Here is the controller method:

public async Task<ActionResult> NewMembershipAlloc()
{
    return PartialView("~/Views/Popup/GenericPopup.cshtml", new Membership());
}

Now, just to be clear, this all works as intended. Nearly everytime I run this code it takes maybe 20ms, as shown below: enter image description here

However, if I edit my html page in any way, even if I just remove one character and then add it back in. The next time I perform the request it will take 2 seconds (ish).

enter image description here

So this is happening within my development environment which should be the only place my html is edited. It does not happen when I refresh the page, clear the cache or open a new browser. So my questions are the following:

  1. What's the full reason this is happening?
  2. Can I stop it from being slow the first time? Or is it the price I pay for loading new html?
  3. Would this delay ever happen after release? If someone doesn't use the webpage for a few days for example and shuts down their machine maybe.

Upvotes: 1

Views: 1638

Answers (1)

Numli
Numli

Reputation: 407

It looks similar to this question:

https://stackoverflow.com/a/25006809/6862867

In reference to the above link:

The first time a new or edited view is requested the server will parse and compile the code. This takes a small amount of time (but longer than you'd expect for a page load) and is only required once each time the page changes. I suspect that the reason you are seeing it even if you remove a character and then add it back in is that the server does not recognise what has changed until parse/compile time only that the file has changed since the last time it was loaded.

It would seem that you can precompile your project on publish although I'm not clear myself on how this is done/works.

Post release it would happen only the very first time that page was loaded. You could get round this by making that request yourself as soon as you release it.

Hope this helps :)

Upvotes: 2

Related Questions