Skip to content

Commit 86998b6

Browse files
committed
Fix live reload
1 parent 30bfb17 commit 86998b6

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/tooling/docs-builder/Http/DocumentationWebHost.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public DocumentationWebHost(ILoggerFactory logFactory, string? path, int port, I
3434
{
3535
_writeFileSystem = writeFs;
3636
var builder = WebApplication.CreateSlimBuilder();
37-
DocumentationTooling.CreateServiceCollection(builder.Services, LogLevel.Warning);
37+
DocumentationTooling.CreateServiceCollection(builder.Services, LogLevel.Information);
3838

3939
_ = builder.Logging
4040
.AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Error)
4141
.AddFilter("Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware", LogLevel.Error)
42+
.AddFilter("Microsoft.AspNetCore.Routing.EndpointMiddleware", LogLevel.Warning)
43+
.AddFilter("Microsoft.AspNetCore.Http.Result.ContentResult", LogLevel.Warning)
4244
.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Information);
4345

4446
var collector = new LiveModeDiagnosticsCollector(logFactory);
@@ -92,7 +94,6 @@ private void SetUpRoutes()
9294
_ = _webApplication
9395
.UseLiveReloadWithManualScriptInjection(_webApplication.Lifetime)
9496
.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions())
95-
//.UseMiddleware<NoInjectLiveReloadMiddleware>()
9697
.UseStaticFiles(
9798
new StaticFileOptions
9899
{

src/tooling/docs-builder/Http/LiveReload.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace Westwind.AspNetCore.LiveReload;
2323
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresDynamicCode", Justification = "Manually verified")]
2424
public static class LiveReloadMiddlewareExtensions
2525
{
26-
public static IServiceCollection AddAotLiveReload(this IServiceCollection services,
27-
Action<LiveReloadConfiguration> configAction)
26+
public static IServiceCollection AddAotLiveReload(this IServiceCollection services, Action<LiveReloadConfiguration> configAction)
2827
{
2928

3029
var provider = services.BuildServiceProvider();
@@ -68,9 +67,9 @@ public static IApplicationBuilder UseLiveReloadWithManualScriptInjection(this IA
6867

6968
if (config.LiveReloadEnabled)
7069
{
71-
var webSocketOptions = new WebSocketOptions()
70+
var webSocketOptions = new WebSocketOptions
7271
{
73-
KeepAliveInterval = TimeSpan.FromSeconds(300),
72+
KeepAliveInterval = TimeSpan.FromSeconds(300)
7473
};
7574
_ = builder.UseWebSockets(webSocketOptions);
7675

@@ -80,7 +79,6 @@ public static IApplicationBuilder UseLiveReloadWithManualScriptInjection(this IA
8079
var middleWare = new NoInjectLiveReloadMiddleware(next, webApplicationLifetime);
8180
return middleWare.InvokeAsync(context);
8281
});
83-
LiveReloadFileWatcher.StartFileWatcher();
8482

8583
// always refresh when the server restarts...
8684
_ = LiveReloadMiddleware.RefreshWebSocketRequest();

src/tooling/docs-builder/Http/ReloadGeneratorService.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
using Microsoft.Extensions.Hosting;
66
using Microsoft.Extensions.Logging;
7+
using Westwind.AspNetCore.LiveReload;
78

89
namespace Documentation.Builder.Http;
910

10-
public sealed class ReloadGeneratorService(
11-
ReloadableGeneratorState reloadableGenerator,
12-
ILogger<ReloadGeneratorService> logger) : IHostedService,
13-
IDisposable
11+
public sealed class ReloadGeneratorService(ReloadableGeneratorState reloadableGenerator, ILogger<ReloadGeneratorService> logger) : IHostedService, IDisposable
1412
{
1513
private FileSystemWatcher? _watcher;
1614
private ReloadableGeneratorState ReloadableGenerator { get; } = reloadableGenerator;
@@ -23,7 +21,9 @@ public async Task StartAsync(Cancel cancellationToken)
2321
{
2422
await ReloadableGenerator.ReloadAsync(cancellationToken);
2523

26-
var watcher = new FileSystemWatcher(ReloadableGenerator.Generator.DocumentationSet.SourceDirectory.FullName)
24+
var directory = ReloadableGenerator.Generator.DocumentationSet.SourceDirectory.FullName;
25+
Logger.LogInformation("Start file watch on: {Directory}", directory);
26+
var watcher = new FileSystemWatcher(directory)
2727
{
2828
NotifyFilter = NotifyFilters.Attributes
2929
| NotifyFilters.CreationTime
@@ -50,10 +50,11 @@ public async Task StartAsync(Cancel cancellationToken)
5050
private void Reload() =>
5151
_ = _debouncer.ExecuteAsync(async ctx =>
5252
{
53-
Logger.LogInformation("Reload due to changes!");
5453
await ReloadableGenerator.ReloadAsync(ctx);
5554
Logger.LogInformation("Reload complete!");
56-
}, default);
55+
56+
_ = LiveReloadMiddleware.RefreshWebSocketRequest();
57+
}, Cancel.None);
5758

5859
public Task StopAsync(Cancel cancellationToken)
5960
{
@@ -66,26 +67,27 @@ private void OnChanged(object sender, FileSystemEventArgs e)
6667
if (e.ChangeType != WatcherChangeTypes.Changed)
6768
return;
6869

70+
Logger.LogInformation("Changed: {FullPath}", e.FullPath);
71+
6972
if (e.FullPath.EndsWith("docset.yml"))
7073
Reload();
7174
if (e.FullPath.EndsWith(".md"))
7275
Reload();
7376

74-
Logger.LogInformation("Changed: {FullPath}", e.FullPath);
7577
}
7678

7779
private void OnCreated(object sender, FileSystemEventArgs e)
7880
{
81+
Logger.LogInformation("Created: {FullPath}", e.FullPath);
7982
if (e.FullPath.EndsWith(".md"))
8083
Reload();
81-
Logger.LogInformation("Created: {FullPath}", e.FullPath);
8284
}
8385

8486
private void OnDeleted(object sender, FileSystemEventArgs e)
8587
{
88+
Logger.LogInformation("Deleted: {FullPath}", e.FullPath);
8689
if (e.FullPath.EndsWith(".md"))
8790
Reload();
88-
Logger.LogInformation("Deleted: {FullPath}", e.FullPath);
8991
}
9092

9193
private void OnRenamed(object sender, RenamedEventArgs e)

0 commit comments

Comments
 (0)