aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Hot Reload for Blazor

Open danroth27 opened this issue 8 years ago • 144 comments

danroth27 avatar Jan 25 '18 17:01 danroth27

See https://github.com/aspnet/blazor/issues/193 for status updates on this work item.

SteveSandersonMS avatar Mar 16 '18 17:03 SteveSandersonMS

For now, we can use dotnet watch run and recompile every time when the change occurs. Just using this in csproj file : <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" /> <Watch Include="**\*.cshtml"/>

martasp avatar Mar 23 '18 22:03 martasp

We've hit a snag with live reload for 0.2.0, so moving this out until we can work through a more robust design.

danroth27 avatar Apr 04 '18 18:04 danroth27

Hi, I'm using dotnet sdk 2.2.100-preview1-009349 and blazor 0.5.1 under Mac. Live reload doesn't work using "dotnet blazor serve". If I change some html markup in a cshtml file, the app doesn't reload itself and ever after a manual browser reload, the app display the old html content. How can I solve this?

ghost avatar Sep 06 '18 16:09 ghost

@danroth27, what's https://github.com/aspnet/AspNetCore/issues/4056 then? Should it be closed?

mkArtakMSFT avatar Mar 20 '19 22:03 mkArtakMSFT

Few questions! 1 Does this track live reload for both server side blazor and client side blazor? 2. Will live reload ship in go live release (i.e in net core 3.0) ? 3. Will live reload mechanism lose page state (I.e equivalent to an f5 refresh) or will it behave similar to Hot Module Replacement in javascript land - I.e only the changed Components UI will re-render? If the latter, will there be a mechanism to preserve component state on the client between updates?

dazinator avatar May 11 '19 02:05 dazinator

Does this track live reload for both server side blazor and client side blazor?

Yes

Will live reload ship in go live release (i.e in net core 3.0) ?

For .NET Core 3.0 we expect to support auto rebuild based on file changes, but you will still need to manually refresh the browser.

Will live reload mechanism lose page state (I.e equivalent to an f5 refresh) or will it behave similar to Hot Module Replacement in javascript land - I.e only the changed Components UI will re-render? If the latter, will there be a mechanism to preserve component state on the client between updates?

We don't currently have a plan for supporting hot module replacement in a way that preserves client state.

danroth27 avatar May 13 '19 23:05 danroth27

We don't currently have a plan for supporting hot module replacement in a way that preserves client state.

At least, not automagically. Theoretically if you follow a Redux-like architecture, or anything else that strictly decouples state from display, then you could serialize that state out before unload and restore on reload. However this isn't something we're planning to bake in as a feature, as not everyone wants to follow that kind of architecture.

SteveSandersonMS avatar May 14 '19 10:05 SteveSandersonMS

then you could serialize that state out before unload and restore on reload.

Thanks. Please, once ready, would you be able to document the appropriate hooks (before unload / reload etc) provided in the design to facilitate this. I'd like to start on an implementation / helper nuget package to enable this pattern for those that want it!

dazinator avatar May 14 '19 18:05 dazinator

Couldn't get the dotnet watch run to work, tried following and other options too,

dotnet watch --project "Portfolio.Client" run --project "Portfolio.Server"

Just ended up with the following crude solution using nodemon:

npx nodemon --watch "Portfolio.Client" -e razor,css,html,cs --exec 'dotnet run --project "Portfolio.Server"'

ahmad2smile avatar May 26 '19 09:05 ahmad2smile

I thought i was supposed to run: dotnet watch --project BlazorTest.Client run But that gave me an error.

If I used: dotnet watch --project BlazorTest.Server run

With the following in the project BlazorTest.Server.csproj file:

<ItemGroup>
    <Watch Include="..\**\*.razor" />
    <Watch Include="..\**\*.scss" />
    <Watch Include="..\**\*.cs" />
</ItemGroup>

It picked up changes in the BlazorTest.Client project and restarted the server, so I only had to do a manual refresh in the browser.

yberstad avatar May 30 '19 13:05 yberstad

It picked up changes in the BlazorTest.Client project and restarted the server, so I only had to do a manual refresh in the browser.

Does that mean the server restarts everytime there is a a css, html change?

dazinator avatar May 30 '19 14:05 dazinator

@dazinator, yes :-)

yberstad avatar May 30 '19 16:05 yberstad

.. ok just checking but that is a bad thing right? I.e server restart should be unnecessary for a html or css file change as a browser refresh (with cache invalidated) should suffice?

dazinator avatar May 30 '19 17:05 dazinator

You are right, that is not necessary. Just add or remove the file extensions you are interested in within the <ItemGroup>. Updated my answer to avoid confusion.

yberstad avatar May 30 '19 17:05 yberstad

Sorry if off-topic, is there anyway to live reload from Visual Studio right now (Blazor client-side)? Right now for every change excluding wwwroot files, I have to Build the project (Ctrl Shift B) and reload the browser. Would be wonderful if VS can auto build on saving changes.

datvm avatar Jun 08 '19 05:06 datvm

@datvm We've enabled this for server-side Blazor projects, but we need to do some work to enable this again for client-side Blazor projects and Razor class libraries. It will probably be a bit before we get to this as we are focused right now at shipping .NET Core 3.0.

danroth27 avatar Jun 09 '19 02:06 danroth27

For client side, you can probably use anything that just does a full page refresh when something changes on the server. Shameless plug: check out NetPack - run the sample project and navigate to the /BrowserReload example: https://github.com/dazinator/NetPack/blob/develop/src/NetPack.Web/Views/Home/BrowserReload.cshtml - that might help you. If not, there are other solutions out there for triggering a client side page reload based on server side file changes if you can't wait for an out of the box solution.

dazinator avatar Jun 09 '19 10:06 dazinator

Thanks for the solution, may actually be useful for someone else. For me it will just be a QoL improvement, nothing critical. Still love the product and the effort everyone is putting in. Extra pressing of Ctrl Shift B works for me (now).

datvm avatar Jun 09 '19 11:06 datvm

My solution currently looks like this: I have a separate exe BlazorDebugLauncher which gets started by launchsettings.json parameterized with the port and hostname that the browser shall be opened. It then

  • starts a separat small exe DebugAttacher which detaches and reattaches Visual studio
  • starts dotnet watch run on the project folder
  • refreshes the browser as soon as the server process is up (works fine with MS Edge :-) )

If anyone is interested, I can put this somewhere...

AdmiralSnyder avatar Jun 12 '19 20:06 AdmiralSnyder