Skip to content

Commit 5d30191

Browse files
authored
fix: correctly resolve $app/state (alternative) (#13192)
fixes #13179 Uses the same approach as the other virtual modules to differentiate between client and server
1 parent 4d91b5c commit 5d30191

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

.changeset/odd-mirrors-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correctly resolve `$app/state` on the server with Vite 5

packages/kit/scripts/generate-dts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ await createBundle({
1414
'$app/navigation': 'src/runtime/app/navigation.js',
1515
'$app/paths': 'src/runtime/app/paths/types.d.ts',
1616
'$app/server': 'src/runtime/app/server/index.js',
17-
'$app/state': 'src/runtime/app/state/client.js',
17+
'$app/state': 'src/runtime/app/state/index.js',
1818
'$app/stores': 'src/runtime/app/stores.js'
1919
},
2020
include: ['src']

packages/kit/src/runtime/app/state/client.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,6 @@ import {
55
} from '../../client/state.svelte.js';
66
import { stores } from '../../client/client.js';
77

8-
/**
9-
* A reactive object with information about the current page, serving several use cases:
10-
* - retrieving the combined `data` of all pages/layouts anywhere in your component tree (also see [loading data](https://svelte.dev/docs/kit/load))
11-
* - retrieving the current value of the `form` prop anywhere in your component tree (also see [form actions](https://svelte.dev/docs/kit/form-actions))
12-
* - retrieving the page state that was set through `goto`, `pushState` or `replaceState` (also see [goto](https://svelte.dev/docs/kit/$app-navigation#goto) and [shallow routing](https://svelte.dev/docs/kit/shallow-routing))
13-
* - retrieving metadata such as the URL you're on, the current route and its parameters, and whether or not there was an error
14-
*
15-
* ```svelte
16-
* <!--- file: +layout.svelte --->
17-
* <script>
18-
* import { page } from '$app/state';
19-
* </script>
20-
*
21-
* <p>Currently at {page.url.pathname}</p>
22-
*
23-
* {#if page.error}
24-
* <span class="red">Problem detected</span>
25-
* {:else}
26-
* <span class="small">All systems operational</span>
27-
* {/if}
28-
* ```
29-
*
30-
* On the server, values can only be read during rendering (in other words _not_ in e.g. `load` functions). In the browser, the values can be read at any time.
31-
*
32-
* @type {import('@sveltejs/kit').Page}
33-
*/
348
export const page = {
359
get data() {
3610
return _page.data;
@@ -58,12 +32,6 @@ export const page = {
5832
}
5933
};
6034

61-
/**
62-
* An object representing an in-progress navigation, with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
63-
* Values are `null` when no navigation is occurring, or during server rendering.
64-
* @type {import('@sveltejs/kit').Navigation | { from: null, to: null, type: null, willUnload: null, delta: null, complete: null }}
65-
*/
66-
// @ts-expect-error
6735
export const navigating = {
6836
get from() {
6937
return _navigating.current ? _navigating.current.from : null;
@@ -92,10 +60,6 @@ Object.defineProperty(navigating, 'current', {
9260
}
9361
});
9462

95-
/**
96-
* A reactive value that's initially `false`. If [`version.pollInterval`](https://svelte.dev/docs/kit/configuration#version) is a non-zero value, SvelteKit will poll for new versions of the app and update `current` to `true` when it detects one. `updated.check()` will force an immediate check, regardless of polling.
97-
* @type {{ get current(): boolean; check(): Promise<boolean>; }}
98-
*/
9963
export const updated = {
10064
get current() {
10165
return _updated.current;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
page as client_page,
3+
navigating as client_navigating,
4+
updated as client_updated
5+
} from './client.js';
6+
import {
7+
page as server_page,
8+
navigating as server_navigating,
9+
updated as server_updated
10+
} from './server.js';
11+
import { BROWSER } from 'esm-env';
12+
13+
/**
14+
* A reactive object with information about the current page, serving several use cases:
15+
* - retrieving the combined `data` of all pages/layouts anywhere in your component tree (also see [loading data](https://svelte.dev/docs/kit/load))
16+
* - retrieving the current value of the `form` prop anywhere in your component tree (also see [form actions](https://svelte.dev/docs/kit/form-actions))
17+
* - retrieving the page state that was set through `goto`, `pushState` or `replaceState` (also see [goto](https://svelte.dev/docs/kit/$app-navigation#goto) and [shallow routing](https://svelte.dev/docs/kit/shallow-routing))
18+
* - retrieving metadata such as the URL you're on, the current route and its parameters, and whether or not there was an error
19+
*
20+
* ```svelte
21+
* <!--- file: +layout.svelte --->
22+
* <script>
23+
* import { page } from '$app/state';
24+
* </script>
25+
*
26+
* <p>Currently at {page.url.pathname}</p>
27+
*
28+
* {#if page.error}
29+
* <span class="red">Problem detected</span>
30+
* {:else}
31+
* <span class="small">All systems operational</span>
32+
* {/if}
33+
* ```
34+
*
35+
* On the server, values can only be read during rendering (in other words _not_ in e.g. `load` functions). In the browser, the values can be read at any time.
36+
*
37+
* @type {import('@sveltejs/kit').Page}
38+
*/
39+
export const page = BROWSER ? client_page : server_page;
40+
41+
/**
42+
* An object representing an in-progress navigation, with `from`, `to`, `type` and (if `type === 'popstate'`) `delta` properties.
43+
* Values are `null` when no navigation is occurring, or during server rendering.
44+
* @type {import('@sveltejs/kit').Navigation | { from: null, to: null, type: null, willUnload: null, delta: null, complete: null }}
45+
*/
46+
// @ts-expect-error
47+
export const navigating = BROWSER ? client_navigating : server_navigating;
48+
49+
/**
50+
* A reactive value that's initially `false`. If [`version.pollInterval`](https://svelte.dev/docs/kit/configuration#version) is a non-zero value, SvelteKit will poll for new versions of the app and update `current` to `true` when it detects one. `updated.check()` will force an immediate check, regardless of polling.
51+
* @type {{ get current(): boolean; check(): Promise<boolean>; }}
52+
*/
53+
export const updated = BROWSER ? client_updated : server_updated;

packages/kit/src/runtime/app/state/package.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)