Hot Reload for Blazor
- [x] Mono Method Body Replacement
- [ ] Build performance optimization
- [ ] Via
dotnet watch - [ ] Development middleware (websocket connection to receive updates)
See https://github.com/aspnet/blazor/issues/193 for status updates on this work item.
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"/>
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.
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?
@danroth27, what's https://github.com/aspnet/AspNetCore/issues/4056 then? Should it be closed?
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?
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.
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.
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!
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"'
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.
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, yes :-)
.. 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?
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.
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 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.
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.
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).
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...