Deploy Precompiled Blazor WebAssembly Apps to GitHub Pages.
Please view the live demo at https://arvinboggs.github.io/BlazorGitHubPagesDemo
Project page: https://github.com/arvinboggs/BlazorGitHubPages
- Open Visual Studio 2022 (or later).
- Create a new project.
- Choose Blazor WebAssembly App as the project type.
- On the Additional Information page, tick the Progressive Web Application checkbox.
This is important so Visual Studio will include the necessary files needed to make your page reload offline.
- After creating the project, open the wwwroot\index.html file and delete the line that has the following code:
<base href="/" />
. Including this line will make all relative URL’s base to the root of the subdomain. More info.GitHub Pages have URL’s in the format of https://<username>.github.io/<repository name>. So, if your GitHub Page URL is https://my_username.github.io/MyRepository, requests to myScript.js, for example, will be fetched from https://my_username.github.io/myScript.js — which is not what we wanted. We want it to be fetched from https://my_username.github.io/MyRepository/myScript.js.
- Add a file named .gitattributes to the root of your project. Add the following content to the file:
1 2 3 4 5
# put these in .gitattributes file *.js binary *.json binary *.css binary *.html binary
This is needed because line endings are converted to Unix style when you commit source files like js, json, css, & html. More info. And we need to push source files without any modification.
- Add a file named .nojekyll to the root of your project.
This is needed to prevent Jekyll from processing your files, including your directories that start with underscore. More info.
Publish
your project by running the following at the root directory of your project:dotnet publish --configuration Release
Push
everything in the \bin\Release\net6.0\publish\wwwroot directory to your repository.Optionally, exclude these files since GitHub have their own compression.
- _framework*.br
- _framework*.gz
- Done. Your website is now live.