Post

The new #sveltekit explicit environment variables are really great! But one thing that has been bothering me, is how to handle missing env variables during build time. We just found a nice solution that you might want to use as well. 1/ #svelte

0 reposts3 likes

The docs suggest to make them optional during building, but the downside is that you then need to handle the optional type all over the code since the type of the variable will be `undefined | string`. Although this is technically correct it's not great DX and unnecessary during runtime. 2/

This runtimeOnly schema wrapper does two things: 1. It makes the value optional during build, but doesn't make the return type optional. 2. It transforms the value to an Error during build, so that _if_ it is accessed, it will be very apparent and the build will fail in all likelihood

Yes! We tried proxying the access and actually throw, but this just throws whenever it’s imported not when it’s used (since that’s not a feature JS supports - same problem as with signals). It’s not perfect but in all use cases we thought of this caused the build code to fail with a useful message.