How can I get the ID included in the URL issued when deploying?
Pages issues a unique URL (
{unique ID}.{domains}
) for each deployment. Is there a way to get this ID?
This ID and the versionId
from the version metadata seem to be different.
https://developers.cloudflare.com/workers/runtime-apis/bindings/version-metadata/Cloudflare Docs
Version metadata
Exposes Worker version metadata (
versionID
and versionTag
). These fields can be added to events emitted from the Worker to send to downstream observability systems.9 Replies
It's the first part of the deployment ID, in CI you can use
CF_PAGES_URL
, at runtime you'll also have env.CF_PAGES_URL
. Outside of both, you can call the API for the ID or just the urlIn productin builds, the URLs take the format:
<WORKER_NAME>.<SUBDOMAIN>.workers.dev
, but they are still assigned URLs of the format: <VERSION_PREFIX>.<WORKER_NAME>.<SUBDOMAIN>.workers.dev
.
Is it possible to get VERSION_PREFIXZ<VERSION_PREFIX>
when accessing the production environment URL?Oh you want this in Workers?
It's the same pattern, you can use the version metadata binding to grab the first bit of the version and pass in the worker name
${env.VERSION_METADATA.id.substring(0, env.VERSION_METADATA.id.indexOf('-'))-${scriptName}.${subdomain}.workers.dev
Actually we (i work on the same project as @hennin (えなん) ) want this piece of information in Pages Function. The reason is that we want to be able to present a "permlink" on some of the web pages specific to the deployment, even when the user access via the production url (e.g. on a page accessed at
https://site1.pages.dev/foo
, a permlink to itself reading https://abcdefg.site1.pages.dev/foo
where abcdefg
is the deployment id.)
We thought the deployment id would be the same as (a part of to be exact) env.VERSION_METADATA.id as the case for workers, but it seems not actually being the case.
Also I should mention that since the source code is in a mono repo that hosts many other company projects, we can only build locally and deploy via wrangler
so we cannot rely on CF_PAGES_URL
on CF Pages's CI..as i mentioned, env.CF_PAGES_URL exists for runtime
I see. We will try and see whether env.CF_PAGES_URL accessed from production environment also gives us the deployment id instead of the "production url" only
I can get
env.CF_VERSION_METADATA
in _worker.js
, but not env.CF_PAGES_URL
.
Do I need to configure anything?nope, it is available for all Pages Functions - https://test-dcz.pages.dev/cf
and yes, it will give the version url
what's your pages.dev?
Sorry, I misunderstood and
CF_PAGES_URL
is available.
Thank you for your detailed reply!