Blazor WASM cache errors


I've now written my first Blazor web assembly app and I ran into a few issues after I got it running on the server as a PWA. The first load of the app would work but subsequent loads would fail with a Uncaught SyntaxError: Unexpected token '<' error (because index.html was served when a JavaScript file was requested) or an integrity error because the hash expected for the JavaScript resource didn't match the hash of what was provided (which was index.html).

What fixed the issue for me was updating the shouldServeIndexHtml variable so that it only did so on a request to '/'.

service-worker.js

const shouldServeIndexHtml = event.request.mode === 'navigate' && new URL(event.request.url).pathname === '/';

I was pointed in the right direction by a GitHub issue posted by Abdul Rahman.

It should be noted that this fix might break offline functionality something that was not an issue for my use cases.

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.