sveltejs / sveltejs/kit

(docs) Add a warning in the use of `$env/{dynamic|static}/*` by third-party libraries

Open
#12,028 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

documentation pkg:svelte-package
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

SvelteKit in its configuration allows to customize the config.kit.env.publicPrefix and config.kit.env.privatePrefix, thus making any vars without those prefix unavailable from $env/{dynamic|static}/*

While allowing this configuration is great for projects, this introduces problems with libraries developed for SvelteKit that directly try to import vars from $env/{dynamic|static}/* with a specific name, not taking into account that they might not be available.

Describe the proposed solution

Add a warning in the documentation that libraries developed for SvelteKit should not import from $env/{dynamic|static}/* because the project settings might not allow the expected variables to be available, which would make the library unusable for those kind of projects.

Libraries should be recommended to either get those values passed as configuration or support the fact those variables can be prefixed differently than the default values.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

I came across this problem while using SvelteKitAuth because they do import {env} from "$env/dynamic/private" and then try to access some variables in it.

This actually introduces 2 problems:

  • prefixed variables are not available, breaking the expectations of the library
  • can't pre-render pages, because dynamic env not available during pre-rendering (it's a different problem for sure, but could be avoided with configuration being passed when instantiating the object instead of expecting stuff in the library)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the configuration documentation linked in the issue and review the env section covering config.kit.env.publicPrefix and privatePrefix. Add a warning for library authors about importing from $env/{dynamic|static}/*, including the prefix and prerendering concerns, and recommend configuration or prefix-aware alternatives. Done means the guidance is clear and consistent with the existing documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.