Type
Product improvement
Description
Cloudflare Pages
Benefit
The problem is that build tooling is unable to deterministically find the previous deployment on the same Production branch.
For Preview builds, there is a branch Alias URL, like fix-api.<project>.pages.dev
, but not for Production.
Build tooling that wants to access the current deployment can do so using the Alias URL, but only for Preview builds, not for Production builds (Assume preview on the same branch is disabled). If preview builds are enabled, and they happen to be enabled on the same branch that Production builds from, then the Alias URL is (accidentally) available, but not if Preview builds are turned off.
Then problem has been encountered before:
While the asker was satisfied with the answer given, the problem remains: branch Alias URL’s are only available (haphazardly) if Preview builds are also enabled, and the Production branch happens to be enabled for Previews also.
The “Alias URL” feature needs to work in the Production environment too.
My workaround
My workaround requires an environment variable CF_BUG_756652_WORKAROUND
to be set to hint the build script that it is running in a production environment, and the correct branch URL is <proj>.pages.dev
instead of <branch>.<proj>.pages.dev
# https://community.cloudflare.com/t/branch-alias-url-for-pages-production-env/756652
# On Cloudflare Pages, a branch URL Alias is not available for Production builds (if Preview Deployments are disabled)
# See community discussion above.
# This workaround should work for both preview and production.
# Set the Environment variable CF_BUG_756652_WORKAROUND to contain both the production branch name and the
# project URL, which is what the branch URL would point to. Example:
# CF_BUG_756652_WORKAROUND="release https://my-project.pages.dev"
if [[ ${CF_BUG_756652_WORKAROUND:+isset} ]] ; then
local cf_production_branch_alias_params=($CF_BUG_756652_WORKAROUND)
if [[ ${cf_production_branch_alias_params[0]} == ${CF_PAGES_BRANCH} ]] ; then
BRANCH_URL=${cf_production_branch_alias_params[1]}
else
local escaped_branch=$(echo $CF_PAGES_BRANCH | sed 's@[^[:alnum:]]@-@g;')
BRANCH_URL=$(echo $CF_PAGES_URL | sed -r "s@https://\\w+.@https://$escaped_branch.@")
fi
fi