saeho
saeho3mo ago

Deno Fresh: client side check dev or production

For my Deno Fresh app, I want to do a simple check from the client-side code to see whether it's in development mode or production deployed mode. Use case is: I want to point my API to my localhost if it's in development, otherwise point the API to production server. How can I do this in Deno Fresh?
1 Reply
Deno AI Helper
Deno AI Helper3mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on your description, you might consider using environment variables to differentiate between development and production environments. In a typical setup, you could have an environment variable that you set differently depending on the environment. For example, you might have a DENO_ENV variable that is set to development in your development environment and production in your production environment. On the client side, you could then have a script that checks this variable and sets the API endpoint accordingly. However, since environment variables are server-side and not exposed to the client-side directly, you would need to inject this information into your client-side code during the build process or serve it through an API endpoint. Here's a very basic example of how you might approach this:
let apiEndpoint;

if (Deno.env.get("DENO_ENV") === "development") {
apiEndpoint = "http://localhost:8000";
} else {
apiEndpoint = "https://production-server.com";
}
let apiEndpoint;

if (Deno.env.get("DENO_ENV") === "development") {
apiEndpoint = "http://localhost:8000";
} else {
apiEndpoint = "https://production-server.com";
}
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice. It's also important to ensure that you do not expose any sensitive information to the client-side.