Worker environment variables by route

Is there a way to set different environment variables for different routes the worker executes on? My use case would be a language redirect worker that redirects from domain.tld to domain.tld/en/, domain.tld/de/ and so on based on Accept-Language.
I want to activate this worker on multiple domains where I can set different website urls, available languages and a default languages for each website.

I guess what I could do is check the Request domain and change the variables based on that, but I would prefer a more clean solution.

Thank you for your answers!

Environment Variables are allocated per-script only, are limited in amount (max 30) and very limited in size (max 5kb). So I’d suggest using KV for this and if you want to have it as “secure” as a KV you can encrypt the KV strings using WebCrypto API.

I think you misunderstood, the same worker script would execute on different sites/domains, as the redirection logic is the same across our multi-language websites.
The things different for each website are the following 3 variables. I would prefer not to duplicate the whole worker in order to change only those 3 variables.

const languages = ['en', 'de', 'fr'];
const defaultLanguage = 'en';
const websiteURL = 'https://domain.tld';

Ideally I would activate the worker on the websites/routes I want and pass those 3 variables somehow.

Don’t think so.

You should either use KV or implement a control logic with hard-coded data in your code based on zone ID / destination.

1 Like

Then this would do what you need:

You can use one script with multiple environments, then have different secrets per environment.

Since your worker needs to handle different sites/domains, you would need this regardless to map to the different routes.

1 Like

Thank you, that is exactly what I was looking for. From the documentation it was initially not clear to me that custom environments other than staging and production can be defined, but makes sense!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.