Export your site

Render every mapped page to plain files with --bevel-build, choose the output folder, and run steps such as Pagefind after the export.

On this page
bash
dotnet run -c Release -- --bevel-build dist

An export renders every mapped page and writes plain files.

Choose where files go

  • dotnet run -- --bevel-build writes into wwwroot. It creates the folder if it is missing.
  • Pass a directory (--bevel-build dist) or set BevelOptions.OutputPath to write elsewhere. Your wwwroot assets are copied next to the pages.
  • $(PublishDir)wwwroot is another directory. See below.

In export mode AddBevel swaps the server for a no-op one, so no port is bound. It exports before other hosted services start, then stops the app. Failures exit non-zero.

Use builder.AddBevel(...), not services.AddBevel. The second fails with a clear error if a server is registered later. You can also call app.ExportStaticSiteAsync() yourself.

Export during publish

Set the property in your project file:

MySite.csprojxml
<PropertyGroup>
  <BevelExportOnPublish>true</BevelExportOnPublish>
</PropertyGroup>

The export runs as a child process with DOTNET_gcServer=0. An explicit DOTNET_gcServer in your environment wins. The published site's GC settings are not touched. See MSBuild.BevelExportOnPublish.

Pick an output style

OutputStyle = BevelOutputStyle.DirectoryIndex (default) writes about/index.html. FlatHtml writes about.html. Links follow the style. See Choose an output style.

Run steps after the export

Since 0.3 Run your own steps, such as a search indexer or a link checker, after a successful export. The files are already written when a step runs.

Program.cscsharp
builder.AddBevel(o => o.AfterExport.Add(async (ctx, ct) =>
{
    await RunPagefindAsync(ctx.OutputDirectory, ct);
}));
Program.cscsharp
builder.Services.AddBevelExportStep<LinkCheckStep>();   // class LinkCheckStep : IBevelExportStep
builder.Services.AddBevelExportStep((ctx, ct) => Task.CompletedTask);   // inline
  • Steps run one at a time. First the BevelOptions.AfterExport callbacks run, then IBevelExportStep services, in registration order.
  • A step that throws fails the export. Bevel logs the error and --bevel-build exits non-zero.
  • Cancellation passes through unchanged. Other failures are wrapped in an InvalidOperationException that names the step.
  • IBevelExportStep services resolve from a new scope for each export.

BevelExportContext gives you:

Member What it holds
OutputDirectory The folder Bevel wrote.
PathBase The configured path base.
SiteUrl The configured site URL, if any.
Routes Every exported route.
Result The exported files. Each BevelExportedFile has Route, RelativePath, FullPath, ByteCount and ContentType.
Services Your service provider.

Example: index the site with Pagefind

Run Pagefind as a pinned binary, not through a package runner. A pinned binary gives you the same index on your machine and in CI. The steps:

  1. Download one release binary of a fixed Pagefind version.
  2. Check its SHA256 against a value you keep in the repository.
  3. Run pagefind --site <export dir>.

This repository does it in eng/pagefind.sh. See Build a docs site for how the docs site wires it in. A step can call the same script after the export:

PagefindStep.cscsharp
using System.Diagnostics;

public sealed class PagefindStep : IBevelExportStep
{
    public async Task RunAsync(BevelExportContext ctx, CancellationToken ct)
    {
        // eng/pagefind.sh downloads the pinned binary, verifies its SHA256, then runs: pagefind --site <dir>
        using var process = Process.Start(new ProcessStartInfo("bash", $"eng/pagefind.sh \"{ctx.OutputDirectory}\"")
            { RedirectStandardOutput = true })!;
        await process.WaitForExitAsync(ct);
        if (process.ExitCode != 0)
            throw new InvalidOperationException($"pagefind exited with code {process.ExitCode}.");
    }
}
Program.cscsharp
builder.Services.AddBevelExportStep<PagefindStep>();

Bevel does not ship an indexer. The step is your code.

Leave files out of the export Since 0.3

The export copies everything in wwwroot (and your asset folders). To keep a file out of the exported site, add a glob to BevelOptions.ExcludeAssets:

csharp
builder.AddBevel(options =>
{
    options.ExcludeAssets.Add("*.map");            // any depth
    options.ExcludeAssets.Add("js/grammar-tests.html");
    options.ExcludeAssets.Add("dev/**");           // a whole folder
});

Patterns match the file's path in the output, ignoring case. * matches within a segment, ** across segments, and a pattern without a / matches at any depth. For an asset folder, the path includes its RequestPath: a file in the folder mounted at /img is img/draft/a.png.

Excluded files are not copied and are not listed in .bevel-output.json. If an earlier export copied one, Bevel removes it as stale. The list is empty by default, so nothing changes until you add to it.

What Bevel will touch

Bevel never deletes recursively. It keeps .bevel-output.json in the output directory. The manifest lists the relative path and SHA-256 of every file Bevel wrote. For each file it also records the ContentType Since 0.3.

  • New files are written. Files from the previous manifest that this export no longer produces are deleted, along with directories left empty. Set BevelOptions.CleanOutputDirectory to false to keep them.
  • If an export would overwrite a file that is not in the previous manifest (a hand-written wwwroot/index.html), it fails before it writes anything and lists the conflicts. Rename the route or the file, or use .ExcludeFromStaticExport(). A file with identical content is adopted.
  • If you edited a file the manifest lists, the export fails and lists it. Restore, delete or move the file.
  • Bevel refuses a custom output directory when it is a filesystem root, your home directory, the content root or one of its ancestors, an ancestor of the web root, a symbolic link, or anything containing a *.csproj, *.sln, *.slnx or .git entry. --bevel-build . fails without touching anything.
  • Two routes with the same output path fail the export.
  • Two routes that differ only by case (/Foo and /foo) depend on the output directory. Bevel probes it with a temporary file. On a case-insensitive file system (Windows, macOS by default) the export fails before writing and names both routes. On a case-sensitive one, Bevel writes both files and logs a warning, because the site breaks when you copy it to a case-insensitive host. Use [Unique(IgnoreCase = true)] on slugs to catch this at build time.
  • Route segments are URL-decoded and checked. .., empty segments, separators, :, *, ? and Windows device names (con, nul, com1) are rejected.

How the export stays safe

  • Bevel renders each page straight into a staging directory (.bevel-staging-<id>) next to the output directory. Memory stays bounded by one page instead of the whole site. Bevel keeps only the path, hash and size of each file.
  • Bevel checks all paths and links, and reports every conflict, before it touches the output directory. If anything fails or you cancel the export, Bevel deletes the staging directory and leaves the old output alone.
  • Then Bevel moves changed files from staging into place (a rename on the same volume) and removes stale files. Unchanged files are not touched. The manifest is written last. Assets are copied directly from their source.
  • The staging directory is never inside the output directory, the web root or an asset folder. When you export into wwwroot, it sits beside it. The next export removes a directory left by a crashed export, but only if it holds Bevel's marker file and no running export holds it.
  • Before the first change, Bevel writes .bevel-output.pending.json (temp file, then move). It holds the old manifest's hashes plus the planned ones, per path. If the export dies midway, the next one accepts any hash listed there as Bevel's own, so a half-replaced output repairs itself. A hash in neither the manifest nor the journal is still a user edit and is refused. Bevel deletes the journal after the final manifest. While it exists, Production serving treats exported files as unverified and falls back to live rendering (with the usual warning).
  • A lock file (.bevel-output.lock) makes a second export fail fast.