Skip to content

Conversation

@MackinnonBuck
Copy link
Member

@MackinnonBuck MackinnonBuck commented Jun 8, 2023

[Blazor WASM] Don't apply hot reload deltas if Blazor has not been initialized

Fixes an issue where if Blazor hot reload deltas are received before Blazor is initialized, hot reload fails. VS interprets this case as a fatal error, requiring the customer to restart the app to get hot reload working again.

Description

This fix works by added a check to see if window.Blazor._internal.applyHotReload is defined before attempting to apply hot reload deltas. Upon first glance, it might seem that this means Blazor won't ever receive the skipped deltas, but this is not the case because Blazor applies all existing deltas after it boots.

Another possible fix for this would be for VS to not interpret the "delta not applied" case as fatal. However, I think it's preferable to apply the fix in WebSocketScriptInjection.js because there still might be cases where exceptions thrown from Blazor's JS implementation are "fatal" in nature. I think it's natural for WebSocketScriptInjection.js to differentiate between cases where deltas were intentionally skipped vs. deltas could not be applied.

We could alternatively report the "skipped" case differently from the "success" case so that we can get insight via telemetry about how often this happens.

Fixes https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1779975

@MackinnonBuck MackinnonBuck requested a review from a team as a code owner June 8, 2023 17:52
@ghost ghost added Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch untriaged Request triage from a team member labels Jun 8, 2023
@ghost
Copy link

ghost commented Jun 8, 2023

Thanks for your PR, @MackinnonBuck.
To learn about the PR process and branching schedule of this repo, please take a look at the SDK PR Guide.

.then(response => {
if (response.status == 200) {
const etag = response.headers['etag'];
window.sessionStorage.setItem('blazor-webasssembly-cache', { etag, deltas });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an extra 's' here, meaning we never used the cached hot reload deltas 🙃

Even with this fix, the caching mechanism is broken because session storage stores its values as strings. So we'll need to stringify the deltas here and parse them when they get read back in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'm surprised this has lived for so long. I'm now desperately waiting for the next fix which should hopefully give noticeable perf boost.

@MackinnonBuck MackinnonBuck requested a review from BillHiebert June 8, 2023 18:45
Copy link
Contributor

@mkArtakMSFT mkArtakMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MackinnonBuck!
Do we have to backport this change now to .NET 7 & 6 ?

@mkArtakMSFT mkArtakMSFT removed the untriaged Request triage from a team member label Jun 8, 2023
@MackinnonBuck
Copy link
Member Author

@mkArtakMSFT Yes, this should probably be backported. I'll go ahead and do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants