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.
Blazor uses C#. You will need the latest version of the .NET SDK ↗︎ to continue creating a Blazor project. If you do not have the SDK installed on your system, download and run the installer.
There are two types of Blazor hosting models: Blazor Server ↗︎, which requires a server to serve the Blazor application to the end user, and Blazor WebAssembly ↗︎, which runs in the browser. Blazor Server is incompatible with Cloudflare Pages' static hosting model, so this guide only uses Blazor WebAssembly.
Create a new Blazor WebAssembly (WASM) application by running the following command:
dotnet new blazorwasm -o my-blazor-projectTo deploy, Cloudflare Pages needs a way to build the Blazor project. In the project's directory root, create a build.sh file (adjusting the -c channel argument to match the .NET SDK version your project targets):
#!/bin/sh
curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh -c 8.0 -InstallDir ./dotnet
./dotnet/dotnet --version
./dotnet/dotnet publish -c Release -o outputYour build.sh file needs to be executable for the build command to work. You can make it so by running chmod +x build.sh.
This guide assumes you already have a fundamental understanding of Git ↗︎. If you are new to Git, refer to the GitHub guide to Git ↗︎ 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.
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 gitignoreCreate a new GitHub repository by visiting repo.new ↗︎. After creating a new repository, go to your newly created project directory to 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 mainTo deploy your site to Pages:
-
In the Cloudflare dashboard, go to the Workers & Pages page.
Go to Workers & Pages ↗ -
Select Create application (if prompted).
-
At the bottom of the page, select the link to continue to Cloudflare Pages (Get started or Continue to Pages).
-
Select Import an existing Git repository.
-
Select the new GitHub repository that you created and then select Begin setup.
-
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.
If you receive the error message Error: Asset "/opt/buildhome/repo/output/wwwroot/_framework/dotnet.wasm" is over the 25MiB limit, resolve this by doing one of the following actions:
- Reduce the size of your assets with the following guide ↗︎.
Or
- Remove the
*.wasmfiles from the output (rm output/wwwroot/_framework/*.wasm) and modify your Blazor application to load the Brotli compressed files ↗︎ instead.
By completing this guide, you have deployed your Blazor site to Cloudflare Pages. For new projects, refer to Cloudflare Workers framework guides, or refer to the Pages framework guides for other Pages setups.