1
votes

I created a new Blazor PWA WebAssembly (last version default template) project and deployed it in a IIS in Windows Server to try PWA.

Installed the last .NET Core Hosting Bundle.

After publising it, I ran the script in the Microsoft Docs to rename dll files:

dir .\_framework\_bin | rename-item -NewName { $_.name -replace ".dll\b",".bin" } ((Get-Content .\_framework\blazor.boot.json -Raw) -replace '.dll"','.bin"') | Set-Content .\_framework\blazor.boot.json

And the serviceworker renaming code too:

((Get-Content .\service-worker-assets.js -Raw) -replace '.dll"','.bin"') | Set-Content .\service-worker-assets.js

Then I deleted the compressed files as the docs says:

wwwroot\service-worker-assets.js.br
wwwroot\service-worker-assets.js.gz
wwwroot\_framework\blazor.boot.json.br
wwwroot\_framework\blazor.boot.json.gz

But I am still getting an error when I load the app:

enter image description here

What Am I missing here?

I guess that it has to do with the hashes and the renaming thing but cant find any solution in the Blazor´s Github issues.

1
Do you have recompress the updated blazor.boot.json file? - samwu
@samwu No, how do I recompress it? - Andres
You can recompress them using PowerShell or straight .NET code: docs.microsoft.com/en-us/dotnet/api/… docs.microsoft.com/en-us/dotnet/api/… There's probably some utilities on the web you could also use. Though, the gzip and brotli files aren't required, nor used automatically when hosted in IIS. You can enable their usage by updating web.config: raw.githubusercontent.com/dotnet/AspNetCore.Docs/master/… - Swimburger
I had the same problem lately, and what worked for me is to simply also rename the language .dll that are in the depending folders below _bin. so basically in your first command you have to add -r then it works. dir .\_framework\_bin -r | ... - SimonS

1 Answers

1
votes

As a result of your modifications to the blazor.boot.json file, the integrity checks fails. service-worker-assets.js contains a list of files and their integrity hashes which are calculated at the time of publish. You can manually recalculate the hashes using Bash/PowerShell, since you're using IIS, I'll provide the PowerShell script I used for a similar issue:

# make sure you're in the wwwroot folder of the published application
$JsFileContent = Get-Content -Path service-worker-assets.js -Raw
# remove JavaScript from contents so it can be interpreted as JSON
$Json = $JsFileContent.Replace("self.assetsManifest = ", "").Replace(";", "") | ConvertFrom-Json
# grab the assets JSON array
$Assets = $Json.assets
foreach ($Asset in $Assets) {
  $OldHash = $Asset.hash
  $Path = $Asset.url
  
  $Signature = Get-FileHash -Path $Path -Algorithm SHA256
  $SignatureBytes = [byte[]] -split ($Signature.Hash -replace '..', '0x$& ')
  $SignatureBase64 = [System.Convert]::ToBase64String($SignatureBytes)
  $NewHash = "sha256-$SignatureBase64"
  
  If ($OldHash -ne $NewHash) {
    Write-Host "Updating hash for $Path from $OldHash to $NewHash"
    # slashes are escaped in the js-file, but PowerShell unescapes them automatically,
    # we need to re-escape them
    $OldHash = $OldHash.Replace("/", "\/")
    $NewHash = $NewHash.Replace("/", "\/")
    $JsFileContent = $JsFileContent.Replace("""$OldHash""", """$NewHash""")
  }
}

Set-Content -Path service-worker-assets.js -Value $JsFileContent -NoNewline

This script iterates over all files listed inside of service-worker-assets.js, calculates the new hash for each file and updates the hash in the JavaScript file if it's different. You have to execute the script with the published wwwroot folder as the current working directory.

I described this in more detail on my blog: Fix Blazor WebAssembly PWA integrity checks