Skip to content

Commit

Permalink
Wait for WebApp build to be ready in development mode (#233)
Browse files Browse the repository at this point in the history
### Summary & Motivation

In development mode it often happens that the WebApp build folder is
being created as the server starts up - the UseStaticFiles middleware
throws an exception if the folder doesn't exist.

This is only an issue in development mode.

### Checklist

- [x] I have added a Label to the pull-request
- [x] I have added tests, and done manual regression tests
- [x] I have updated the documentation, if necessary
  • Loading branch information
tjementum authored Nov 25, 2023
2 parents c61c338 + da4bec8 commit 862af57
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions application/shared-kernel/ApiCore/Middleware/WebAppMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,18 @@ public static IApplicationBuilder UseWebAppMiddleware(
var buildRootPath = GetWebAppDistRoot(webAppProjectName, "dist");
var templateFilePath = Path.Combine(buildRootPath, "index.html");

if (!File.Exists(templateFilePath) &&
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "development")
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "development")
{
var tryUntil = DateTime.UtcNow.AddSeconds(10);
while (!File.Exists(templateFilePath))
{
if (DateTime.UtcNow > tryUntil) break;
Debug.WriteLine($"Waiting for {webAppProjectName} build to be ready...");
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}

if (!File.Exists(templateFilePath))
{
throw new FileNotFoundException("index.html does not exist.", templateFilePath);
}
Expand Down

0 comments on commit 862af57

Please sign in to comment.