dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

Feature Request: Need for a bool IsPrerendered check in ComponentBase #12162

Closed AmarjeetBanwait closed 5 years ago

AmarjeetBanwait commented 5 years ago

In case of Prerendering, Component is rendered twice. So in this condition this will be a good idea if we have a check IsPrerendered in Component base. It makes no sense to render a component twice. This check will facilitate us to do things like

 protected override async Task OnInitAsync()
    {
         if ( !IsPrerendered )
         {
             forecasts = await Http.GetJsonAsync<WeatherForecast[]>("api/SampleData");
         }
     }

or simply

protected override void OnInit()
    {
         if( IsPrerendered )
         {
             CanReRender = false;      //will prevent re-rendering of whole component
         }
    }
mkArtakMSFT commented 5 years ago

Thanks for contacting us, @AmarjeetBanwait. When a component is being prerendered, the rendering happens once only anyway, so there is no value in such a check. If, however, you still believe you're facing an issue, where the component gets rendered twice, please share a repro project so we can investigate that.

AmarjeetBanwait commented 5 years ago

Thanks @mkArtakMSFT Here is the repro link. Prerendering Issue

  1. Just build and run.
  2. Goto /fetchdata page
  3. Refresh page from browser button.

You will see weather forecast table refreshing again after rendering.

  1. Add breakpoint in SampleData Controller. Controller will hit 2 Times.

  2. Not sure following issue will reflect on your end or not After refreshing from browser reload button Date Format in the table will be different and after 2nd render Date will be shown differently

1st render Date format: 07-18-2019 Rerender Date format: 07/18/2019

mkArtakMSFT commented 5 years ago

Thanks for the details, @AmarjeetBanwait. Looks like you're experiencing this behavior on client-side Blazor. This is actually expected and by-design.

AmarjeetBanwait commented 5 years ago

Can you please tell why this behavior is necessary in client side? Because in cases when we are fetching large data on render it makes no sense fetching it twice.

mkArtakMSFT commented 5 years ago

@SteveSandersonMS can you answer this question please? Thanks

SteveSandersonMS commented 5 years ago

When you have a client-side Blazor application that is also prerendered on the server, you have two completely separate instances of the application:

They have to be separate instances, because how could they not be? They run on different machines. As such, whatever you tell it to do, it's going to do twice.

Theoretically it could be possible for the server-side instance to fetch the data and then write it out as part of the initial HTTP response somehow (e.g., in an <input type=hidden> or similar), and then the client-side app could pick up the data from there instead of doing its own outbound HTTP request to fetch it. But that sort of thing can't be done automatically - you would have to write code to make that happen.