This site is being designed in public. Please excuse placeholder content, broken design, etc.!

Tom Callahan’s Blog

I have several projects where I'm deploying to production in one CloudFlare Pages instance, but I want to have a preview instance with a stable domain name for use inside Sanity.

My production Pages instance is set up as a Git integration with automatic deployments on pushes to main and a custom domain. So far so good.

But Pages' Preview deployments use a different subdomain each time, and they also run the same build command so I can't get production to be a static build and preview to be a SSR/SPA build (depending on framework), like I need.

And you can't have multiple Pages sites integrated to the same Git repo -- this would be a much easier solution.

And if you create a Pages application using the dashboard, it defaults to "main" as the production branch. You can't change the production branch after creation, so all deploys are preview deploys (which doesn't work for me since main is already being used by the production Pages instance).

So the workaround is to have two Pages sites, one with the Git integration, and one pushed manually using an npm script. This is OK because the preview is updated on an ad-hoc basis as code changes dictate. The npm script sets the proper SSR/SPA build mode before deploying, and since this mode is always pulling content live from Sanity, it only needs to be updated when I feel it's ready to be.

The problem is that if you create a new Pages application from the dashboard, it does not give you the option to select a production branch, and

So because I've had to look this up multiple times because it just won't stick in my brain, here's the solution:

1. Don't create direct upload Pages instances via the dashboard if you're using it with something in a Git repo.

2. Instead use the Wrangler CLI to create the project, which gives you the option to specify a "production" branch (in our case this is actually a dedicated branch I'm calling "preview"):

wrangler pages project create <PROJECT_NAME> --production-branch preview

(Documented here: https://developers.cloudflare.com/workers/wrangler/commands/#project-create)

It looks like this is a common problem given the number of support threads I see about it in CloudFlare's forum -- I'm not sure why they don't offer the option to change branches for direct upload projects in the dashboard, or why direct upload projects even care what branch is being used -- if I chose to set up an application in a Git repo without using the Git integration, shouldn't it just ignore the branch entirely?

Socials