ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
750 stars 113 forks source link

IIS published Blazor .NET8 app fails to deliver styles and scripts on first device connection #292

Open ynnob opened 8 months ago

ynnob commented 8 months ago

Hello!

We have a Blazor app running .NET 8 on a Windows Server published through Visual Studios IIS publish option.

We used WebOptimizer in our Program.cs and then started noticing that

  1. After Successful Publishing the webpage thats been opened by Visual Studio does not display any styles or scripts (F12 shows that the files are not delivered by the server 404|500)
  2. Devices like phones that open a link to this blazor app for the first time will also fail to load the script and styles
  3. When reloading the page (F5) the styles and scripts can be loaded

After some digging in the Windows Error logs i saw several errors popping up for each of the failed files that the browser tried to download.

App.razor: <link rel="stylesheet" href="/css/app.min.css" />

Example for app.min.css

Category: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
EventId: 1
SpanId: 2ea13da234f83e9f
TraceId: 54fdcae62c87d46c65c9a3e720204362
ParentId: 0000000000000000
RequestId: 800005f7-0000-ef00-b63f-84710c7967bb
RequestPath: /css/app.min.css

An unhandled exception has occurred while executing the request.

Exception: 
System.UnauthorizedAccessException: Access to the path 'C:\inetpub\sites\<IIS_PROJECT_FOLDER>\obj\WebOptimizerCache' is denied.
   at System.IO.FileSystem.CreateDirectory(String fullPath, Byte[] securityDescriptor)
   at System.IO.Directory.CreateDirectory(String path)
   at WebOptimizer.AssetResponseStore.AddAsync(String bucket, String cachekey, AssetResponse assetResponse)
   at WebOptimizer.AssetBuilder.BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
   at WebOptimizer.AssetMiddleware.HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.<Invoke>g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

This causes a chain reaction and prevents the download of all files.

The published Blazor page does not contain an \obj\WebOptimizerCache folder and WebOptimizer fails to create this folder.

FIX: Manually adding this folder solves all the problems but i wonder what is causing this because the IIS User has all the permissions required. If this is all expected behaviour the documentation should be updated and reflect that it is necessary to create this folder structure to use the WebOptimizer.

A9G-Data-Droid commented 6 months ago

My first time using this library and I hit the same issue today. I'm using the latest version of everything. After creating the missing path the error goes away and the site becomes usable.

Strange note: No cache files are ever created there! So it's throwing an unrecoverable error that crashes the site for all users and then it's not even using the cache folder. I would not consider this exceptional. It needs to log the error and then have a graceful fallback to a different method. Especially when this folder isn't in use!