Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle ssr for reactivity/window #14681

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-guests-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly handle ssr for `reactivity/window`
8 changes: 5 additions & 3 deletions packages/svelte/src/reactivity/window/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const online = new ReactiveValue(
* `devicePixelRatio.current` is a reactive view of `window.devicePixelRatio`. On the server it is `undefined`.
* Note that behaviour differs between browsers — on Chrome it will respond to the current zoom level,
* on Firefox and Safari it won't.
* @type {{ get current(): number }}
* @type {{ get current(): number | undefined }}
* @since 5.11.0
*/
export const devicePixelRatio = /* @__PURE__ */ new (class DevicePixelRatio {
Expand All @@ -144,11 +144,13 @@ export const devicePixelRatio = /* @__PURE__ */ new (class DevicePixelRatio {
}

constructor() {
this.#update();
if (BROWSER) {
this.#update();
}
}

get current() {
get(this.#dpr);
return window.devicePixelRatio;
return BROWSER ? window.devicePixelRatio : undefined;
}
})();
Original file line number Diff line number Diff line change
@@ -0,0 +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><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import { devicePixelRatio, innerHeight, innerWidth, online, outerHeight, outerWidth, screenLeft, screenTop, scrollX, scrollY } from "svelte/reactivity/window";
</script>

<p>devicePixelRatio: {devicePixelRatio.current}</p>
<p>innerHeight: {innerHeight.current}</p>
<p>innerWidth: {innerWidth.current}</p>
<p>online: {online.current}</p>
<p>outerHeight: {outerHeight.current}</p>
<p>outerWidth: {outerWidth.current}</p>
<p>screenLeft: {screenLeft.current}</p>
<p>screenTop: {screenTop.current}</p>
<p>scrollX: {scrollX.current}</p>
<p>scrollY: {scrollY.current}</p>
2 changes: 1 addition & 1 deletion packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ declare module 'svelte/reactivity/window' {
* @since 5.11.0
*/
export const devicePixelRatio: {
get current(): number;
get current(): number | undefined;
};
class ReactiveValue<T> {

Expand Down
Loading