breaking: serve public env dynamically, prevent use of dynamic env vars during prerendering#11277
Conversation
🦋 Changeset detectedLatest commit: d4f7ce2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
|
Is there much value in an |
|
True, if someone upgrades to SvelteKit 2 they might as well upgrade to adapter-static v3. We should add that to the migration script though (auto-bump the adapter-static version) |
dummdidumm
left a comment
There was a problem hiding this comment.
If you want to do the _app change (and if it's easy) feel free to do so, else I think this is good to merge.
I think this might be incorrect. Even with prerendering disabled it now prevents any My specific use-case is using the The The build initially compiles OK but then errors at the pre-render stage even with pre-rendering disabled: +layout.server.ts: export const prerender = falseBuild output: It's possible to workaround it by adding tests for Scratch that: I think it was all down to me accessing it outside of the |
|
@CaptainCodeman this does seem to be an issue, see #11341. was there a workaround? if so, could you share it in #11341 perhaps? I did not quite understand the |
…are unused (#16306) closes #8946 The follow-up I promised in #16024, a few weeks late. Retargeting it onto `version-3` turned it into a test, because the fix I wrote for `main` is already there. The env embed sits behind the same `client.uses_env_dynamic_public` check that the `$env/dynamic/public` branch has used since #11277: ```js if (client.uses_env_dynamic_public) { properties.push(`env: ${load_env_eagerly ? 'null' : devalue.uneval(env.rendered_env)}`); } ``` What's left is the regression coverage. On kit 2 with `experimental.explicitEnvironmentVariables` enabled, every server-rendered page ships an `env: {}` object the client can never read, since static values are inlined into the generated `$app/env/public` module as literals. 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". One assertion added to the existing options-2 prerender test, so version-3 can't regress into shipping the dead object again. Verified it passes on `version-3` and that the same assertion fails on `main` without the omitted fix. --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] 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 - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. ### Tests - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check` ### Changesets - [x] 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 - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Fixes #10008. This does two things:
$env/dynamic/publicwhile prerendering (use$env/static/publicinstead)$env/dynamic/publicfrom a new dynamically rendered/_env.jsmodule, instead of populating it with stale build-time valuesTODO:
%sveltekit.env.PUBLIC_WHATEVER%to continue working (seems reasonable that this would be an exception to the 'you can't use dynamic public env vars during prerendering' rule)$env/dynamic/privateduring prerendering_env.jswhen prerendering if we detect that$env/dynamic/publicis used somewhere in the app_env.jsconfigurable, viaconfig.kit.env.publicModuleadapter-static(and other adapters, if they want) to generate the_env.jsmodule at build timePlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.