Skip to content

feat: switch to buildApp hook instead of invoking Vite build twice - #15532

Merged
teemingc merged 35 commits into
version-3from
feat-environment-api-build
Mar 12, 2026
Merged

feat: switch to buildApp hook instead of invoking Vite build twice#15532
teemingc merged 35 commits into
version-3from
feat-environment-api-build

Conversation

@teemingc

@teemingc teemingc commented Mar 10, 2026

Copy link
Copy Markdown
Member

closes #11919
(plugin authors are responsible for declaring which environments they run in)

This PR moves our server/client/service worker build to take advantage of the new buildApp hook https://main.vite.dev/guide/api-environment-frameworks#environments-during-build . Instead of running the Vite build twice, we are setting the environment config settings ahead of time, then building each environment in the buildApp hook.

This is the one of the first steps to adopting the environment API.


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.

@teemingc teemingc added the vite label Mar 10, 2026
@changeset-bot

changeset-bot Bot commented Mar 10, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: bbfe4bd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/kit/src/exports/vite/index.js
Comment thread packages/kit/test/apps/options/test/paths-assets.test.js Outdated
Comment on lines +1499 to +1509
experimental: {
renderBuiltUrl() {
// We could always use a relative asset base path here, but it's better for performance not to.
// E.g. Vite generates `new URL('/asset.png', import.meta).href` for a relative path vs just '/asset.png'.
// That's larger and takes longer to run and also causes an HTML diff between SSR and client
// causing us to do a more expensive hydration check.
return {
relative: kit.paths.relative !== false || !!kit.paths.assets
};
}
},

@teemingc teemingc Mar 11, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had to move this to experimental.renderBuiltUrl because we can't configure the base path per environment. Previously, we were setting a different base path for ssr/client and that was possible because the config plugin hook would run twice per the two Vite builds

@teemingc teemingc Mar 11, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this to the client environment config only causes a weird discrepancy between ssr/client. Inlined styles will be loaded again due to a URL mismatch between the two. Therefore, it's better to keep it at the top-level where it affects the server environments too

@teemingc
teemingc marked this pull request as ready for review March 11, 2026 01:17
Comment thread packages/kit/src/runtime/app/paths/internal/server.js Outdated
Comment thread packages/kit/src/exports/vite/index.js
Comment thread packages/kit/src/exports/vite/index.js Outdated
Comment thread packages/kit/src/exports/vite/index.js
Comment thread packages/kit/src/exports/vite/index.js Outdated
@teemingc teemingc linked an issue Mar 11, 2026 that may be closed by this pull request
@teemingc
teemingc marked this pull request as draft March 12, 2026 11:49
@teemingc

teemingc commented Mar 12, 2026

Copy link
Copy Markdown
Member Author

Seems like adding ssr: true to the Vite config causes all asset paths to be absolute for both server and client builds. Gotta have a think about how to fix this one so that kit.paths.relative: true is respected.

EDIT: ok so we need to set it true at the top-level for the first fix to work, then set it to false in the client environment to disable it there. Setting build.ssr: true in the ssr environment does nothing.

EDIT 2: ok that did not help because it made the client output absolute paths. We need to manually fix things using renderBuiltUrl

@teemingc
teemingc marked this pull request as ready for review March 12, 2026 19:02

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't 100% understand what's going on in renderBuiltUrl but the tests are green so I see no reason not to merge!

@teemingc
teemingc merged commit 8ab200c into version-3 Mar 12, 2026
19 checks passed
@teemingc
teemingc deleted the feat-environment-api-build branch March 12, 2026 20:32
@teemingc teemingc linked an issue May 21, 2026 that may be closed by this pull request
@teemingc teemingc linked an issue May 21, 2026 that may be closed by this pull request
teemingc pushed a commit that referenced this pull request Jul 27, 2026
closes #16530

v2 was unaffected because the secondary client build re-read the Vite
config and created fresh plugin instances, so the extra client-side
pushes landed in an array nobody read. #15532 made one instance serve
both environments.

---

### 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants