How to access `VERCEL_URL` env var client-side in a SvelteKit app?

Hi!
According to this documentation, I expect some environment variables (such as VERCEL_URL) to exist and be available in my code.
However, I whenever I try to access those using i.e. import { PUBLIC_VERCEL_URL } from '$env/dynamic/public', I only get undefined.

I added the PUBLIC_ prefix because this section of the doc said that prefixes are added depending on the framework chosen for the project, and I chose SvelteKit.
Although the doc only mentions outdated SvelteKit v0 (I use v2), the next paragraph do mention the correct SvelteKit prefix for such environment variables (PUBLIC_), so I thought everything should work.

When trying to understand what was wrong, I was advised by the Svelte community to display all the environment variables to see if there was some kind of typo. I was surprised to see (when writing the content of import { env } from '$env/dynamic/public' in the browser console) that there was no variables created by Vercel.

The solution is indeed in the SvelteKit doc when looking at the Vercel page.
But it looks like it’s the opposite of what’s in the Vercel doc. And, to be frank, I would rather have a solution similar to what’s explained in the Vercel doc.

So, my question is: what is the correct way to access the env vars automatically created by Vercel?
Is it normal that the Vercel doc is wrong (maybe it’s outdated) or is there currently an issue (meaning that I should expect to have vars such as PUBLIC_VERCEL_URL)?
Is the solution provided in the SvelteKit doc a “workaround”, or is it the correct way to do things from now on?

I hope I’m clear, I must admit I’m a bit confused :sweat_smile:

Hi @opack. I’m checking with the team to be certain, but I believe the server side environment variable loading strategy shown in the Svelte documentation is the right way to go about it.

I checked with the Svelte team and they confirmed that the SvelteKit docs have it right.

I’ve opened a ticket to get the Vercel environment variable docs updated to avoid confusion. Thanks for letting us know about it!

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