get the url of your app in a worker?

Is there a way to get the url of where your app is deployed inside a worker? In a service worker i can do self.location, but it doesnt exist in a worker Bonus question: easy way to switch that url between localhost for dev
4 Replies
Hello, I’m Allie!
Like this?
export default <ExportedHandler> {
async fetch(req) {
const url = req.url; // Worker's location
}
}
export default <ExportedHandler> {
async fetch(req) {
const url = req.url; // Worker's location
}
}
Passle
Passle10mo ago
Is there any way to get the url in the module scope? Or only at the moment a request comes in?
Hello, I’m Allie!
Only once a request comes in. This is because a Worker can be deployed on multiple Routes(Domains + Pathnames) simultaneously, and can theoretically be configured to answer for any domain, even ones you don't own, so other than reading your wrangler.toml/wrangler.json at build time, there is no way to statically known where a Worker is deployed.
Passle
Passle10mo ago
Right, that makes sense Thank you :)
Want results from more Discord servers?
Add your server
More Posts