Deploy a Blazor site
Blazor is an SPA framework that can use C# code, rather than JavaScript in the browser. In this guide, you will build a site using Blazor, and deploy it using Cloudflare Pages.
Install .NET
Blazor uses C#, so you will need to install the .NET SDK by grabbing the newest installation from the .NET downloads page and download and running the installer.
Creating a new Blazor WASM project
There are two types of Blazor projects: Blazor Server applications, which run on the server, and Blazor WASM (WebAssembly), which run in the browser. Since Blazor Server is not static, this guide will use Blazor WASM. Create a new Blazor WASM application by running the following command in your terminal:
$ dotnet new blazorwasm my-blazor-project
Creating the build script
To deploy, Cloudflare Pages will need a way to build the Blazor project. In the project’s directory root, create a build.sh
file. Populate the file with this:
#!/bin/sh
curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh -c 6.0 -InstallDir ./dotnet6
./dotnet6/dotnet --version
./dotnet6/dotnet publish -c Release -o output
Before you continue
All of the framework guides assume you already have a fundamental understanding of Git. If you are new to Git, refer to this summarized Git handbook on how to set up Git on your local machine.
If you clone with SSH, you must generate SSH keys on each computer you use to push or pull from GitHub.
Refer to the GitHub documentation and Git documentation for more information.
Create a .gitignore
file
Creating a .gitignore
file ensures that only what is needed gets pushed onto your GitHub repository. Create a .gitignore
file by running the following command:
$ dotnet new gitignore
Create a GitHub repository
Create a new GitHub repository by visiting repo.new. After creating a new repository, prepare and push your local application to GitHub by running the following commands in your terminal:
$ git init
$ git remote add origin https://github.com/<your-gh-username>/<repository-name>
$ git add .
$ git commit -m "Initial commit"
$ git branch -M main
$ git push -u origin main
Deploying with Cloudflare Pages
Deploy your site to Pages by logging in to the Cloudflare dashboard > Account Home > Pages dashboard and selecting Create a project. Select the new GitHub repository that you created and, in the Set up builds and deployments section, provide the following information:
Configuration option | Value |
---|---|
Production branch | main |
Build command | ./build.sh |
Build directory | output/wwwroot |
After configuring your site, you can begin your first deploy. You should see Cloudflare Pages installing dotnet
, your project dependencies, and building your site, before deploying it.
After deploying your site, you will receive a unique subdomain for your project on *.pages.dev
.
Every time you commit new code to your Blazor site, Cloudflare Pages will automatically rebuild your project and deploy it. You will also get access to preview deployments on new pull requests, so you can preview how changes look to your site before deploying them to production.
Troubleshooting
A file is over the 25 MiB limit
If you receive the error message Error: Asset "/opt/buildhome/repo/output/wwwroot/_framework/dotnet.wasm" is over the 25MiB limit
, you have two options:
- Reduce the size of your assets with the following guide.
Or
- Remove the
*.wasm
files from the output (rm output/wwwroot/_framework/*.wasm
) and modify your Blazor application to load the Brotli compressed files instead.
Learn more
By completing this guide, you have successfully deployed your Blazor site to Cloudflare Pages. To get started with other frameworks, refer to the list of Framework guides.