Skip to content

chore: split $app/state into client/server halves - #16801

Merged
Rich-Harris merged 10 commits into
version-3from
split-app-state
Aug 14, 2026
Merged

chore: split $app/state into client/server halves#16801
Rich-Harris merged 10 commits into
version-3from
split-app-state

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

Follow-up to #16795 — this applies the same thinking to $app/state. It allows us to get rid of the awful BROWSER hack and colocate logic more sensibly, with less indirection.

Again, no changeset, since no user-observable changes.


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

pkg-svelte-dev Bot commented Aug 14, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 76c6838:

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

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

@changeset-bot

changeset-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 76c6838

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

},
props: {
page,
page: { ...page },

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.

this was a latent bug that would likely have come to bite us eventually — we were mutating the actual page object later (navigation_result.props.page.foo = bar) when we should only ever mutate a copy

Comment thread packages/kit/src/runtime/client/client.js
Rich-Harris and others added 3 commits August 14, 2026 11:38
…tches the import source `#app/state/client`, so the spec fails to load with a ReferenceError.

This commit fixes the issue reported at packages/kit/src/runtime/client/remote-functions/shared.transport.spec.js:16

## Bug

After the module split, `shared.svelte.js` now imports its reactive state from `#app/state/client`:

```js
import { navigating, page, notify_version } from '#app/state/client';
```

But `shared.transport.spec.js` still mocked the old path:

```js
vi.mock(new URL('../state.svelte.js', import.meta.url).pathname, () => ({ ... }));
```

Since that path is no longer imported by `shared.svelte.js`, the mock intercepts nothing. Vitest then loads the real `#app/state/client` → `src/runtime/app/state/client.svelte.js`, which references the build-time global `__SVELTEKIT_APP_VERSION_POLL_INTERVAL__` at module top-level. That global is not defined/stubbed in the test environment, so the module throws:

```
ReferenceError: __SVELTEKIT_APP_VERSION_POLL_INTERVAL__ is not defined
 ❯ src/runtime/app/state/client.svelte.js:131
 ❯ src/runtime/client/remote-functions/shared.svelte.js:9
 ❯ src/runtime/client/remote-functions/shared.transport.spec.js:23
```

The whole spec file fails to load — 0 tests run.

## Fix

Point the mock at the actual import specifier `#app/state/client`:

```js
vi.mock('#app/state/client', () => ({
	navigating: { current: null },
	page: { url: new URL('http://localhost/') },
	updated: { current: false, check: () => Promise.resolve(false) },
	notify_version: () => {}
}));
```

## Verification

```
npx vitest run --config vitest.kit.config.js --project kit-client-runtime \
  src/runtime/client/remote-functions/shared.transport.spec.js
```

Before: `FAIL (0 test)` with the ReferenceError. After: `1 passed (1)` file, `3 passed (3)` tests.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
…eset invalidations because the preserved values are written before `apply_navigation_result` overwrites them

This commit fixes the issue reported at packages/kit/src/runtime/client/client.js:655

## Bug

In `_invalidate` (`packages/kit/src/runtime/client/client.js` ~line 655), the refactor to use `update_page(...)` reversed the effective order of operations relative to the original code.

**Original code:**
```js
if (!reset_page_state) {
    navigation_result.props.page.state = prev_state;
}
navigation_result.props.page.shallow = prev_shallow;
apply_navigation_result(navigation_result);
```
This mutated the *page object inside the navigation result*, so when `apply_navigation_result` later did `Object.assign(page, result.props.page)`, the preserved `prev_state`/`prev_shallow` values were the ones assigned — they won.

**Refactored (buggy) code:**
```js
if (!reset_page_state) {
    update_page({ state: prev_state });   // writes to internal_page
}
update_page({ shallow: prev_shallow });   // writes to internal_page
apply_navigation_result(navigation_result); // update_page(result.props.page) → Object.assign(internal_page, ...)
```
Now the preserved values are written to `internal_page` *first*, then `apply_navigation_result` calls `update_page(result.props.page)` which does `Object.assign(internal_page, result.props.page)`, clobbering them.

## Concrete trigger

In `get_navigation_result_from_branch`, when `page_changed` is true (the normal invalidation case, since `data_changed` makes `page_changed` true), `result.props.page` is rebuilt as a fresh object containing:
```js
state: {},
shallow: null,
```
So on any invalidation that changes data (e.g. `refresh`/`refreshAll` via `_invalidate(false)`), `apply_navigation_result` overwrites the just-restored `prev_state` with `{}` and `prev_shallow` with `null`. Result: `page.state` set through `goto`/shallow routing is lost, and `page.shallow` is reset to `null`, even though the caller requested state preservation (`reset_page_state === false`).

## Fix

Move the `update_page({ state: prev_state })` / `update_page({ shallow: prev_shallow })` calls to run *after* `apply_navigation_result(navigation_result)`, so the preserved values are applied last and win — matching the original `Object.assign(page, result.props.page)` ordering semantics.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
@Rich-Harris
Rich-Harris merged commit 26fbf89 into version-3 Aug 14, 2026
27 checks passed
@Rich-Harris
Rich-Harris deleted the split-app-state branch August 14, 2026 16:39
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.

2 participants