tesar-tech / BlazorStatic

Embrace the capabilities of Blazor on .NET 8 to craft static websites.
https://tesar-tech.github.io/BlazorStatic/
GNU Affero General Public License v3.0
85 stars 9 forks source link
blazor csharp dotnet static-site static-site-generator

#

Discord Nuget (with prereleases) Build, publish to gh pages anN nuget Netlify Status

Embrace the capabilities of Blazor on .NET 8 to craft static websites.

Transform your Blazor app into a static site generator.

How does it work?

You have your SSR Blazor app that you can develop, run, and test. You add the BlazorStatic package and configure it to specify which URLs convert to HTML files. Upon running the app, BlazorStatic fetches the pages' HTML with HttpClient, outputs HTML files and assets to the output folder, which you can deploy to any static hosting.

There are a lot of defaults to keep usage simple, but you can configure it extensively, for example, to match your YAML front matter in markdown files or ignore files in the output.

Features

Getting Started

You can fork BlazorStaticMinimalBlog as a template if you want something quick and easy to start with. It is ready to work with GitHub Pages, and you can have your static page in a matter of minutes.

If you want to start from scratch, follow the steps below.

1. Creating a Project (if you don't have one)

Create a new Blazor project with the following command:

dotnet new blazor --name MyBlazorStaticApp  --interactivity None --empty

2. Installation

Just add the BlazorStatic Nuget package to your project:

cd MyBlazorStaticApp
$ dotnet add package BlazorStatic --prerelease

3. Registering Services

3 lines of code in Program.cs and you are ready to go:

using BlazorStatic;

builder.Services.AddBlazorStaticService();
//...
app.UseBlazorStaticGenerator();

4. Running

Run the app as usual (for example, dotnet run), and you will see the static files generated in the output folder.

You can see similar output in the console:

Copying C:\PathToRepo\MyBlazorStaticApp\wwwroot\app.css to output\app.css
Generating into index.html

Inside the output folder, you will find the generated files. It includes the index.html file and app.css, which BlazorStatic copied from the wwwroot folder. You can deploy the output folder to any static hosting, and it will just work.

With this approach, you can add any page to your app, and it will be generated as a static HTML file. BlazorStatic isn't a framework or a way to build websites. It is just a tool to generate static HTML files from your existing Blazor app.

Markdown Content

Now you want to add some content to your page. BlazorStatic has some tools to handle markdown files.

1. Create a Markdown File

2. Parsing markdown

3. Adding a Page to Use the Content

Now we need to use the content in a page. This is not something BlazorStatic is meant to do. It just makes the content available, and it's up to you how you use it.

It assumes you will have a "/blog" page available (you can also configure this).

A simple example of such a page is this:

@page "/blog"
@page "/blog/{fileName}"
@using BlazorStatic
@using BlazorStatic.Services
@inject BlazorStaticContentService<BlogFrontMatter> blogService

@if (post == null)
{
   <div>There is total of @blogService.BlogPosts.Count() blog posts </div>
}
else
{
   <div>
      @post.FrontMatter.Title
      @((MarkupString)post.HtmlContent)
   </div>
}

@code
{
   [Parameter] public string? FileName { get; set; }
   Post<BlogFrontMatter>? post;

   protected override void OnInitialized()
   {
      if (string.IsNullOrWhiteSpace(FileName)) return;
      post = blogService.BlogPosts.FirstOrDefault(x => x.Url == FileName);
   }
}

Now BlazorStatic will generate a blog folder with an index.html file where you will see the total number of posts and a first-post.html file where you will see the content of the post we created earlier.

Check the More In Depth section to see how to configure BlazorStatic to match your needs.

Deploying

You can deploy the output folder to any static hosting service, such as GitHub Pages, Netlify, Vercel, Azure Static Web Apps, etc.

Detailed instructions are here.

Don't forget to shut down the app after the files are generated; otherwise, the pipeline will hang. You can do it like this:

app.UseBlazorStaticGenerator(shutdownApp: !app.Environment.IsDevelopment());

More in Depth

The best way to see how things work is to check the existing code. For example, this repo contains a feature-rich demonstration of BlazorStatic capabilities.

You can also see a different guide for starting. These instructions will help you build a project similar to BlazorStaticMinimalBlog and are beneficial for understanding the inner workings of BlazorStatic.

Samples

Description Source Live
Page about BlazorStatic (this repo contains the code itself) source live
Minimal blog source live
Zodoc - image processing and deep learning sample source live
❓ Add your page here!!!

Contributions

Contributions are highly encouraged and appreciated. If you find something missing, unclear, or encounter an issue with the code, I warmly welcome your input. Feel free to: