Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.200",
"version": "8.0.411",
"rollForward": "latestFeature"
}
}
4 changes: 2 additions & 2 deletions src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>An alternative approach to the new ASP.NET Core SPA templates in .NET 6. It uses YARP as proxy to forward requests to the SPA dev server.</Description>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
Expand All @@ -24,7 +24,7 @@

<ItemGroup>
<None Include="build\**" Pack="true" PackagePath="build\" />
<None Include="..\..\Readme.md" Pack="true" PackagePath="\"/>
<None Include="..\..\Readme.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion src/AspNetCore.SpaYarp/SpaProxyLaunchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void StartInBackground(CancellationToken cancellationToken)
{
if (_launchTask == null)
{
_logger.LogInformation($"No SPA development server running at {_options.ClientUrl} found.");
_logger.LogInformation($"No SPA development server running at {_options.ClientUrl} found. {DateTime.Now}");
_launchTask = UpdateStatus(StartSpaProcessAndProbeForLiveness(cancellationToken));
}
}
Expand Down Expand Up @@ -137,9 +137,11 @@ private async Task StartSpaProcessAndProbeForLiveness(CancellationToken cancella
var livenessProbeSucceeded = false;
var maxTimeoutReached = false;
var httpClient = CreateHttpClient();

while (_spaProcess != null && !_spaProcess.HasExited && !maxTimeoutReached)
{
livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl(httpClient, cancellationToken);

if (livenessProbeSucceeded)
{
break;
Expand All @@ -152,6 +154,7 @@ private async Task StartSpaProcessAndProbeForLiveness(CancellationToken cancella

maxTimeoutReached = sw.Elapsed >= _options.MaxTimeout;
await Task.Delay(1000, cancellationToken);

}

if (_spaProcess == null || _spaProcess.HasExited)
Expand Down
28 changes: 20 additions & 8 deletions src/AspNetCore.SpaYarp/SpaProxyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,35 @@ public SpaProxyMiddleware(

public async Task Invoke(HttpContext context)
{

if (!_spaClientRunning && !await _spaProxyLaunchManager.IsSpaClientRunning(context.RequestAborted))
{
_spaProxyLaunchManager.StartInBackground(_hostLifetime.ApplicationStopping);
_logger.LogInformation("SPA client is not ready. Returning temporary landing page.");
context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
context.Response.ContentType = "text/html";
if (_spaClientRunning)
{ // catch race condition from pending invokes
_logger.LogWarning($"SPA client already started. {DateTime.Now}");
}
else
{
_spaProxyLaunchManager.StartInBackground(_hostLifetime.ApplicationStopping);
_logger.LogInformation($"SPA client is not ready. Returning temporary landing page. {DateTime.Now}");
context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
context.Response.ContentType = "text/html";

await using var writer = new StreamWriter(context.Response.Body, Encoding.UTF8);
await writer.WriteAsync(GenerateSpaLaunchPage(_options.Value));
await using var writer = new StreamWriter(context.Response.Body, Encoding.UTF8);
await writer.WriteAsync(GenerateSpaLaunchPage(_options.Value));
}
}
else
{
_logger.LogInformation($"SPA client is ready.");
_spaClientRunning = true;
_spaClientRunning = true; // set flag before logger call
_logger.LogInformation($"SPA client is ready. {DateTime.Now}");
await _next(context);
}

/**
* This page hasa refresh of 3 seconds, which causes Invoke to get called again
* as the server handles the request
*/
string GenerateSpaLaunchPage(SpaDevelopmentServerOptions options)
{
return $@"
Expand Down