Is it possible for a worker to have more than one subdomain (e.g. 1.example.com
and 2.example.com
) and it be able to tell the difference when handling the request? So domain 1 would say “hallo world”, and domain 2 would respond with “Goodbye world”
Short answer: Yes
How do you do it?
Start by adding multiple domains as per documentation
Then in the worker, check request.url
for the hostname e.g.
export default {
async fetch(request) {
const url = new URL(request.url)
if (url.hostname === "example.com")
// Return something
if (url.hostname === "example.net")
// Return something else
// Other stuf here perhaps.
}
}
Check out the worker examples
1 Like