Skip to content

Commit dd052dc

Browse files
crisbetoatscott
authored andcommitted
fix(common): server-side rendering error when using in-memory scrolling (#53683)
Fixes that the `BrowserViewportScroller` was throwing an error during server-side rendering because it was accessing `window` directly. Also removes some assertions that aren't necessary anymore. Fixes #53682. PR Close #53683
1 parent 4cb4e98 commit dd052dc

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

packages/common/src/viewport_scroller.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ɵɵdefineInjectable, ɵɵinject} from '@angular/core';
9+
import {inject, PLATFORM_ID, ɵɵdefineInjectable} from '@angular/core';
1010

1111
import {DOCUMENT} from './dom_tokens';
12+
import {isPlatformBrowser} from './platform_id';
1213

1314

1415

@@ -24,7 +25,9 @@ export abstract class ViewportScroller {
2425
static ɵprov = /** @pureOrBreakMyCode */ ɵɵdefineInjectable({
2526
token: ViewportScroller,
2627
providedIn: 'root',
27-
factory: () => new BrowserViewportScroller(ɵɵinject(DOCUMENT), window)
28+
factory: () => isPlatformBrowser(inject(PLATFORM_ID)) ?
29+
new BrowserViewportScroller(inject(DOCUMENT), window) :
30+
new NullViewportScroller()
2831
});
2932

3033
/**
@@ -88,21 +91,15 @@ export class BrowserViewportScroller implements ViewportScroller {
8891
* @returns The position in screen coordinates.
8992
*/
9093
getScrollPosition(): [number, number] {
91-
if (this.supportsScrolling()) {
92-
return [this.window.pageXOffset, this.window.pageYOffset];
93-
} else {
94-
return [0, 0];
95-
}
94+
return [this.window.scrollX, this.window.scrollY];
9695
}
9796

9897
/**
9998
* Sets the scroll position.
10099
* @param position The new position in screen coordinates.
101100
*/
102101
scrollToPosition(position: [number, number]): void {
103-
if (this.supportsScrolling()) {
104-
this.window.scrollTo(position[0], position[1]);
105-
}
102+
this.window.scrollTo(position[0], position[1]);
106103
}
107104

108105
/**
@@ -117,10 +114,6 @@ export class BrowserViewportScroller implements ViewportScroller {
117114
* @see https://html.spec.whatwg.org/#scroll-to-fragid
118115
*/
119116
scrollToAnchor(target: string): void {
120-
if (!this.supportsScrolling()) {
121-
return;
122-
}
123-
124117
const elSelected = findAnchorFromDocument(this.document, target);
125118

126119
if (elSelected) {
@@ -139,9 +132,7 @@ export class BrowserViewportScroller implements ViewportScroller {
139132
* Disables automatic scroll restoration provided by the browser.
140133
*/
141134
setHistoryScrollRestoration(scrollRestoration: 'auto'|'manual'): void {
142-
if (this.supportsScrolling()) {
143-
this.window.history.scrollRestoration = scrollRestoration;
144-
}
135+
this.window.history.scrollRestoration = scrollRestoration;
145136
}
146137

147138
/**
@@ -157,14 +148,6 @@ export class BrowserViewportScroller implements ViewportScroller {
157148
const offset = this.offset();
158149
this.window.scrollTo(left - offset[0], top - offset[1]);
159150
}
160-
161-
private supportsScrolling(): boolean {
162-
try {
163-
return !!this.window && !!this.window.scrollTo && 'pageXOffset' in this.window;
164-
} catch {
165-
return false;
166-
}
167-
}
168151
}
169152

170153
function findAnchorFromDocument(document: Document, target: string): HTMLElement|null {

0 commit comments

Comments
 (0)