🚀 Feature Request: Get worker domain/url at build time
Describe the solution
Similar to https://github.com/cloudflare/workers-sdk/issues/2410, when we are building a worker, sometimes we want to know the URL of where it will be deployed while building it. For production cases the main URL will be known, and likely aliased to a non workers.dev URL, but for previews, there's no way to know the "version prefix" until after the version is created. Adding different environments into the mix further complicates things. As does supporting local wrangler dev url.
Possible solutions:
- allow calculating the version prefix before doing a build
- allow creating a version without uploading any files, just to get the new prefix, and then build and upload the files
- allow manually specifying a version prefix rather than using a calculated one
Figuring this out sooner rather than later would be good, considering what is happening with the workers builds git integration and automatically doing branch/PR builds. We'll have same issues there and even more cases to deal with.
Hi, thanks for the feature request :) - would be great as well to know more specifics about your use case and how/why you need the preview url at build time?
I'm building dev tools that help deal with config more generally, so I don't have a specific use case myself... But often it is useful to know the URL at build time so you can include it as a static replacements within your bundled code, use it within config files fed into other tools, or use it when interacting with external services that must be set up as part of a larger deploy process. For example say you need to generate some JSON config file that will be fed into another tool, and part of that config file is the current URL.
We also need this to migrate from Pages to Workers for our static sites. Our use case is as follows:
We need to generate a RSS feed at build time and to allow us to test the RSS feed in preview environments. We need to use absolute URLs for the feed as the RSS specification doesn't support relative URLs.
Manually setting a variable for each test deployment simply isn't a viable option. It works for production as the URL is stable.
CF_PAGES_URL worked perfectly for this requirement and having an equivalent in Workers would enable our migration path.
Another use case is a static website which wants to explicitly name itself. Say you're deploying to https://abcdef01-website.example-cf.workers.dev/ and on that very website you want the string "Hello, you are visiting https://abcdef01-website.example-cf.workers.dev/!" to appear. Now this in itself might seem a bit nonsensical, but there are quite a few cases where such self reference is preferred. For example, "Just run curl https://abcdef01-website.example-cf.workers.dev/install | bash to install!" (which is problematic for many other reasons, but I guess that's off topic here).