Azure / static-web-apps

Azure Static Web Apps. For bugs and feature requests, please create an issue in this repo. For community discussions, latest updates, kindly refer to the Discussions Tab. To know what's new in Static Web Apps, visit https://aka.ms/swa/ThisMonth
https://aka.ms/swa
MIT License
330 stars 57 forks source link

Blazor WASM PWA service worker unable to start #490

Open jberlyn opened 3 years ago

jberlyn commented 3 years ago

Describe the bug I have a Blazor WebAssembly application that is currently deployed to Azure Static Web Apps. I have setup my Blazor application as a PWA using the steps from the official Microsoft documentation.

https://docs.microsoft.com/en-us/aspnet/core/blazor/progressive-web-app?view=aspnetcore-5.0&tabs=visual-studio

When I run the deployed application, the service worker fails to start as it is unable to access certain resources of my Blazor application, as specified in the service worker assets file.

When I run my application locally with IIS, the service worker starts correctly and I am able to install the application as a PWA.

To Reproduce The application is deployed here: https://witty-pebble-0c280bc10-11.centralus.azurestaticapps.net/

As you can see, when you open it in Google Chrome (or another browser), the service worker fails to reach many resources such as files in my _framework folder, as well as the staticwebapp.config.json file.

The app is deployed using the default GitHub actions build file when first setting up the static web app. The only different is that I have a staticwebapp.config.json file that contains the following:

{
  "navigationFallback": {
    "rewrite": "/index.html"
  }
}

Expected behavior The service worker should start and my user should be prompted to install the application.

Screenshots image image

Device info (if applicable):

anthonychu commented 3 years ago

Looks like this may be related to #259.

There's a slight difference in how you might fix this compared to the previous routes.json file. With staticwebapp.config.json, the file no longer needs to be in the build output. What this means is that you don't need to place it in wwwroot. Instead, place it in the same folder as the Blazor app's csproj file (this should be the same folder that is set as your app_location in the workflow file).

If staticwebapp.config.json isn't in wwwroot, you shouldn't need to modify offlineAssetsExclude.

Fixing this might solve the other issues as well. I think the service worker cancelled the other requests because it couldn't download staticwebapp.config.json.

Zhiyuan-Amos commented 3 years ago

If staticwebapp.config.json isn't in wwwroot, you shouldn't need to modify offlineAssetsExclude.

I tried this couple of days back and it shows the same error above. My staticwebapp.config.json is in the same directory as the Blazor app's .csproj file. Didn't verify if modifying offlineAssetsExclude would resolve this issue.

anthonychu commented 3 years ago

@Zhiyuan-Amos This is surprising. Can you share the repo if it's public? Another thing to try is to remove the staticwebapp.config.json file completely just to see if the error still appears (it would be even more surprising if it's still a problem).

Zhiyuan-Amos commented 3 years ago

@anthonychu I've created PRs on my repository so that you can validate the behaviour on the deployed webpages:

  1. https://github.com/Zhiyuan-Amos/couple-management/pull/64 - behaviour described in my earlier reply
  2. https://github.com/Zhiyuan-Amos/couple-management/pull/65 - staticwebapp.config.json removed
Zhiyuan-Amos commented 3 years ago

@anthonychu do you still need my PRs & the corresponding deployed webpages? :)

jberlyn commented 2 years ago

Any update to this? Has this been resolved? I moved away from Azure Static Web Apps since I created this issue.

Jeroen-VdB commented 1 year ago

The solution from @anthonychu worked for me. After I moved the staticwebapp.config.json from the wwwroot folder to the root of my Blazor project (using .NET 7) I'm able to install my PWA again, when using Azure Static Web Apps.