Skip to content

fix: omit env from server-rendered pages when the client doesn't use dynamic public env vars#16306

Open
Nic-Polumeyv wants to merge 1 commit into
sveltejs:mainfrom
Nic-Polumeyv:omit-env-when-unused
Open

fix: omit env from server-rendered pages when the client doesn't use dynamic public env vars#16306
Nic-Polumeyv wants to merge 1 commit into
sveltejs:mainfrom
Nic-Polumeyv:omit-env-when-unused

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

closes #8946

The follow-up I promised in #16024, a few weeks late.

With experimental.explicitEnvironmentVariables enabled, every server-rendered page ships this, even when the client can never read it:

<script>
	__sveltekit_y93mhh = {
		base: new URL("..", location).pathname.slice(0, -1),
		assets: "/basepath",
		env: {}
	};

Static values are already inlined into the generated $app/env/public module as literals, and only dynamic ones read the embedded object. Rich said as much reviewing #15934, "rendered_env is what gets sent from the server to the client. static variables are already in the client". The embed just never got the gate.

It now sits behind the same client.uses_env_dynamic_public check the $env/dynamic/public branch has used since #11277. #16024 broadened that flag to cover explicit env vars, which is why this had to wait for it. Dev hardcodes the flag to true, so dev is unaffected, and the dynamic case keeps its env: null deferral while prerendering.

One assertion added to the existing options-2 prerender test. Fails on main, passes with this change, in both halves of the DYNAMIC_PUBLIC_ENV matrix.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from cc99e2d:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/cc99e2d55c9f014c2e73c925013f0b41d32adf7c

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16306

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cc99e2d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Omit env from server renders if $env/dynamic/public isn't used

1 participant