The problem:
I have a very similar problem as this question, except I want to use the sveltekit dynamic variables instead of vite.
I have an api client ApiClient.ts
. I would like to use this ApiClient
both on the client-side and on server-side for server side rendering.
I have both sveltekit and api running in the same kubernetes cluster, so I would like to make calls from the server-side directly to http://api:8080 and calls from client-side to https://my-url.com.
I have the INTERNAL_API_URL stored inside private environment and PUBLIC_API_URL in public environemnt.
The question:
Can I conditionally import public environment when running on client and private when running on server environment?
Something like this:
// ApiClient.ts
if (typeof window !== 'undefined') {
import { env as publicEnv } from '$env/dynamic/public'
} else {
import { env as privateEnv } from '$env/dynamic/private'
}
// ...