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.36k stars 9.99k forks source link

Blazor preserving prerended state does not seem to work #35605

Closed dannyBies closed 3 years ago

dannyBies commented 3 years ago

Describe the bug

I'm following the guide at https://docs.microsoft.com/en-us/aspnet/core/blazor/components/prerendering-and-integration?view=aspnetcore-6.0&pivots=server#preserve-prerendered-state to preserve the prerendered component state. However when I try to run the application I get the following error: image

To Reproduce

1 -> Create new Blazor Server template 2 -> Add the <preserve-component-state /> below <script src="_framework/blazor.server.js"></script> in _Host.cshtml 3 -> Change the code in FetchDate.razor to the code mentioned in https://docs.microsoft.com/en-us/aspnet/core/blazor/components/prerendering-and-integration?view=aspnetcore-6.0&pivots=server#preserve-prerendered-state. 4 -> Run application

It seems this issue also occurs using the hosted webassembly template but I'm not familiar enough with that approach to be 100% sure of that.

A few more potential issuesI have noticed:

Further technical details

ASP.NET Core version 6.0.100-preview.7.21379.14 Visual studio community 2022 preview 3.1

guardrex commented 3 years ago

In case this is purely a docs issue ...

Although we've made updates to the Prerendering and integration topic for 6.0, I haven't confirmed the guidance/fixed remaining problems with the coverage. I've noted to do so for this topic on the 6.0 docs tracking issue. I'll keep an 👁️ on this PU issue in the meantime.

javiercn commented 3 years ago

@dannyBies thanks for contacting us.

Would you mind providing a minimal repro as a publicly accessible GH repository to help us move forward? We have automation covering this scenario and its very hard for us to detect if you are missing a step or doing something slightly differently based on the docs.

dannyBies commented 3 years ago

Yeah no problem, I have added the repro here: https://github.com/dannyBies/BlazorPreserveComponentStateRepro.

This repro has been generated using the default blazor server template with VS 2022 17.0.0 Preview 3.1 and sdk 6.0.0-preview.7.21378.6.

The only files that I've changed are:

guardrex commented 3 years ago

@dannyBies ... Was the app not generated from the 6.0 preview project template? _Host.cshtml with that content leads me to believe that's the case. The markup for the layout of a 6.0 preview app would be in _Layout.cshtml.

Here's the spot that I'd expect based on the new project template ...

https://github.com/dotnet/aspnetcore/blob/main/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorServerWeb-CSharp/Pages/_Layout.cshtml#L30

... but I see that you do target net6.0 in the project file and otherwise looks like a 6.0 preview app.

dannyBies commented 3 years ago

I have generated it using the .NET 6.0 (Preview) option and have used this template to use other preview features which work fine. image

I just checked that running dotnet new blazorserver will generate the same template as VS and not the one you've linked. I've also ran dotnet new --update-check to confirm I have the latest version of the templates.

Is there something special I need to do to use the preview templates?

guardrex commented 3 years ago

I'm not sure as I haven't adopted 6.0 preview yet. I tend to adopt them for final doc content check with the release of RC1 (coming soon) due to engineering churn. At RC1, I will check all of (or most of) our 6.0 preview content in local test apps to make sure that we're good on coverage.

I just noticed that it didn't match, so either the SDK that VS is using might be older/different or there's another reason for it ... e.g., a short delay until the latest SDK (or latest VS preview) rolls out given that the PU repo here (or VS preview) can lag the actual appearance of the SDK for download.

Could you generate a Blazor Server project fresh from the command line ... dotnet new blazorserver ... and see if you get a project with _Layout.cshtml. If so, give that a shot with the guidance. If not ... or if it still doesn't work even with _Layout.cshtml ... stand-by here for product unit support.

I'll continue to watch this 👀 as the "doc guy" for a potential update that I can make to our current doc guidance.

dannyBies commented 3 years ago

I can confirm that I don't get a _Layout.cshtml file when using dotnet new blazorserver, I'll see if I can manually copy paste the relevant code from https://github.com/dotnet/aspnetcore/tree/main/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorServerWeb-CSharp later tonight to see if that fixes the issue.

Although I would assume that I would be able to use <preserve-component-state /> even without the latest template. But I'll try it out later to see if that fixes the main issue.

I appreciate the help :)

guardrex commented 3 years ago

I don't think it will turn out to be the cause of the problem. The PU will respond when they get a chance, and I'll update the doc guidance as needed based on what they come up with.

UPDATE: But it turns out that the original feature was Preview 7, which is out now ...

https://github.com/dotnet/aspnetcore/pull/34518

I see you noted that you have 6.0.100-preview.7.21379.14 in your opening remarks.

dannyBies commented 3 years ago

I just had another quick look and I found the issue. In the docs the following needs to be changed: if (!ApplicationState.TryTakeAsJson<WeatherForecast[]>("fetchdata", out var forecasts)) to if (!ApplicationState.TryTakeAsJson<WeatherForecast[]>("fetchdata", out forecasts)) as currently it shadows the forecasts variable. I completely overlooked that!

I'm assuming the issue with my templates not being up to date is because these have not been released yet. I'll have another look when a new update gets released for VS 2022, but for now my issue has been solved. Thanks for the help @guardrex!

guardrex commented 3 years ago

Thanks @dannyBies. The code was dumped into the doc from the blog post. When I get on RC1, all of (or most of) the code examples will be checked. Some of them, including that example, will be improved and loaded into a snippet sample app that will break at runtime on that type of error. I'll get the doc fixed up soon.

In the meantime, ~I'll revert back to the example and use data~ ... actually, there were updates that follow the second blog post, so I'll adopt your approach ...

https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-2/#preserve-prerendered-state-in-blazor-apps

UPDATE: This will be resolved on https://github.com/dotnet/AspNetCore.Docs/pull/23152.

dannyBies commented 3 years ago

No problem at all, I'm working with preview bits after all :)