Skip to content

Commit

Permalink
Adding fix for service worker assets issue to deployment workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
gowon committed Jun 17, 2023
1 parent 7e4c083 commit 21418a5
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions .github/workflows/gh-pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,53 @@ jobs:
dotnet-version: 7.x

- name: Publish with dotnet
run: dotnet publish --configuration Release --output build --nologo
run: dotnet publish --configuration Release --output release --nologo

# changes the base-tag in index.html to match GitHub Pages repository subdirectory
- name: Change base-tag in index.html
run: sed -i 's/<base href="\/" \/>/<base href="\/amzn-bkmt\/" \/>/g' build/wwwroot/index.html
run: sed -i 's/<base href="\/" \/>/<base href="\/amzn-bkmt\/" \/>/g' release/wwwroot/index.html

# changes the base-tag in index.html from '/' to 'BlazorGitHubPagesDemo' to match GitHub Pages repository subdirectory
- name: Fix service-worker-assets.js hashes
working-directory: release/wwwroot
run: |
jsFile=$(<service-worker-assets.js)
# remove JavaScript from contents so it can be interpreted as JSON
json=$(echo "$jsFile" | sed "s/self.assetsManifest = //g" | sed "s/;//g")
# grab the assets JSON array
assets=$(echo "$json" | jq '.assets[]' -c)
for asset in $assets
do
oldHash=$(echo "$asset" | jq '.hash')
#remove leading and trailing quotes
oldHash="${oldHash:1:-1}"
path=$(echo "$asset" | jq '.url')
#remove leading and trailing quotes
path="${path:1:-1}"
newHash="sha256-$(openssl dgst -sha256 -binary $path | openssl base64 -A)"
if [ $oldHash != $newHash ]; then
# escape slashes for json
oldHash=$(echo "$oldHash" | sed 's;/;\\/;g')
newHash=$(echo "$newHash" | sed 's;/;\\/;g')
echo "Updating hash for $path from $oldHash to $newHash"
# escape slashes second time for sed
oldHash=$(echo "$oldHash" | sed 's;/;\\/;g')
jsFile=$(echo -n "$jsFile" | sed "s;$oldHash;$newHash;g")
fi
done
echo -n "$jsFile" > service-worker-assets.js
# copy index.html to 404.html to serve the same file when a file is not found
- name: Copy index.html to 404.html
run: cp release/wwwroot/index.html release/wwwroot/404.html

# add .nojekyll file to tell GitHub pages to not treat this as a Jekyll project. (Allow files and folders starting with an underscore)
- name: Add .nojekyll file
run: touch build/wwwroot/.nojekyll
run: touch release/wwwroot/.nojekyll

- name: Deploy to Github Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: build/wwwroot
folder: release/wwwroot

0 comments on commit 21418a5

Please sign in to comment.