Skip to content

Cloudflare Pages

Cloudflare Pages hosting attaches to a JavaScript app that Aspire already models — it is not a standalone “point at a folder” resource. You add your frontend with the standard Aspire JavaScript APIs, then mark it for Pages.

builder.AddViteApp("web", "../web") // or AddNodeApp / AddJavaScriptApp
.PublishAsCloudflarePages();
  • aspire run → the app runs as its normal dev server (e.g. Vite).
  • aspire deploy → the app’s build runs, then the output is uploaded with wrangler pages deploy. The Pages project is created if it doesn’t exist.
builder.AddViteApp("web", "../web")
.PublishAsCloudflarePages(options =>
{
options.ProjectName = "my-site"; // defaults to a sanitized resource name
options.OutputDirectory = "dist"; // build output dir (Vite default)
options.Branch = "main"; // production branch
options.BuildCommand = "npm run build";
options.SkipBuild = false; // set true if the output is already built
});

Inject the API’s URL so the frontend can call it:

var api = builder.AddProject<Projects.Api>("api")
.WithHttpEndpoint(name: "http")
.PublishAsCloudflareContainer()
.WithCustomDomain("api.example.com", "your-zone-id");
builder.AddViteApp("web", "../web")
.WithEnvironment("VITE_API_URL", api.GetEndpoint("http"))
.PublishAsCloudflarePages();

The build receives resolved WithEnvironment values in publish context. Endpoint references use the target’s custom domain (https://api.example.com here), not a local dev-server URL. A referenced endpoint must have a Cloudflare target and custom domain; otherwise provide its production URL explicitly. The default workers.dev placeholder is not an account-qualified production address.

In the frontend, read import.meta.env.VITE_API_URL and fetch from it. Configure the API’s CORS policy for the site’s origin.

var zone = builder.AddDeploymentParameter("zone-id");
var host = builder.AddDeploymentParameter("web-hostname");
builder.AddViteApp("web", "../web")
.PublishAsCloudflarePages()
.WithCustomDomain(host, zone);

Custom domains require DNS: Edit and Zone: Read on the relevant zone, and the Zone ID.