Skip to content

Commit edfc0a9

Browse files
fix: correctly handle ssr for reactivity/window (#14681)
1 parent 1d7f0fe commit edfc0a9

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

.changeset/khaki-guests-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly handle ssr for `reactivity/window`

packages/svelte/src/reactivity/window/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const online = new ReactiveValue(
124124
* `devicePixelRatio.current` is a reactive view of `window.devicePixelRatio`. On the server it is `undefined`.
125125
* Note that behaviour differs between browsers — on Chrome it will respond to the current zoom level,
126126
* on Firefox and Safari it won't.
127-
* @type {{ get current(): number }}
127+
* @type {{ get current(): number | undefined }}
128128
* @since 5.11.0
129129
*/
130130
export const devicePixelRatio = /* @__PURE__ */ new (class DevicePixelRatio {
@@ -144,11 +144,13 @@ export const devicePixelRatio = /* @__PURE__ */ new (class DevicePixelRatio {
144144
}
145145

146146
constructor() {
147-
this.#update();
147+
if (BROWSER) {
148+
this.#update();
149+
}
148150
}
149151

150152
get current() {
151153
get(this.#dpr);
152-
return window.devicePixelRatio;
154+
return BROWSER ? window.devicePixelRatio : undefined;
153155
}
154156
})();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--[--><p>devicePixelRatio: </p> <p>innerHeight: </p> <p>innerWidth: </p> <p>online: </p> <p>outerHeight: </p> <p>outerWidth: </p> <p>screenLeft: </p> <p>screenTop: </p> <p>scrollX: </p> <p>scrollY: </p><!--]-->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import { devicePixelRatio, innerHeight, innerWidth, online, outerHeight, outerWidth, screenLeft, screenTop, scrollX, scrollY } from "svelte/reactivity/window";
3+
</script>
4+
5+
<p>devicePixelRatio: {devicePixelRatio.current}</p>
6+
<p>innerHeight: {innerHeight.current}</p>
7+
<p>innerWidth: {innerWidth.current}</p>
8+
<p>online: {online.current}</p>
9+
<p>outerHeight: {outerHeight.current}</p>
10+
<p>outerWidth: {outerWidth.current}</p>
11+
<p>screenLeft: {screenLeft.current}</p>
12+
<p>screenTop: {screenTop.current}</p>
13+
<p>scrollX: {scrollX.current}</p>
14+
<p>scrollY: {scrollY.current}</p>

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ declare module 'svelte/reactivity/window' {
20152015
* @since 5.11.0
20162016
*/
20172017
export const devicePixelRatio: {
2018-
get current(): number;
2018+
get current(): number | undefined;
20192019
};
20202020
class ReactiveValue<T> {
20212021

0 commit comments

Comments
 (0)